Search in sources :

Example 6 with CtVariableAccess

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;
}
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

CtVariableAccess (spoon.reflect.code.CtVariableAccess)6 Factory (spoon.reflect.factory.Factory)5 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CtAssignment (spoon.reflect.code.CtAssignment)3 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtMethod (spoon.reflect.declaration.CtMethod)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 HashMap (java.util.HashMap)2 Launcher (spoon.Launcher)2 CtBlock (spoon.reflect.code.CtBlock)2 CtComment (spoon.reflect.code.CtComment)2 CtExpression (spoon.reflect.code.CtExpression)2 CtIf (spoon.reflect.code.CtIf)2 CtInvocation (spoon.reflect.code.CtInvocation)2 CtParameter (spoon.reflect.declaration.CtParameter)2 Utils (eu.stamp.project.ex2amplifier.Utils)1