Search in sources :

Example 6 with TypeAnnotation

use of spoon.test.annotation.testclasses.TypeAnnotation 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 7 with TypeAnnotation

use of spoon.test.annotation.testclasses.TypeAnnotation in project spoon by INRIA.

the class AnnotationTest method testUsageOfTypeAnnotationInNewInstance.

@Test
public void testUsageOfTypeAnnotationInNewInstance() 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 CtConstructorCall<?> ctConstructorCall = ctClass.getElements(new AbstractFilter<CtConstructorCall<?>>(CtConstructorCall.class) {

        @Override
        public boolean matches(CtConstructorCall<?> element) {
            return "String".equals(element.getType().getSimpleName());
        }
    }).get(0);
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctConstructorCall.getType().getAnnotations();
    assertEquals("Type of the new class must use an annotation", 1, typeAnnotations.size());
    assertEquals("Type of the new class is typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("New class with an type annotation must be well printed", "new java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String()", ctConstructorCall.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtConstructorCall(spoon.reflect.code.CtConstructorCall) 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

Annotation (java.lang.annotation.Annotation)7 Test (org.junit.Test)7 Launcher (spoon.Launcher)7 CtAnnotation (spoon.reflect.declaration.CtAnnotation)7 CtClass (spoon.reflect.declaration.CtClass)7 Factory (spoon.reflect.factory.Factory)7 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)7 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)7 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)7 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)7 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)7 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)7 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)7 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)3 CtTypeReference (spoon.reflect.reference.CtTypeReference)2 AnnotationsAppliedOnAnyTypeInAClass (spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass)2 CtConstructorCall (spoon.reflect.code.CtConstructorCall)1 CtLocalVariable (spoon.reflect.code.CtLocalVariable)1 CtReturn (spoon.reflect.code.CtReturn)1 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)1