use of spoon.reflect.declaration.CtParameter 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;
}
use of spoon.reflect.declaration.CtParameter in project dspot by STAMP-project.
the class TestCollectionCreator method testCreateCollection.
@Test
public void testCreateCollection() throws Exception {
final CtTypeReference typeList = Utils.findMethod("fr.inria.statementadd.ClassTarget", "getList").getType();
assertEquals("java.util.Collections.<fr.inria.statementadd.ClassParameterAmplify>emptyList()", CollectionCreator.generateCollection(typeList, "List", List.class).toString());
assertEquals("java.util.Collections.singletonList(new fr.inria.statementadd.ClassParameterAmplify(1224731715))", CollectionCreator.generateCollection(typeList, "List", List.class).toString());
assertEquals("java.util.Collections.<java.lang.Object>emptyList()", CollectionCreator.generateCollection(Utils.getFactory().Type().get(Object.class).getReference(), "List", List.class).toString());
final CtTypeReference typeSet = ((CtParameter) Utils.findMethod("fr.inria.statementadd.ClassTarget", "getSizeOfTypedCollection").getParameters().get(0)).getType();
assertEquals("java.util.Collections.<fr.inria.statementadd.ClassParameterAmplify>emptySet()", CollectionCreator.generateCollection(typeSet, "Set", Set.class).toString());
// This done because of the randomness
CollectionCreator.generateCollection(typeSet, "Set", Set.class).toString();
assertEquals("java.util.Collections.singleton(new fr.inria.statementadd.ClassParameterAmplify(-1355944483))", CollectionCreator.generateCollection(typeSet, "Set", Set.class).toString());
}
Aggregations