Search in sources :

Example 6 with CtLocalVariable

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

the class ImportTest method testImportStaticAndFieldAccessWithImport.

@Test
public void testImportStaticAndFieldAccessWithImport() throws Exception {
    // contract: Qualified field access and an import static with import should import the type first, and not use static import
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput", "--with-imports" });
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/internal4/");
    launcher.addInputResource("./src/test/java/spoon/test/imports/testclasses/Tacos.java");
    launcher.buildModel();
    final CtType<Object> aTacos = launcher.getFactory().Type().get(Tacos.class);
    final CtStatement assignment = aTacos.getMethod("m").getBody().getStatement(0);
    assertTrue(assignment instanceof CtLocalVariable);
    assertEquals("Constants.CONSTANT.foo", ((CtLocalVariable) assignment).getAssignment().toString());
}
Also used : CtStatement(spoon.reflect.code.CtStatement) Launcher(spoon.Launcher) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 7 with CtLocalVariable

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

the class AnnotationTest method testRepeatSameAnnotationOnLocalVariable.

@Test
public void testRepeatSameAnnotationOnLocalVariable() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsRepeated.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsRepeated.class);
    final CtMethod<?> method = ctClass.getMethodsByName("methodWithLocalVariable").get(0);
    final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {

        @Override
        public boolean matches(CtLocalVariable<?> element) {
            return true;
        }
    }).get(0);
    final List<CtAnnotation<? extends Annotation>> annotations = ctLocalVariable.getAnnotations();
    assertEquals("Local variable must to have multi annotation of the same type", 2, annotations.size());
    assertEquals("Type of the first annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(0).getAnnotationType().getActualClass());
    assertEquals("Type of the second annotation is AnnotationRepeated", AnnotationRepeated.class, annotations.get(1).getAnnotationType().getActualClass());
    assertEquals("Argument of the first annotation is \"Local 1\"", "Local 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Local 2\"", "Local 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AnnotationsRepeated(spoon.test.annotation.testclasses.AnnotationsRepeated) CtLocalVariable(spoon.reflect.code.CtLocalVariable) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 8 with CtLocalVariable

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

the class AnnotationTest method testUsageOfTypeAnnotationOnLocalVariableInMethod.

@Test
public void testUsageOfTypeAnnotationOnLocalVariableInMethod() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get(AnnotationsAppliedOnAnyTypeInAClass.class);
    final CtMethod<?> method = ctClass.getMethodsByName("m6").get(0);
    final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {

        @Override
        public boolean matches(CtLocalVariable<?> element) {
            return true;
        }
    }).get(0);
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctLocalVariable.getType().getAnnotations();
    assertEquals("Local variable type with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation with the local variable type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Local variable type with an type annotation must be well printed", "java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String s = \"\"", ctLocalVariable.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotationsAppliedOnAnyTypeInAClass(spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass) Test(org.junit.Test)

Example 9 with CtLocalVariable

use of spoon.reflect.code.CtLocalVariable 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 10 with CtLocalVariable

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

Aggregations

CtLocalVariable (spoon.reflect.code.CtLocalVariable)29 Test (org.junit.Test)21 Factory (spoon.reflect.factory.Factory)15 Launcher (spoon.Launcher)10 CtParameter (spoon.reflect.declaration.CtParameter)7 CtTypeReference (spoon.reflect.reference.CtTypeReference)7 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)7 CtStatement (spoon.reflect.code.CtStatement)6 CtClass (spoon.reflect.declaration.CtClass)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtElement (spoon.reflect.declaration.CtElement)5 CtLocalVariableReference (spoon.reflect.reference.CtLocalVariableReference)5 List (java.util.List)4 CtComment (spoon.reflect.code.CtComment)4 CtTry (spoon.reflect.code.CtTry)4 CtField (spoon.reflect.declaration.CtField)4 CtType (spoon.reflect.declaration.CtType)4 CtAssignment (spoon.reflect.code.CtAssignment)3 CtCatchVariable (spoon.reflect.code.CtCatchVariable)3 CtExpression (spoon.reflect.code.CtExpression)3