Search in sources :

Example 51 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationTest method testAnnotationInterfacePreserveMethods.

@Test
public void testAnnotationInterfacePreserveMethods() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/PortRange.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtAnnotationType<?> ctAnnotationType = (CtAnnotationType) factory.Type().get(PortRange.class);
    List<CtMethod<?>> ctMethodMin = ctAnnotationType.getMethodsByName("min");
    assertEquals("Method min is preserved after transformation", 1, ctMethodMin.size());
    List<CtMethod<?>> ctMethodMax = ctAnnotationType.getMethodsByName("max");
    assertEquals("Method max is preserved after transformation", 1, ctMethodMax.size());
    List<CtMethod<?>> ctMethodMessage = ctAnnotationType.getMethodsByName("message");
    assertEquals("Method message is preserved after transformation", 1, ctMethodMessage.size());
    List<CtMethod<?>> ctMethodGroups = ctAnnotationType.getMethodsByName("groups");
    assertEquals("Method groups is preserved after transformation", 1, ctMethodGroups.size());
    List<CtMethod<?>> ctMethodPayload = ctAnnotationType.getMethodsByName("payload");
    assertEquals("Method payload is preserved after transformation", 1, ctMethodPayload.size());
}
Also used : PortRange(spoon.test.annotation.testclasses.PortRange) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtAnnotationType(spoon.reflect.declaration.CtAnnotationType) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 52 with CtMethod

use of spoon.reflect.declaration.CtMethod 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 53 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationTest method testAnnotationNotRepeatableNotArrayAnnotation.

@Test
public void testAnnotationNotRepeatableNotArrayAnnotation() {
    // contract: when trying to annotate multiple times with same not repeatable, not array annotation, it should throw an exception
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/notrepeatable/StringAnnot.java");
    spoon.buildModel();
    CtMethod aMethod = spoon.getFactory().createMethod().setSimpleName(("mamethod"));
    spoon.getFactory().Annotation().annotate(aMethod, StringAnnot.class, "value", "foo");
    assertEquals(1, aMethod.getAnnotations().size());
    String methodContent = aMethod.toString();
    assertTrue("Content: " + methodContent, methodContent.contains("@spoon.test.annotation.testclasses.notrepeatable.StringAnnot(\"foo\")"));
    try {
        spoon.getFactory().Annotation().annotate(aMethod, StringAnnot.class, "value", "bar");
        methodContent = aMethod.toString();
        fail("You should not be able to add two values to StringAnnot annotation: " + methodContent);
    } catch (SpoonException e) {
        assertEquals("cannot assign an array to a non-array annotation element", e.getMessage());
    }
}
Also used : SpoonException(spoon.SpoonException) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 54 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationTest method testCreateRepeatableAnnotation.

@Test
public void testCreateRepeatableAnnotation() {
    // contract: when creating two repeatable annotations, two annotations should be created
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/annotation/testclasses/repeatable");
    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, Tag.class, "value", "foo");
    assertEquals(1, firstMethod.getAnnotations().size());
    spoon.getFactory().Annotation().annotate(firstMethod, Tag.class, "value", "bar");
    annotations = firstMethod.getAnnotations();
    assertEquals(2, annotations.size());
    for (CtAnnotation a : annotations) {
        assertEquals("Tag", a.getAnnotationType().getSimpleName());
    }
    String classContent = type.toString();
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"foo\")"));
    assertTrue("Content of the file: " + classContent, classContent.contains("@spoon.test.annotation.testclasses.repeatable.Tag(\"bar\")"));
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 55 with CtMethod

use of spoon.reflect.declaration.CtMethod in project spoon by INRIA.

the class AnnotationTest method testAnnotatedElementTypes.

