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());
}
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());
}
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());
}
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);
}
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());
}
Aggregations