Search in sources :

Example 1 with CtAbstractInvocation

use of spoon.reflect.code.CtAbstractInvocation in project Ex2Amplifier by STAMP-project.

the class Utils method getRealTypeOfLiteral.

public static CtTypeReference<?> getRealTypeOfLiteral(CtLiteral<?> literal) {
    if (literal.getValue() instanceof Number) {
        final CtTypedElement typedParent = literal.getParent(CtTypedElement.class);
        if (typedParent != null) {
            // special treatment for int literal
            if (typedParent instanceof CtAbstractInvocation) {
                final CtExecutableReference<?> executable = ((CtAbstractInvocation) typedParent).getExecutable();
                final int indexOf = ((CtAbstractInvocation) typedParent).getArguments().indexOf(literal);
                final CtTypeReference<?> ctTypeReference = executable.getParameters().get(indexOf);
                if (Number.class.isAssignableFrom(ctTypeReference.getActualClass())) {
                    return ctTypeReference;
                } else {
                    return literal.getType();
                }
            } else if (typedParent.getType() instanceof CtArrayTypeReference) {
                return ((CtArrayTypeReference) typedParent.getType()).getComponentType();
            } else {
                return typedParent.getType();
            }
        } else {
            throw new IllegalArgumentException(literal.toString());
        }
    } else {
        return literal.getType();
    }
}
Also used : CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtTypedElement(spoon.reflect.declaration.CtTypedElement) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 2 with CtAbstractInvocation

use of spoon.reflect.code.CtAbstractInvocation in project spoon by INRIA.

the class ExecutableRefTest method getInvocationFromMethod.

private CtAbstractInvocation<?> getInvocationFromMethod(String methodName) throws Exception {
    Factory factory = build(ExecutableRefTestSource.class, MyIntf.class);
    CtClass<ExecutableRefTestSource> clazz = factory.Class().get(ExecutableRefTestSource.class);
    Assert.assertNotNull(clazz);
    List<CtMethod<?>> methods = clazz.getMethodsByName(methodName);
    assertEquals(1, methods.size());
    CtMethod<?> ctMethod = methods.get(0);
    CtBlock<?> ctBody = (CtBlock<?>) ctMethod.getBody();
    Assert.assertNotNull(ctBody);
    List<CtStatement> ctStatements = ctBody.getStatements();
    assertEquals(1, ctStatements.size());
    CtStatement ctStatement = ctStatements.get(0);
    Assert.assertTrue(ctStatement instanceof CtAbstractInvocation<?>);
    return (CtAbstractInvocation<?>) ctStatement;
}
Also used : CtBlock(spoon.reflect.code.CtBlock) CtStatement(spoon.reflect.code.CtStatement) ExecutableRefTestSource(spoon.test.executable.testclasses.ExecutableRefTestSource) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod)

Example 3 with CtAbstractInvocation

use of spoon.reflect.code.CtAbstractInvocation in project spoon by INRIA.

the class VariableReferencesTest method getLiteralValue.

private Integer getLiteralValue(CtVariable<?> var) {
    CtExpression<?> exp = var.getDefaultExpression();
    if (exp != null) {
        try {
            return getLiteralValue(exp);
        } catch (ClassCastException e) {
        }
    }
    if (var instanceof CtParameter) {
        CtParameter param = (CtParameter) var;
        CtExecutable<?> l_exec = param.getParent(CtExecutable.class);
        int l_argIdx = l_exec.getParameters().indexOf(param);
        assertTrue(l_argIdx >= 0);
        if (l_exec instanceof CtLambda) {
            CtLambda<?> lambda = (CtLambda<?>) l_exec;
            CtLocalVariable<?> lamVar = (CtLocalVariable) lambda.getParent();
            CtLocalVariableReference<?> lamVarRef = lamVar.getParent().filterChildren((CtLocalVariableReference ref) -> ref.getSimpleName().equals(lamVar.getSimpleName())).first();
            CtAbstractInvocation inv = lamVarRef.getParent(CtAbstractInvocation.class);
            return getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
        } else {
            CtExecutableReference<?> l_execRef = l_exec.getReference();
            List<CtAbstractInvocation<?>> list = l_exec.getFactory().Package().getRootPackage().filterChildren((CtAbstractInvocation inv) -> {
                // return inv.getExecutable().equals(l_execRef);
                return inv.getExecutable().getExecutableDeclaration() == l_exec;
            }).list();
            CtAbstractInvocation inv = list.get(0);
            Integer firstValue = getLiteralValue((CtExpression<?>) inv.getArguments().get(l_argIdx));
            // check that all found method invocations are using same key
            list.forEach(inv2 -> {
                assertEquals(firstValue, getLiteralValue((CtExpression<?>) inv2.getArguments().get(l_argIdx)));
            });
            return firstValue;
        }
    }
    return getCommentValue(var);
}
Also used : CtLambda(spoon.reflect.code.CtLambda) CtExpression(spoon.reflect.code.CtExpression) CtAbstractInvocation(spoon.reflect.code.CtAbstractInvocation) CtParameter(spoon.reflect.declaration.CtParameter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference)

Example 4 with CtAbstractInvocation

use of spoon.reflect.code.CtAbstractInvocation 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

CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)4 CtMethod (spoon.reflect.declaration.CtMethod)2 CtParameter (spoon.reflect.declaration.CtParameter)2 Factory (spoon.reflect.factory.Factory)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 List (java.util.List)1 Map (java.util.Map)1 CtBlock (spoon.reflect.code.CtBlock)1 CtExpression (spoon.reflect.code.CtExpression)1 CtLambda (spoon.reflect.code.CtLambda)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtStatement (spoon.reflect.code.CtStatement)1 CtUnaryOperator (spoon.reflect.code.CtUnaryOperator)1 CtVariableAccess (spoon.reflect.code.CtVariableAccess)1 CtTypedElement (spoon.reflect.declaration.CtTypedElement)1 CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)1