use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class AnnotationTest method testGetAnnotationOuter.
@Test
public void testGetAnnotationOuter() 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);
Foo.OuterAnnotation annot = testMethod.getAnnotation(Foo.OuterAnnotation.class);
assertNotNull(annot);
assertEquals(2, annot.value().length);
}
use of spoon.reflect.declaration.CtClass 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());
}
use of spoon.reflect.declaration.CtClass in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration.
@Test
public void testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration() 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<?> genericClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyGenericClass")).get(0);
// New type parameter declaration.
final List<CtTypeParameter> typeParameters = genericClass.getFormalCtTypeParameters();
assertEquals("Generic class has 2 generics parameters.", 2, typeParameters.size());
assertEquals("First generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
assertEquals("Second generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "K", typeParameters.get(1).toString());
final CtTypeReference<?> superInterface = genericClass.getSuperInterfaces().toArray(new CtTypeReference<?>[0])[0];
final String expected = "spoon.test.annotation.testclasses.BasicAnnotation<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T>";
assertEquals("Super interface has a generic type with type annotation", expected, superInterface.toString());
}
use of spoon.reflect.declaration.CtClass 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());
}
use of spoon.reflect.declaration.CtClass 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());
}
Aggregations