use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class JavaReflectionTreeBuilder method visitAnnotation.
@Override
public void visitAnnotation(final Annotation annotation) {
final CtAnnotation<Annotation> ctAnnotation = factory.Core().createAnnotation();
enter(new AnnotationRuntimeBuilderContext(ctAnnotation) {
@Override
public void addMethod(CtMethod ctMethod) {
try {
Object value = annotation.annotationType().getMethod(ctMethod.getSimpleName()).invoke(annotation);
ctAnnotation.addValue(ctMethod.getSimpleName(), value);
} catch (Exception ignore) {
ctAnnotation.addValue(ctMethod.getSimpleName(), "");
}
}
});
super.visitAnnotation(annotation);
exit();
contexts.peek().addAnnotation(ctAnnotation);
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testModelBuildingAnnotationBoundUsage.
@Test
public void testModelBuildingAnnotationBoundUsage() 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");
assertEquals("Main", type.getSimpleName());
CtParameter<?> param = type.getElements(new TypeFilter<CtParameter<?>>(CtParameter.class)).get(0);
assertEquals("a", param.getSimpleName());
List<CtAnnotation<? extends Annotation>> annotations = param.getAnnotations();
assertEquals(1, annotations.size());
CtAnnotation<?> a = annotations.get(0);
Bound actualAnnotation = (Bound) a.getActualAnnotation();
assertEquals(8, actualAnnotation.max());
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testReplaceAnnotationValue.
@Test
public void testReplaceAnnotationValue() 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();
// contract: test replace of single value
CtExpression integerValue = a.getValue("integer");
assertEquals(42, ((CtLiteral<Integer>) integerValue).getValue().intValue());
assertEquals(42, annot.integer());
integerValue.replace(factory.createLiteral(17));
CtExpression newIntegerValue = a.getValue("integer");
assertEquals(17, ((CtLiteral<Integer>) newIntegerValue).getValue().intValue());
assertEquals(17, annot.integer());
// even if second value is null
try {
a.getValue("integer").replace(Arrays.asList(factory.createLiteral(18), null));
fail();
} catch (SpoonException e) {
// OK
}
// contract: replacing of single value by no value
a.getValue("integer").delete();
assertNull(a.getValue("integer"));
try {
annot.integer();
fail();
} catch (NullPointerException e) {
// OK - fails because int cannot be null
}
// contract: replace with null value means remove
a.getValue("string").replace((CtElement) null);
assertNull(a.getValue("string"));
// contract: check that null value can be returned
assertNull(annot.string());
// contract: replace with null value in collection means remove
a.getValue("clazz").replace(Collections.singletonList(null));
assertNull(a.getValue("clazz"));
// contract: check that null value can be returned
assertNull(annot.clazz());
// contract: test replace of item in collection
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
CtNewArray<?> integersNewArray = (CtNewArray) a.getValue("integers");
integersNewArray.getElements().get(0).replace(Arrays.asList(null, factory.createLiteral(101), null, factory.createLiteral(102)));
assertEquals(2, annot.integers().length);
assertEquals(101, annot.integers()[0]);
assertEquals(102, annot.integers()[1]);
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testGetAnnotationFromParameter.
@Test
public void testGetAnnotationFromParameter() {
// contract: Java 8 receiver parameters are handled
Launcher spoon = new Launcher();
spoon.addInputResource("src/test/resources/noclasspath/Initializer.java");
String output = "target/spooned-" + this.getClass().getSimpleName() + "-firstspoon/";
spoon.setSourceOutputDirectory(output);
spoon.getEnvironment().setNoClasspath(true);
Factory factory = spoon.getFactory();
spoon.buildModel();
List<CtMethod> methods = factory.getModel().getElements(new NamedElementFilter<>(CtMethod.class, "setField"));
assertThat(methods.size(), is(1));
CtMethod methodSet = methods.get(0);
assertThat(methodSet.getSimpleName(), is("setField"));
List<CtParameter> parameters = methodSet.getParameters();
assertThat(parameters.size(), is(1));
CtParameter thisParameter = parameters.get(0);
assertThat(thisParameter.getSimpleName(), is("this"));
CtTypeReference thisParamType = thisParameter.getType();
assertThat(thisParamType.getSimpleName(), is("Initializer"));
List<CtAnnotation<?>> annotations = thisParameter.getType().getAnnotations();
assertThat(annotations.size(), is(2));
CtAnnotation unknownInit = annotations.get(0);
CtAnnotation raw = annotations.get(1);
assertThat(unknownInit.getAnnotationType().getSimpleName(), is("UnknownInitialization"));
assertThat(raw.getAnnotationType().getSimpleName(), is("Raw"));
}
use of spoon.reflect.declaration.CtAnnotation in project spoon by INRIA.
the class AnnotationTest method testRepeatSameAnnotationOnLocalVariable.
@Test
public void testRepeatSameAnnotationOnLocalVariable() 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 CtMethod<?> method = ctClass.getMethodsByName("methodWithLocalVariable").get(0);
final CtLocalVariable<?> ctLocalVariable = method.getBody().getElements(new AbstractFilter<CtLocalVariable<?>>(CtLocalVariable.class) {
@Override
public boolean matches(CtLocalVariable<?> element) {
return true;
}
}).get(0);
final List<CtAnnotation<? extends Annotation>> annotations = ctLocalVariable.getAnnotations();
assertEquals("Local variable 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 \"Local 1\"", "Local 1", ((CtLiteral) annotations.get(0).getValue("value")).getValue());
assertEquals("Argument of the second annotation is \"Local 2\"", "Local 2", ((CtLiteral) annotations.get(1).getValue("value")).getValue());
}
Aggregations