Search in sources :

Example 16 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.

the class AnnotationTest method testAnnotationParameterTypes.

@Test
public void testAnnotationParameterTypes() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
    CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
    List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> a = annotations.get(0);
    AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
    assertEquals(42, annot.integer());
    assertEquals(1, annot.integers().length);
    assertEquals(42, annot.integers()[0]);
    assertEquals("Hello World!", annot.string());
    assertEquals(2, annot.strings().length);
    assertEquals("Hello", annot.strings()[0]);
    assertEquals("World", annot.strings()[1]);
    assertEquals(Integer.class, annot.clazz());
    assertEquals(2, annot.classes().length);
    assertEquals(Integer.class, annot.classes()[0]);
    assertEquals(String.class, annot.classes()[1]);
    assertEquals(true, annot.b());
    assertEquals('c', annot.c());
    assertEquals(42, annot.byt());
    assertEquals((short) 42, annot.s());
    assertEquals(42, annot.l());
    assertEquals(3.14f, annot.f(), 0f);
    assertEquals(3.14159, annot.d(), 0);
    assertEquals(AnnotParamTypeEnum.G, annot.e());
    assertEquals("dd", annot.ia().value());
    CtMethod<?> m2 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m2")).get(0);
    annotations = m2.getAnnotations();
    assertEquals(1, annotations.size());
    a = annotations.get(0);
    annot = (AnnotParamTypes) a.getActualAnnotation();
    assertEquals(42, annot.integer());
    assertEquals(1, annot.integers().length);
    assertEquals(42, annot.integers()[0]);
    assertEquals("Hello World!", annot.string());
    assertEquals(2, annot.strings().length);
    assertEquals("Hello", annot.strings()[0]);
    assertEquals("world", annot.strings()[1]);
    assertEquals(false, annot.b());
    assertEquals(42, annot.byt());
    assertEquals((short) 42, annot.s());
    assertEquals(42, annot.l());
    assertEquals(3.14f, annot.f(), 0f);
    assertEquals(3.14159, annot.d(), 0);
    assertEquals(AnnotParamTypeEnum.G, annot.e());
    assertEquals("dd", annot.ia().value());
    // tests binary expressions
    CtMethod<?> m3 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m3")).get(0);
    annotations = m3.getAnnotations();
    assertEquals(1, annotations.size());
    a = annotations.get(0);
    annot = (AnnotParamTypes) a.getActualAnnotation();
    assertEquals(45, annot.integer());
    assertEquals(2, annot.integers().length);
    assertEquals(40, annot.integers()[0]);
    assertEquals(42 * 3, annot.integers()[1]);
    assertEquals("Hello World!concatenated", annot.string());
    assertEquals(2, annot.strings().length);
    assertEquals("Helloconcatenated", annot.strings()[0]);
    assertEquals("worldconcatenated", annot.strings()[1]);
    assertEquals(true, annot.b());
    assertEquals(42 ^ 1, annot.byt());
    assertEquals((short) 42 / 2, annot.s());
    assertEquals(43, annot.l());
    assertEquals(3.14f * 2f, annot.f(), 0f);
    assertEquals(3.14159d / 3d, annot.d(), 0);
    assertEquals(AnnotParamTypeEnum.G, annot.e());
    assertEquals("dddd", annot.ia().value());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotParamTypes(spoon.test.annotation.testclasses.AnnotParamTypes) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) 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 17 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.

the class AnnotationTest method testRepeatSameAnnotationOnField.

