Search in sources :

Example 41 with CtTypeReference

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) TypeAnnotation(spoon.test.annotation.testclasses.TypeAnnotation) GlobalAnnotation(spoon.test.annotation.testclasses.GlobalAnnotation) SuperAnnotation(spoon.test.annotation.testclasses.SuperAnnotation) AnnotationDefaultAnnotation(spoon.test.annotation.testclasses.AnnotationDefaultAnnotation) InnerAnnotation(spoon.test.annotation.testclasses.Foo.InnerAnnotation) Annotation(java.lang.annotation.Annotation) MiddleAnnotation(spoon.test.annotation.testclasses.Foo.MiddleAnnotation) OuterAnnotation(spoon.test.annotation.testclasses.Foo.OuterAnnotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation) Test(org.junit.Test)

Example 42 with CtTypeReference

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());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtFieldRead(spoon.reflect.code.CtFieldRead) CtTypeReference(spoon.reflect.reference.CtTypeReference) Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 43 with CtTypeReference

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;"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) File(java.io.File) Test(org.junit.Test)

Example 44 with CtTypeReference

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());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 45 with CtTypeReference

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());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9