Search in sources :

Example 6 with CtLocalVariableReference

use of spoon.reflect.reference.CtLocalVariableReference 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 7 with CtLocalVariableReference

use of spoon.reflect.reference.CtLocalVariableReference in project spoon by INRIA.

the class VariableReferencesTest method testPotentialVariableAccessFromStaticMethod.

@Test
public void testPotentialVariableAccessFromStaticMethod() throws Exception {
    Factory factory = ModelUtils.build(VariableReferencesFromStaticMethod.class);
    CtClass<?> clazz = factory.Class().get(VariableReferencesFromStaticMethod.class);
    CtMethod staticMethod = clazz.getMethodsByName("staticMethod").get(0);
    CtStatement stmt = staticMethod.getBody().getStatements().get(1);
    assertEquals("org.junit.Assert.assertTrue((field == 2))", stmt.toString());
    CtLocalVariableReference varRef = stmt.filterChildren(new TypeFilter<>(CtLocalVariableReference.class)).first();
    List<CtVariable> vars = varRef.map(new PotentialVariableDeclarationFunction()).list();
    assertEquals("Found unexpected variable declaration.", 1, vars.size());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) CtVariable(spoon.reflect.declaration.CtVariable) PotentialVariableDeclarationFunction(spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod) VariableReferencesModelTest(spoon.test.query_function.testclasses.VariableReferencesModelTest) Test(org.junit.Test)

Aggregations

CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)7 Test (org.junit.Test)6 CtLocalVariable (spoon.reflect.code.CtLocalVariable)5 Launcher (spoon.Launcher)3 CtScanner (spoon.reflect.visitor.CtScanner)2 Tortillas (spoon.test.reference.testclasses.Tortillas)2 CtAbstractInvocation (spoon.reflect.code.CtAbstractInvocation)1 CtExpression (spoon.reflect.code.CtExpression)1 CtLambda (spoon.reflect.code.CtLambda)1 CtLiteral (spoon.reflect.code.CtLiteral)1 CtStatement (spoon.reflect.code.CtStatement)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtParameter (spoon.reflect.declaration.CtParameter)1 CtVariable (spoon.reflect.declaration.CtVariable)1 Factory (spoon.reflect.factory.Factory)1 LocalVariableReferenceFunction (spoon.reflect.visitor.filter.LocalVariableReferenceFunction)1 PotentialVariableDeclarationFunction (spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction)1 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)1 VariableReferencesModelTest (spoon.test.query_function.testclasses.VariableReferencesModelTest)1