Search in sources :

Example 1 with TypeAnnotation

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

the class AnnotationTest method testUsageOfTypeAnnotationInExtendsImplementsOfAClass.

@Test
public void testUsageOfTypeAnnotationInExtendsImplementsOfAClass() 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 CtClass<?> innerClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyClass")).get(0);
    final CtTypeReference<?> extendsActual = innerClass.getSuperclass();
    final List<CtAnnotation<? extends Annotation>> extendsTypeAnnotations = extendsActual.getAnnotations();
    final String superClassExpected = "spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "AnnotArrayInnerClass";
    assertEquals("Extends with a type annotation must have it in its model", 1, extendsTypeAnnotations.size());
    assertEquals("Type annotation on a extends must be typed by TypeAnnotation", TypeAnnotation.class, extendsTypeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, extendsTypeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Extends with an type annotation must be well printed", superClassExpected, extendsActual.toString());
    final Set<CtTypeReference<?>> superInterfaces = innerClass.getSuperInterfaces();
    final CtTypeReference<?> firstSuperInterface = superInterfaces.toArray(new CtTypeReference<?>[0])[0];
    final List<CtAnnotation<? extends Annotation>> implementsTypeAnnotations = firstSuperInterface.getAnnotations();
    final String superInterfaceExpected = "spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation";
    assertEquals("Implements with a type annotation must have it in its model", 1, implementsTypeAnnotations.size());
    assertEquals("Type annotation on a extends must be typed by TypeAnnotation", TypeAnnotation.class, implementsTypeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, implementsTypeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Extends with an type annotation must be well printed", superInterfaceExpected, firstSuperInterface.toString());
    final CtEnum<?> enumActual = ctClass.getElements(new NamedElementFilter<>(CtEnum.class, "DummyEnum")).get(0);
    final Set<CtTypeReference<?>> superInterfacesOfEnum = enumActual.getSuperInterfaces();
    final CtTypeReference<?> firstSuperInterfaceOfEnum = superInterfacesOfEnum.toArray(new CtTypeReference<?>[0])[0];
    final List<CtAnnotation<? extends Annotation>> enumTypeAnnotations = firstSuperInterfaceOfEnum.getAnnotations();
    final String enumExpected = "public enum DummyEnum implements spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation {" + System.lineSeparator() + "    ;" + System.lineSeparator() + "}";
    assertEquals("Implements in a enum with a type annotation must have it in its model", 1, enumTypeAnnotations.size());
    assertEquals("Type annotation on a implements in a enum must be typed by TypeAnnotation", TypeAnnotation.class, enumTypeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, enumTypeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Implements in a enum with an type annotation must be well printed", enumExpected, enumActual.toString());
    final CtInterface<?> interfaceActual = ctClass.getElements(new NamedElementFilter<>(CtInterface.class, "DummyInterface")).get(0);
    final Set<CtTypeReference<?>> superInterfacesOfInterface = interfaceActual.getSuperInterfaces();
    final CtTypeReference<?> firstSuperInterfaceOfInterface = superInterfacesOfInterface.toArray(new CtTypeReference<?>[0])[0];
    final List<CtAnnotation<? extends Annotation>> interfaceTypeAnnotations = firstSuperInterfaceOfInterface.getAnnotations();
    final String interfaceExpected = "public interface DummyInterface extends spoon.test.annotation.testclasses.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "BasicAnnotation {}";
    assertEquals("Implements in a interface with a type annotation must have it in its model", 1, interfaceTypeAnnotations.size());
    assertEquals("Type annotation on a implements in a enum must be typed by TypeAnnotation", TypeAnnotation.class, interfaceTypeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, interfaceTypeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Implements in a interface with an type annotation must be well printed", interfaceExpected, interfaceActual.toString());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) 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) CtClass(spoon.reflect.declaration.CtClass) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 2 with TypeAnnotation

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

the class AnnotationTest method testUsageOfTypeAnnotationBeforeExceptionInSignatureOfMethod.

@Test
public void testUsageOfTypeAnnotationBeforeExceptionInSignatureOfMethod() 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("m").get(0);
    final CtTypeReference<?> thrownReference = method.getThrownTypes().toArray(new CtTypeReference<?>[0])[0];
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = thrownReference.getAnnotations();
    assertEquals("Thrown type with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation with the thrown type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Thrown type with an type annotation must be well printed", "public void m() throws java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "Exception {" + System.lineSeparator() + "}", method.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) 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 3 with TypeAnnotation

use of spoon.test.annotation.testclasses.TypeAnnotation 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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtLocalVariable(spoon.reflect.code.CtLocalVariable) 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 4 with TypeAnnotation

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

Example 5 with TypeAnnotation

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

the class AnnotationTest method testUsageOfTypeAnnotationInCast.

@Test
public void testUsageOfTypeAnnotationInCast() 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 CtReturn<?> returns = ctClass.getElements(new AbstractFilter<CtReturn<?>>(CtReturn.class) {

        @Override
        public boolean matches(CtReturn<?> element) {
            return !element.getReturnedExpression().getTypeCasts().isEmpty();
        }
    }).get(0);
    final CtExpression<?> returnedExpression = returns.getReturnedExpression();
    final List<CtAnnotation<? extends Annotation>> typeAnnotations = returnedExpression.getTypeCasts().get(0).getAnnotations();
    assertEquals("Cast with a type annotation must have it in its model", 1, typeAnnotations.size());
    assertEquals("Type annotation in the cast must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
    assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
    assertEquals("Cast with an type annotation must be well printed", "((java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String) (s))", returnedExpression.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtReturn(spoon.reflect.code.CtReturn) 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