use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnClass.
@Test
public void testRepeatSameAnnotationOnClass() 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 List<CtAnnotation<? extends Annotation>> annotations = ctClass.getAnnotations();
assertEquals("Class 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 \"First\"", "First", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Second\"", "Second", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationOnParameterInMethod.
@Test
public void testUsageOfTypeAnnotationOnParameterInMethod() 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 CtParameter<?> ctParameter = method.getParameters().get(0);
final List<CtAnnotation<? extends Annotation>> typeAnnotations = ctParameter.getType().getAnnotations();
assertEquals("Parameter type with a type annotation must have it in its model", 1, typeAnnotations.size());
assertEquals("Type annotation with the parameter type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Parameter type with an type annotation must be well printed", "java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "String param", ctParameter.toString());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testRepeatableAnnotationAreManagedWithArrays.
@Test
public void testRepeatableAnnotationAreManagedWithArrays() {
// contract: when two identical repeatable annotation with arrays are used, they should be displayed in two different annotations and not factorized
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
spoon.buildModel();
CtType type = spoon.getFactory().Type().get(RepeatedArrays.class);
CtMethod firstMethod = (CtMethod) type.getMethodsByName("method").get(0);
List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
assertEquals(2, annotations.size());
for (CtAnnotation a : annotations) {
assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
}
String classContent = type.toString();
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"machin\", \"truc\" })"));
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays({ \"truc\", \"bidule\" })"));
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testCreateRepeatableAnnotationWithArrays.
@Test
public void testCreateRepeatableAnnotationWithArrays() {
// contract: when using annotate with a repeatable annotation, it will create a new annotation, even if an annotation with an array already exists
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatandarrays");
spoon.buildModel();
CtType type = spoon.getFactory().Type().get(Repeated.class);
CtMethod firstMethod = (CtMethod) type.getMethodsByName("withoutAnnotation").get(0);
List<CtAnnotation<?>> annotations = firstMethod.getAnnotations();
assertTrue(annotations.isEmpty());
spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "foo");
assertEquals(1, firstMethod.getAnnotations().size());
spoon.getFactory().Annotation().annotate(firstMethod, TagArrays.class, "value", "bar");
annotations = firstMethod.getAnnotations();
assertEquals(2, annotations.size());
for (CtAnnotation a : annotations) {
assertEquals("TagArrays", a.getAnnotationType().getSimpleName());
}
String classContent = type.toString();
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"foo\")"));
assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatandarrays.TagArrays(\"bar\")"));
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method annotationOverrideFQNIsOK.
@Test
public void annotationOverrideFQNIsOK() {
Launcher spoon = new Launcher();
Factory factory = spoon.getFactory();
factory.getEnvironment().setNoClasspath(true);
spoon.addInputResource("./src/test/resources/noclasspath/annotation/issue1307/SpecIterator.java");
spoon.buildModel();
List<CtAnnotation> overrideAnnotations = factory.getModel().getElements(new TypeFilter<CtAnnotation>(CtAnnotation.class));
for (CtAnnotation annotation : overrideAnnotations) {
CtTypeReference typeRef = annotation.getAnnotationType();
if (typeRef.getSimpleName().equals("Override")) {
assertThat(typeRef.getQualifiedName(), is("java.lang.Override"));
}
}
}
Aggregations