use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnMethod.
@Test
public void testRepeatSameAnnotationOnMethod() 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("method").get(0);
final List<CtAnnotation<? extends Annotation>> annotations = method.getAnnotations();
assertEquals("Method 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 \"Method 1\"", "Method 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Method 2\"", "Method 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
use of spoon.reflect.declaration.CtAnnotation 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());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class ParentExiter method substituteAnnotation.
private void substituteAnnotation(CtTypedElement ele) {
if (annotationsMap.containsKey(ele)) {
List<CtAnnotation> annotations = annotationsMap.get(ele);
for (CtAnnotation annotation : annotations) {
// if we are here, we may have find an element for whom it's a better place
if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() && annotation.isParentInitialized()) {
CtElement parent = annotation.getParent();
parent.removeAnnotation(annotation);
}
if (!ele.getType().getAnnotations().contains(annotation)) {
ele.getType().addAnnotation(annotation.clone());
}
}
annotationsMap.remove(ele);
}
}
Aggregations