Search in sources :

Example 6 with CtParameterReference

use of spoon.reflect.reference.CtParameterReference 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;
}
Also used : Ex2Amplifier(eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier) Utils(eu.stamp.project.ex2amplifier.Utils) CtParameterReference(spoon.reflect.reference.CtParameterReference) HashMap(java.util.HashMap) Factory(spoon.reflect.factory.Factory) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) Map(java.util.Map) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtVariableAccess(spoon.reflect.code.CtVariableAccess) CtParameter(spoon.reflect.declaration.CtParameter) CtMethod(spoon.reflect.declaration.CtMethod) HashMap(java.util.HashMap) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) AssertionRemover(fr.inria.diversify.dspot.assertGenerator.AssertionRemover) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

CtParameterReference (spoon.reflect.reference.CtParameterReference)6 Test (org.junit.Test)5 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)5 CtParameter (spoon.reflect.declaration.CtParameter)4 Factory (spoon.reflect.factory.Factory)3 List (java.util.List)2 Launcher (spoon.Launcher)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtVariableRead (spoon.reflect.code.CtVariableRead)2 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)2 Tacos (spoon.test.replace.testclasses.Tacos)2 Utils (eu.stamp.project.ex2amplifier.Utils)1 Ex2Amplifier (eu.stamp.project.ex2amplifier.amplifier.Ex2Amplifier)1 AssertionRemover (fr.inria.diversify.dspot.assertGenerator.AssertionRemover)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)1 CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1