Search in sources :

Example 1 with CtLocalVariableReference

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

the class VariableAccessTest method testReferences.

@Test
public void testReferences() throws Exception {
    /* test getReference on local variable
		*  getReference().getDeclaration() must be circular
		*/
    final CtType<Tortillas> aTortillas = buildClass(Tortillas.class);
    final CtMethod<Object> make = aTortillas.getMethod("make", aTortillas.getFactory().Type().stringType());
    final CtLocalVariable localVar = make.getBody().getStatement(0);
    final CtLocalVariable localVarCloned = localVar.clone();
    final CtLocalVariableReference localVarRef = localVar.getReference();
    final CtLocalVariableReference localVarRefCloned = localVarCloned.getReference();
    assertEquals(localVarRef.getDeclaration(), localVar);
    assertSame(localVarRef.getDeclaration(), localVar);
    assertEquals(localVar.getReference().getDeclaration(), localVar);
    assertSame(localVar.getReference().getDeclaration(), localVar);
    assertEquals(localVarRefCloned.getDeclaration(), localVarCloned);
    assertSame(localVarRefCloned.getDeclaration(), localVarCloned);
    assertEquals(localVarCloned.getReference().getDeclaration(), localVarCloned);
    assertSame(localVarCloned.getReference().getDeclaration(), localVarCloned);
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Tortillas(spoon.test.reference.testclasses.Tortillas) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 2 with CtLocalVariableReference

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

the class VariableAccessTest method testMultipleDeclarationsOfLocalVariable.

@Test
public void testMultipleDeclarationsOfLocalVariable() {
    final class CtLocalVariableReferenceScanner extends CtScanner {

        @Override
        public <T> void visitCtLocalVariableReference(final CtLocalVariableReference<T> reference) {
            assertNotNull(reference.getDeclaration());
            final CtLocalVariable decl = reference.getDeclaration();
            assertEquals(decl.getPosition().getLine(), 7);
            assertTrue(decl.getDefaultExpression() instanceof CtLiteral);
            final CtLiteral literal = (CtLiteral) decl.getDefaultExpression();
            assertEquals(literal.getValue(), 42);
            super.visitCtLocalVariableReference(reference);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/reference-test/MultipleDeclarationsOfLocalVariable.java");
    launcher.buildModel();
    new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}
Also used : CtLiteral(spoon.reflect.code.CtLiteral) CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtScanner(spoon.reflect.visitor.CtScanner) Test(org.junit.Test)

Example 3 with CtLocalVariableReference

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

the class VariableAccessTest method testGetDeclarationAfterClone.

@Test
public void testGetDeclarationAfterClone() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/noclasspath/A2.java");
    launcher.buildModel();
    final CtClass<Object> a2 = launcher.getFactory().Class().get("A2");
    final CtClass<Object> a2Cloned = a2.clone();
    assertEquals(a2, a2Cloned);
    final CtMethod<Object> methodA2 = getMethod(launcher, a2);
    final CtMethod<Object> methodA2Cloned = getMethod(launcher, a2Cloned);
    final CtLocalVariable declaration = methodA2.getBody().getStatement(0);
    final CtLocalVariable declarationCloned = methodA2Cloned.getBody().getStatement(0);
    final CtLocalVariableReference localVarRef = getLocalVariableRefF1(methodA2);
    final CtLocalVariableReference localVarRefCloned = getLocalVariableRefF1(methodA2Cloned);
    assertEquals(localVarRef.getDeclaration(), declaration);
    assertSame(localVarRef.getDeclaration(), declaration);
    assertEquals(localVarRefCloned.getDeclaration(), declarationCloned);
    assertSame(localVarRefCloned.getDeclaration(), declarationCloned);
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 4 with CtLocalVariableReference

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

the class VariableAccessTest method testReferencesInInitExpression.

@Test
public void testReferencesInInitExpression() throws Exception {
    /* test getReference on local variable
		*  getReference().getDeclaration() must be circular
		*/
    final CtType<Tortillas> aTortillas = buildClass(Tortillas.class);
    final CtMethod<Object> make = aTortillas.getMethod("make", aTortillas.getFactory().Type().stringType());
    final CtLocalVariable localVarNumber = make.getBody().getStatement(1);
    List<CtLocalVariableReference<?>> refs = localVarNumber.map(new LocalVariableReferenceFunction()).list();
    assertEquals(1, refs.size());
    assertSame(localVarNumber, refs.get(0).getParent(CtLocalVariable.class));
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Tortillas(spoon.test.reference.testclasses.Tortillas) CtLocalVariable(spoon.reflect.code.CtLocalVariable) LocalVariableReferenceFunction(spoon.reflect.visitor.filter.LocalVariableReferenceFunction) Test(org.junit.Test)

Example 5 with CtLocalVariableReference

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

the class VariableAccessTest method testReferenceToLocalVariableDeclaredInLoop.

@Test
public void testReferenceToLocalVariableDeclaredInLoop() {
    final class CtLocalVariableReferenceScanner extends CtScanner {

        @Override
        public <T> void visitCtLocalVariableReference(final CtLocalVariableReference<T> reference) {
            assertNotNull(reference.getDeclaration());
            assertEquals(reference.getDeclaration().getSimpleName(), reference.getSimpleName());
            assertEquals(reference.getDeclaration().getType(), reference.getType());
            super.visitCtLocalVariableReference(reference);
        }
    }
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("src/test/resources/reference-test/ChangeScanner.java");
    launcher.buildModel();
    new CtLocalVariableReferenceScanner().scan(launcher.getModel().getRootPackage());
}
Also used : CtLocalVariableReference(spoon.reflect.reference.CtLocalVariableReference) Launcher(spoon.Launcher) CtScanner(spoon.reflect.visitor.CtScanner) 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