Skip to content

Commit

Permalink
fix(reflection): add annotation value in the model (#1384)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux authored and surli committed Jun 9, 2017
1 parent 841edfc commit bf03cb2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,20 @@ public void addMethod(CtMethod ctMethod) {
}

@Override
public void visitAnnotation(Annotation annotation) {
public void visitAnnotation(final Annotation annotation) {
final CtAnnotation<Annotation> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ public <T extends Annotation> void visitAnnotationClass(Class<T> clazz) {
public void visitAnnotation(Annotation annotation) {
if (annotation.annotationType() != null) {
visitClassReference(annotation.annotationType());
for (RtMethod method : getDeclaredMethods(annotation.annotationType())) {
visitMethod(method);
}
}
}

Expand Down
19 changes: 15 additions & 4 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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");
Expand Down

0 comments on commit bf03cb2

Please sign in to comment.