@Test
public void testRepeatSameAnnotationOnField() 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 CtField<?> field = ctClass.getField("field");
    final List<CtAnnotation<? extends Annotation>> annotations = field.getAnnotations();
    assertEquals("Field 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 \"Field 1\"", "Field 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Field 2\"", "Field 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) AnnotationsRepeated(spoon.test.annotation.testclasses.AnnotationsRepeated) 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 18 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.

the class AnnotationTest method testAnnotationValueReflection.

@Test
public void testAnnotationValueReflection() throws Exception {
    Factory factory = new Launcher().getFactory();
    CtTypeReference reference = factory.createCtTypeReference(PropertyGetter.class);
    CtAnnotation annotation = factory.Interface().get(CtNamedElement.class).getMethod("getSimpleName").getAnnotation(reference);
    assertEquals("The annotation must have a value", 1, annotation.getValues().size());
    assertEquals("NAME", ((CtFieldRead) annotation.getValue("role")).getVariable().getSimpleName());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtFieldRead(spoon.reflect.code.CtFieldRead) CtTypeReference(spoon.reflect.reference.CtTypeReference) Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 19 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.

the class AnnotationTest method testAnnotationTypeAndFieldOnField.

@Test
public void testAnnotationTypeAndFieldOnField() throws IOException {
    // contract: annotation on field with an annotation type which supports type and field, should be attached both on type and field
    // see: https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.7.3
    // in this case, we want to print it only once before the type
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/typeandfield");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.setSourceOutputDirectory("./target/spooned-typeandfield");
    launcher.run();
    CtType type = launcher.getFactory().Type().get(SimpleClass.class);
    CtField field = type.getField("mandatoryField");
    assertEquals(1, field.getAnnotations().size());
    CtAnnotation annotation = field.getAnnotations().get(0);
    assertEquals("spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField", annotation.getAnnotationType().getQualifiedName());
    CtTypeReference fieldType = field.getType();
    assertEquals(1, fieldType.getAnnotations().size());
    CtAnnotation anotherAnnotation = fieldType.getAnnotations().get(0);
    assertEquals(annotation, anotherAnnotation);
    assertEquals("java.lang.String", field.getType().getQualifiedName());
    assertEquals(1, field.getType().getAnnotations().size());
    List<String> lines = Files.readAllLines(new File("./target/spooned-typeandfield/spoon/test/annotation/testclasses/typeandfield/SimpleClass.java").toPath());
    String fileContent = StringUtils.join(lines, "\n");
    assertTrue("Content :" + fileContent, fileContent.contains("@spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField"));
    assertTrue("Content :" + fileContent, fileContent.contains("public java.lang.String mandatoryField;"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 20 with CtAnnotation

use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.

the class AnnotationTest method testSpoonSpoonResult.

@Test
public void testSpoonSpoonResult() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
    String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
    spoon.setSourceOutputDirectory(output);
    Factory factory = spoon.getFactory();
    spoon.run();
    Launcher spoon2 = new Launcher();
    spoon2.addInputResource(output + "/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
    // spoon2.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PortRange.java");
    spoon2.buildModel();
    List<CtMethod<?>> methods = spoon2.getModel().getElements(new NamedElementFilter(CtMethod.class, "getPort"));
    assertEquals("Number of method getPort should be 1", 1, methods.size());
    CtMethod getport = methods.get(0);
    CtTypeReference returnType = getport.getType();
    List<CtAnnotation<?>> annotations = returnType.getAnnotations();
    assertEquals("Number of annotation for return type of method getPort should be 1", 1, annotations.size());
    CtAnnotation annotation = annotations.get(0);
    assertEquals("Annotation should be @spoon.test.annotation.testclasses.PortRange", "spoon.test.annotation.testclasses.PortRange", annotation.getAnnotationType().getQualifiedName());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtAnnotation (spoon.reflect.declaration.CtAnnotation)43 Test (org.junit.Test)37 Launcher (spoon.Launcher)35 Factory (spoon.reflect.factory.Factory)30 Annotation (java.lang.annotation.Annotation)20 CtClass (spoon.reflect.declaration.CtClass)19 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)19 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)19 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)18 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)18 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)18 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)18 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)18 CtMethod (spoon.reflect.declaration.CtMethod)12 CtTypeReference (spoon.reflect.reference.CtTypeReference)8 CtType (spoon.reflect.declaration.CtType)6 AnnotationsRepeated (spoon.test.annotation.testclasses.AnnotationsRepeated)6 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)4 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)4 SpoonException (spoon.SpoonException)3