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());
}
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());
}
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());
}
}
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\")"));
}
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());
}
Aggregations