use of spoon.reflect.code.CtVariableAccess in project Ex2Amplifier by STAMP-project.
the class ArgumentsExtractor method performExtraction.
public static CtMethod<?> performExtraction(CtMethod<?> ctMethod) {
final CtMethod<?> ctMethodWithoutAssertion = new AssertionRemover().removeAssertion(ctMethod);
final Factory factory = ctMethodWithoutAssertion.getFactory();
final CtMethod<?> extractedMethod = ctMethodWithoutAssertion.clone();
extractedMethod.setSimpleName("extract_" + ctMethodWithoutAssertion.getSimpleName());
new ArrayList<>(extractedMethod.getThrownTypes()).forEach(extractedMethod::removeThrownType);
ctMethodWithoutAssertion.getAnnotations().forEach(extractedMethod::removeAnnotation);
final int[] count = new int[1];
final Map<CtAbstractInvocation<?>, List<CtVariableAccess>> parametersPerInvocation = new HashMap<>();
extractedMethod.getElements(Ex2Amplifier.CT_LITERAL_TYPE_FILTER).stream().filter(literal -> !(literal.getValue() instanceof String)).forEach(ctLiteral -> {
final CtParameter parameter = factory.createParameter(extractedMethod, Utils.getRealTypeOfLiteral(ctLiteral), "lit" + count[0]++);
final CtParameterReference<?> parameterReference = factory.createParameterReference();
parameterReference.setSimpleName(parameter.getSimpleName());
parameterReference.setType(parameter.getType());
final CtVariableAccess<?> variableRead = factory.createVariableRead(parameterReference, false);
final CtAbstractInvocation invocation = ctLiteral.getParent(CtAbstractInvocation.class);
if (invocation != null) {
if (!parametersPerInvocation.containsKey(invocation)) {
parametersPerInvocation.put(invocation, new ArrayList<>(invocation.getArguments()));
}
if (ctLiteral.getParent() instanceof CtUnaryOperator) {
ctLiteral.getParent().replace(variableRead);
} else {
ctLiteral.replace(variableRead);
}
} else {
ctLiteral.replace(variableRead);
}
});
extractedMethod.setThrownTypes(ctMethod.getThrownTypes());
return extractedMethod;
}
Aggregations