diff --git a/src/main/java/spoon/support/visitor/java/JavaReflectionTreeBuilder.java b/src/main/java/spoon/support/visitor/java/JavaReflectionTreeBuilder.java index 91093fec46c..e380bbbed68 100644 --- a/src/main/java/spoon/support/visitor/java/JavaReflectionTreeBuilder.java +++ b/src/main/java/spoon/support/visitor/java/JavaReflectionTreeBuilder.java @@ -217,10 +217,20 @@ public void addMethod(CtMethod ctMethod) { } @Override - public void visitAnnotation(Annotation annotation) { + public void visitAnnotation(final Annotation annotation) { final CtAnnotation ctAnnotation = factory.Core().createAnnotation(); - enter(new AnnotationRuntimeBuilderContext(ctAnnotation)); + enter(new AnnotationRuntimeBuilderContext(ctAnnotation) { + @Override + public void addMethod(CtMethod ctMethod) { + try { + Object value = annotation.getClass().getMethod(ctMethod.getSimpleName()).invoke(annotation); + ctAnnotation.addValue(ctMethod.getSimpleName(), value); + } catch (Exception ignore) { + ctAnnotation.addValue(ctMethod.getSimpleName(), ""); + } + } + }); super.visitAnnotation(annotation); exit(); diff --git a/src/main/java/spoon/support/visitor/java/JavaReflectionVisitorImpl.java b/src/main/java/spoon/support/visitor/java/JavaReflectionVisitorImpl.java index 0711615687a..d4a7837c8c0 100644 --- a/src/main/java/spoon/support/visitor/java/JavaReflectionVisitorImpl.java +++ b/src/main/java/spoon/support/visitor/java/JavaReflectionVisitorImpl.java @@ -154,6 +154,9 @@ public void visitAnnotationClass(Class clazz) { public void visitAnnotation(Annotation annotation) { if (annotation.annotationType() != null) { visitClassReference(annotation.annotationType()); + for (RtMethod method : getDeclaredMethods(annotation.annotationType())) { + visitMethod(method); + } } } diff --git a/src/test/java/spoon/test/annotation/AnnotationTest.java b/src/test/java/spoon/test/annotation/AnnotationTest.java index befa1fd021f..70b6344f66d 100644 --- a/src/test/java/spoon/test/annotation/AnnotationTest.java +++ b/src/test/java/spoon/test/annotation/AnnotationTest.java @@ -6,9 +6,11 @@ import spoon.OutputType; import spoon.processing.AbstractAnnotationProcessor; import spoon.processing.ProcessingManager; +import spoon.reflect.annotations.PropertyGetter; import spoon.reflect.code.CtBlock; import spoon.reflect.code.CtConstructorCall; import spoon.reflect.code.CtExpression; +import spoon.reflect.code.CtFieldRead; import spoon.reflect.code.CtLiteral; import spoon.reflect.code.CtLocalVariable; import spoon.reflect.code.CtNewArray; @@ -26,12 +28,12 @@ import spoon.reflect.declaration.CtField; import spoon.reflect.declaration.CtInterface; import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtNamedElement; import spoon.reflect.declaration.CtPackage; import spoon.reflect.declaration.CtParameter; import spoon.reflect.declaration.CtType; import spoon.reflect.declaration.CtTypeParameter; import spoon.reflect.factory.Factory; -import spoon.reflect.reference.CtPackageReference; import spoon.reflect.reference.CtTypeReference; import spoon.reflect.visitor.DefaultJavaPrettyPrinter; import spoon.reflect.visitor.filter.AbstractFilter; @@ -54,10 +56,10 @@ import spoon.test.annotation.testclasses.GlobalAnnotation; import spoon.test.annotation.testclasses.InnerAnnot; import spoon.test.annotation.testclasses.Main; +import spoon.test.annotation.testclasses.PortRange; import spoon.test.annotation.testclasses.SuperAnnotation; import spoon.test.annotation.testclasses.TestInterface; import spoon.test.annotation.testclasses.TypeAnnotation; -import spoon.test.annotation.testclasses.PortRange; import java.io.File; import java.lang.annotation.Annotation; @@ -67,9 +69,7 @@ import java.util.List; import java.util.Set; - import static org.hamcrest.core.Is.is; - import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -93,6 +93,17 @@ public void setUp() throws Exception { factory = launcher.getFactory(); } + @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()); + } + @Test public void testModelBuildingAnnotationBound() throws Exception { CtType type = this.factory.Type().get("spoon.test.annotation.testclasses.Bound");