Search in sources :

Example 11 with CtVariable

use of spoon.reflect.declaration.CtVariable 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

CtVariable (spoon.reflect.declaration.CtVariable)11 Test (org.junit.Test)7 Factory (spoon.reflect.factory.Factory)4 CtLocalVariable (spoon.reflect.code.CtLocalVariable)3 CtStatement (spoon.reflect.code.CtStatement)3 CtClass (spoon.reflect.declaration.CtClass)3 CtElement (spoon.reflect.declaration.CtElement)3 CtField (spoon.reflect.declaration.CtField)3 CtPackage (spoon.reflect.declaration.CtPackage)3 CtType (spoon.reflect.declaration.CtType)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 ArrayList (java.util.ArrayList)2 Launcher (spoon.Launcher)2 CtCatch (spoon.reflect.code.CtCatch)2 CtStatementList (spoon.reflect.code.CtStatementList)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtVariableReference (spoon.reflect.reference.CtVariableReference)2 PotentialVariableDeclarationFunction (spoon.reflect.visitor.filter.PotentialVariableDeclarationFunction)2 VariableReferencesModelTest (spoon.test.query_function.testclasses.VariableReferencesModelTest)2