Search in sources :

Example 21 with CtAnnotation

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

the class AnnotationTest method testRepeatSameAnnotationOnParameter.

@Test
public void testRepeatSameAnnotationOnParameter() 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("methodWithParameter").get(0);
    final CtParameter<?> ctParameter = method.getParameters().get(0);
    final List<CtAnnotation<? extends Annotation>> annotations = ctParameter.getAnnotations();
    assertEquals("Parameter 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 \"Param 1\"", "Param 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
    assertEquals("Argument of the second annotation is \"Param 2\"", "Param 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 22 with CtAnnotation

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

the class AnnotationTest method testInnerAnnotationsWithArray.

@Test
public void testInnerAnnotationsWithArray() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Foo.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.Foo");
    final CtMethod<?> testMethod = ctClass.getMethodsByName("test").get(0);
    final List<CtAnnotation<? extends Annotation>> testMethodAnnotations = testMethod.getAnnotations();
    assertEquals(1, testMethodAnnotations.size());
    final CtAnnotation<? extends Annotation> firstAnnotation = testMethodAnnotations.get(0);
    assertEquals(OuterAnnotation.class, getActualClassFromAnnotation(firstAnnotation));
    final CtNewArray<?> arrayAnnotations = (CtNewArray<?>) firstAnnotation.getValues().get("value");
    assertEquals(2, arrayAnnotations.getElements().size());
    final CtAnnotation<?> firstAnnotationInArray = getMiddleAnnotation(arrayAnnotations, 0);
    assertEquals(MiddleAnnotation.class, getActualClassFromAnnotation(firstAnnotationInArray));
    final CtAnnotation<?> secondAnnotationInArray = getMiddleAnnotation(arrayAnnotations, 1);
    assertEquals(MiddleAnnotation.class, getActualClassFromAnnotation(secondAnnotationInArray));
    final CtAnnotation<?> innerAnnotationInFirstMiddleAnnotation = getInnerAnnotation(firstAnnotationInArray);
    assertEquals(InnerAnnotation.class, getActualClassFromAnnotation(innerAnnotationInFirstMiddleAnnotation));
    assertEquals("hello", getLiteralValueInAnnotation(innerAnnotationInFirstMiddleAnnotation).getValue());
    final CtAnnotation<?> innerAnnotationInSecondMiddleAnnotation = getInnerAnnotation(secondAnnotationInArray);
    assertEquals(InnerAnnotation.class, getActualClassFromAnnotation(innerAnnotationInSecondMiddleAnnotation));
    assertEquals("hello again", getLiteralValueInAnnotation(innerAnnotationInSecondMiddleAnnotation).getValue());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtNewArray(spoon.reflect.code.CtNewArray) 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 23 with CtAnnotation

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

the class AnnotationTest method testCreateRepeatableAnnotation.

@Test
public void testCreateRepeatableAnnotation() {
    // contract: when creating two repeatable annotations, two annotations should be created
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatable");
    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, Tag.class, "value", "foo");
    assertEquals(1, firstMethod.getAnnotations().size());
    spoon.getFactory().Annotation().annotate(firstMethod, Tag.class, "value", "bar");
    annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("Tag", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"foo\")"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"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 24 with CtAnnotation

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

the class AnnotationTest method testAnnotatedElementTypes.

@Test
public void testAnnotatedElementTypes() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    // load package of the test classes
    CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
    // check annotated element type of the package annotation
    List<CtAnnotation<?>> annotations = pkg.getAnnotations();
    assertEquals(2, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(pkg));
    assertEquals(CtAnnotatedElementType.PACKAGE, annotations.get(0).getAnnotatedElementType());
    // load class Main from package and check annotated element type of the class annotation
    CtClass<?> clazz = pkg.getType("Main");
    assertEquals(Main.class, clazz.getActualClass());
    annotations = clazz.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(clazz));
    assertEquals(CtAnnotatedElementType.TYPE, clazz.getAnnotations().get(0).getAnnotatedElementType());
    // load method toString() from class and check annotated element type of the annotation
    List<CtMethod<?>> methods = clazz.getMethodsByName("toString");
    assertEquals(1, methods.size());
    CtMethod<?> method = methods.get(0);
    assertEquals("toString", method.getSimpleName());
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    // load parameter of method m(int) and check annotated element type of the parameter annotation
    methods = clazz.getMethodsByName("m");
    assertEquals(1, methods.size());
    method = methods.get(0);
    assertEquals("m", method.getSimpleName());
    List<CtParameter<?>> parameters = method.getParameters();
    assertEquals(1, parameters.size());
    CtParameter<?> parameter = parameters.get(0);
    annotations = parameter.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(parameter));
    assertEquals(CtAnnotatedElementType.PARAMETER, annotations.get(0).getAnnotatedElementType());
    // load constructor of the clazz and check annotated element type of the constructor annotation
    Set<? extends CtConstructor<?>> constructors = clazz.getConstructors();
    assertEquals(1, constructors.size());
    CtConstructor<?> constructor = constructors.iterator().next();
    annotations = constructor.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(constructor));
    assertEquals(CtAnnotatedElementType.CONSTRUCTOR, annotations.get(0).getAnnotatedElementType());
    // load value ia of the m1() method annotation, which is also an annotation
    // and check the annotated element type of the inner annotation.
    methods = clazz.getMethodsByName("m1");
    assertEquals(1, methods.size());
    method = methods.get(0);
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> annotation = annotations.get(0);
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    Object element = annotation.getValues().get("ia");
    assertNotNull(element);
    assertTrue(element instanceof CtAnnotation);
    assertTrue(((CtAnnotation<?>) element).getAnnotatedElement().equals(annotation));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, ((CtAnnotation<?>) element).getAnnotatedElementType());
    // load enum AnnotParamTypeEnum and check the annotated element type of the annotation of the enum and of the fields
    CtEnum<?> enumeration = pkg.getType("AnnotParamTypeEnum");
    assertEquals(AnnotParamTypeEnum.class, enumeration.getActualClass());
    annotations = enumeration.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(enumeration));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    List<CtEnumValue<?>> fields = enumeration.getEnumValues();
    assertEquals(3, fields.size());
    annotations = fields.get(0).getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(fields.get(0)));
    assertEquals(CtAnnotatedElementType.FIELD, annotations.get(0).getAnnotatedElementType());
    // load interface type TestInterface and check the annotated element type of the annotation
    CtInterface<?> ctInterface = pkg.getType("TestInterface");
    assertEquals(TestInterface.class, ctInterface.getActualClass());
    annotations = ctInterface.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(ctInterface));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    // load annotation type Bound and check the annotated element type of the annotations
    CtAnnotationType<?> annotationType = pkg.getType("Bound");
    assertEquals(Bound.class, annotationType.getActualClass());
    assertNull(annotationType.getSuperclass());
    assertEquals(1, annotationType.getMethods().size());
    assertEquals(0, annotationType.getSuperInterfaces().size());
    annotations = annotationType.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(annotationType));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, annotations.get(0).getAnnotatedElementType());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtEnumValue(spoon.reflect.declaration.CtEnumValue) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 25 with CtAnnotation

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

the class AnnotationTest method testUsageOfTypeAnnotationInReturnTypeInMethod.

@Test
public void testUsageOfTypeAnnotationInReturnTypeInMethod() 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("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
    final CtMethod<?> method = ctClass.getMethodsByName("m3").get(0);
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = method.getType().getAnnotations();
    assertEquals("Return type with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation with the return type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Return type with an type annotation must be well printed", "public java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String m3() {" + System.lineSeparator() + "    return \"\";" + System.lineSeparator() + "}", method.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) 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