use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.
the class AnnotationTest method testUsageOfTypeAnnotationBeforeExceptionInSignatureOfMethod.
@Test
public void testUsageOfTypeAnnotationBeforeExceptionInSignatureOfMethod() 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("m").get(0);
final CtTypeReference<?> thrownReference = method.getThrownTypes().toArray(new CtTypeReference<?>[0])[0];
final List<CtAnnotation<? extends Annotation>> typeAnnotations = thrownReference.getAnnotations();
assertEquals("Thrown type with a type annotation must have it in its model", 1, typeAnnotations.size());
assertEquals("Type annotation with the thrown type must be typed by TypeAnnotation", TypeAnnotation.class, typeAnnotations.get(0).getAnnotationType().getActualClass());
assertEquals(CtAnnotatedElementType.TYPE_USE, typeAnnotations.get(0).getAnnotatedElementType());
assertEquals("Thrown type with an type annotation must be well printed", "public void m() throws java.lang.@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "Exception {" + System.lineSeparator() + "}", method.toString());
}
use of spoon.reflect.reference.CtTypeReference 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.reference.CtTypeReference 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.reference.CtTypeReference 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());
}
use of spoon.reflect.reference.CtTypeReference 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());
}
Aggregations