use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testAnnotationParameterTypes.
@Test
public void testAnnotationParameterTypes() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/Main.java");
launcher.buildModel();
Factory factory = launcher.getFactory();
CtType<?> type = factory.Type().get("spoon.test.annotation.testclasses.Main");
CtMethod<?> m1 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m1")).get(0);
List<CtAnnotation<? extends Annotation>> annotations = m1.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("World", annot.strings()[1]);
assertEquals(Integer.class, annot.clazz());
assertEquals(2, annot.classes().length);
assertEquals(Integer.class, annot.classes()[0]);
assertEquals(String.class, annot.classes()[1]);
assertEquals(true, annot.b());
assertEquals('c', annot.c());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
CtMethod<?> m2 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m2")).get(0);
annotations = m2.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("world", annot.strings()[1]);
assertEquals(false, annot.b());
assertEquals(42, annot.byt());
assertEquals((short) 42, annot.s());
assertEquals(42, annot.l());
assertEquals(3.14f, annot.f(), 0f);
assertEquals(3.14159, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dd", annot.ia().value());
// tests binary expressions
CtMethod<?> m3 = type.getElements(new NamedElementFilter<>(CtMethod.class, "m3")).get(0);
annotations = m3.getAnnotations();
assertEquals(1, annotations.size());
a = annotations.get(0);
annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(45, annot.integer());
assertEquals(2, annot.integers().length);
assertEquals(40, annot.integers()[0]);
assertEquals(42 * 3, annot.integers()[1]);
assertEquals("Hello World!concatenated", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Helloconcatenated", annot.strings()[0]);
assertEquals("worldconcatenated", annot.strings()[1]);
assertEquals(true, annot.b());
assertEquals(42 ^ 1, annot.byt());
assertEquals((short) 42 / 2, annot.s());
assertEquals(43, annot.l());
assertEquals(3.14f * 2f, annot.f(), 0f);
assertEquals(3.14159d / 3d, annot.d(), 0);
assertEquals(AnnotParamTypeEnum.G, annot.e());
assertEquals("dddd", annot.ia().value());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnField.
@Test
public void testRepeatSameAnnotationOnField() 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 CtField<?> field = ctClass.getField("field");
final List<CtAnnotation<? extends Annotation>> annotations = field.getAnnotations();
assertEquals("Field 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 \"Field 1\"", "Field 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Field 2\"", "Field 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testAnnotationValueReflection.
@Test
public void testAnnotationValueReflection() throws Exception {
Factory factory = new Launcher().getFactory();
CtTypeReference reference = factory.createCtTypeReference(PropertyGetter.class);
CtAnnotation annotation = factory.Interface().get(CtNamedElement.class).getMethod("getSimpleName").getAnnotation(reference);
assertEquals("The annotation must have a value", 1, annotation.getValues().size());
assertEquals("NAME", ((CtFieldRead) annotation.getValue("role")).getVariable().getSimpleName());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testAnnotationTypeAndFieldOnField.
@Test
public void testAnnotationTypeAndFieldOnField() throws IOException {
// contract: annotation on field with an annotation type which supports type and field, should be attached both on type and field
// see: https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.7.3
// in this case, we want to print it only once before the type
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/typeandfield");
launcher.getEnvironment().setNoClasspath(true);
launcher.setSourceOutputDirectory("./target/spooned-typeandfield");
launcher.run();
CtType type = launcher.getFactory().Type().get(SimpleClass.class);
CtField field = type.getField("mandatoryField");
assertEquals(1, field.getAnnotations().size());
CtAnnotation annotation = field.getAnnotations().get(0);
assertEquals("spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField", annotation.getAnnotationType().getQualifiedName());
CtTypeReference fieldType = field.getType();
assertEquals(1, fieldType.getAnnotations().size());
CtAnnotation anotherAnnotation = fieldType.getAnnotations().get(0);
assertEquals(annotation, anotherAnnotation);
assertEquals("java.lang.String", field.getType().getQualifiedName());
assertEquals(1, field.getType().getAnnotations().size());
List<String> lines = Files.readAllLines(new File("./target/spooned-typeandfield/spoon/test/annotation/testclasses/typeandfield/SimpleClass.java").toPath());
String fileContent = StringUtils.join(lines, "\n");
assertTrue("Content :" + fileContent, fileContent.contains("@spoon.test.annotation.testclasses.typeandfield.AnnotTypeAndField"));
assertTrue("Content :" + fileContent, fileContent.contains("public java.lang.String mandatoryField;"));
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testSpoonSpoonResult.
@Test
public void testSpoonSpoonResult() throws Exception {
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
spoon.setSourceOutputDirectory(output);
Factory factory = spoon.getFactory();
spoon.run();
Launcher spoon2 = new Launcher();
spoon2.addInputResource(output + "/spoon/test/annotation/testclasses/dropwizard/GraphiteReporterFactory.java");
// spoon2.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PortRange.java");
spoon2.buildModel();
List<CtMethod<?>> methods = spoon2.getModel().getElements(new NamedElementFilter(CtMethod.class, "getPort"));
assertEquals("Number of method getPort should be 1", 1, methods.size());
CtMethod getport = methods.get(0);
CtTypeReference returnType = getport.getType();
List<CtAnnotation<?>> annotations = returnType.getAnnotations();
assertEquals("Number of annotation for return type of method getPort should be 1", 1, annotations.size());
CtAnnotation annotation = annotations.get(0);
assertEquals("Annotation should be @spoon.test.annotation.testclasses.PortRange", "spoon.test.annotation.testclasses.PortRange", annotation.getAnnotationType().getQualifiedName());
}
Aggregations