@Test
public void testAnnotatedElementTypes() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    // load package of the test classes
    CtPackage pkg = factory.Package().get("spoon.test.annotation.testclasses");
    // check annotated element type of the package annotation
    List<CtAnnotation<?>> annotations = pkg.getAnnotations();
    assertEquals(2, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(pkg));
    assertEquals(CtAnnotatedElementType.PACKAGE, annotations.get(0).getAnnotatedElementType());
    // load class Main from package and check annotated element type of the class annotation
    CtClass<?> clazz = pkg.getType("Main");
    assertEquals(Main.class, clazz.getActualClass());
    annotations = clazz.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(clazz));
    assertEquals(CtAnnotatedElementType.TYPE, clazz.getAnnotations().get(0).getAnnotatedElementType());
    // load method toString() from class and check annotated element type of the annotation
    List<CtMethod<?>> methods = clazz.getMethodsByName("toString");
    assertEquals(1, methods.size());
    CtMethod<?> method = methods.get(0);
    assertEquals("toString", method.getSimpleName());
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    // load parameter of method m(int) and check annotated element type of the parameter annotation
    methods = clazz.getMethodsByName("m");
    assertEquals(1, methods.size());
    method = methods.get(0);
    assertEquals("m", method.getSimpleName());
    List<CtParameter<?>> parameters = method.getParameters();
    assertEquals(1, parameters.size());
    CtParameter<?> parameter = parameters.get(0);
    annotations = parameter.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(parameter));
    assertEquals(CtAnnotatedElementType.PARAMETER, annotations.get(0).getAnnotatedElementType());
    // load constructor of the clazz and check annotated element type of the constructor annotation
    Set<? extends CtConstructor<?>> constructors = clazz.getConstructors();
    assertEquals(1, constructors.size());
    CtConstructor<?> constructor = constructors.iterator().next();
    annotations = constructor.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(constructor));
    assertEquals(CtAnnotatedElementType.CONSTRUCTOR, annotations.get(0).getAnnotatedElementType());
    // load value ia of the m1() method annotation, which is also an annotation
    // and check the annotated element type of the inner annotation.
    methods = clazz.getMethodsByName("m1");
    assertEquals(1, methods.size());
    method = methods.get(0);
    annotations = method.getAnnotations();
    assertEquals(1, annotations.size());
    CtAnnotation<?> annotation = annotations.get(0);
    assertTrue(annotations.get(0).getAnnotatedElement().equals(method));
    assertEquals(CtAnnotatedElementType.METHOD, annotations.get(0).getAnnotatedElementType());
    Object element = annotation.getValues().get("ia");
    assertNotNull(element);
    assertTrue(element instanceof CtAnnotation);
    assertTrue(((CtAnnotation<?>) element).getAnnotatedElement().equals(annotation));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, ((CtAnnotation<?>) element).getAnnotatedElementType());
    // load enum AnnotParamTypeEnum and check the annotated element type of the annotation of the enum and of the fields
    CtEnum<?> enumeration = pkg.getType("AnnotParamTypeEnum");
    assertEquals(AnnotParamTypeEnum.class, enumeration.getActualClass());
    annotations = enumeration.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(enumeration));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    List<CtEnumValue<?>> fields = enumeration.getEnumValues();
    assertEquals(3, fields.size());
    annotations = fields.get(0).getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(fields.get(0)));
    assertEquals(CtAnnotatedElementType.FIELD, annotations.get(0).getAnnotatedElementType());
    // load interface type TestInterface and check the annotated element type of the annotation
    CtInterface<?> ctInterface = pkg.getType("TestInterface");
    assertEquals(TestInterface.class, ctInterface.getActualClass());
    annotations = ctInterface.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(ctInterface));
    assertEquals(CtAnnotatedElementType.TYPE, annotations.get(0).getAnnotatedElementType());
    // load annotation type Bound and check the annotated element type of the annotations
    CtAnnotationType<?> annotationType = pkg.getType("Bound");
    assertEquals(Bound.class, annotationType.getActualClass());
    assertNull(annotationType.getSuperclass());
    assertEquals(1, annotationType.getMethods().size());
    assertEquals(0, annotationType.getSuperInterfaces().size());
    annotations = annotationType.getAnnotations();
    assertEquals(1, annotations.size());
    assertTrue(annotations.get(0).getAnnotatedElement().equals(annotationType));
    assertEquals(CtAnnotatedElementType.ANNOTATION_TYPE, annotations.get(0).getAnnotatedElementType());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtEnumValue(spoon.reflect.declaration.CtEnumValue) Factory(spoon.reflect.factory.Factory) CtParameter(spoon.reflect.declaration.CtParameter) Launcher(spoon.Launcher) CtPackage(spoon.reflect.declaration.CtPackage) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16