use of spoon.reflect.declaration.CtEnumValue 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());
}
Aggregations