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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations