Search in sources :

Example 36 with CtAnnotation

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

the class AnnotationTest method testRepeatSameAnnotationOnClass.

@Test
public void testRepeatSameAnnotationOnClass() 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 List<CtAnnotation<? extends Annotation>> annotations = ctClass.getAnnotations();
    assertEquals("Class 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 \"First\"", "First", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Second\"", "Second", ((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 37 with CtAnnotation

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

the class AnnotationTest method testUsageOfTypeAnnotationOnParameterInMethod.

@Test
public void testUsageOfTypeAnnotationOnParameterInMethod() 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 CtParameter<?> ctParameter = method.getParameters().get(0);
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctParameter.getType().getAnnotations();
    assertEquals("Parameter type with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation with the parameter type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Parameter type with an type annotation must be well printed", "java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String param", ctParameter.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) 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) AnnotationsAppliedOnAnyTypeInAClass(spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass) Test(org.junit.Test)

Example 38 with CtAnnotation

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

the class AnnotationTest method testRepeatableAnnotationAreManagedWithArrays.

@Test
public void testRepeatableAnnotationAreManagedWithArrays() {
    // contract: when two identical repeatable annotation with arrays are used, they should be displayed in two different annotations and not factorized
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(RepeatedArrays.class);
    CtMethod firstMethod = (CtMethod) type.getMethodsByName("method").get(0);
    List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"machin\", \"truc\" })"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"truc\", \"bidule\" })"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 39 with CtAnnotation

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

the class AnnotationTest method testCreateRepeatableAnnotationWithArrays.

@Test
public void testCreateRepeatableAnnotationWithArrays() {
    // contract: when using annotate with a repeatable annotation, it will create a new annotation, even if an annotation with an array already exists
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
    spoon.buildModel();
    CtType type = spoon.getFactory().Type().get(Repeated.class);
    CtMethod firstMethod = (CtMethod) type.getMethodsByName("withoutAnnotation").get(0);
    List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
    assertTrue(annotations.isEmpty());
    spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "foo");
    assertEquals(1, firstMethod.getAnnotations().size());
    spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "bar");
    annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"foo\")"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"bar\")"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 40 with CtAnnotation

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

the class AnnotationTest method annotationOverrideFQNIsOK.

@Test
public void annotationOverrideFQNIsOK() {
    Launcher spoon = new Launcher();
    Factory factory = spoon.getFactory();
    factory.getEnvironment().setNoClasspath(true);
    spoon.addInputResource("./src/test/resources/noclasspath/annotation/issue1307/SpecIterator.java");
    spoon.buildModel();
    List<CtAnnotation> overrideAnnotations = factory.getModel().getElements(new TypeFilter<CtAnnotation>(CtAnnotation.class));
    for (CtAnnotation annotation : overrideAnnotations) {
        CtTypeReference typeRef = annotation.getAnnotationType();
        if (typeRef.getSimpleName().equals("Override")) {
            assertThat(typeRef.getQualifiedName(), is("java.lang.Override"));
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) 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