Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add CtAnnotation#getAllValues to reason about all annotation values #2042

Merged
merged 5 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtS
@PropertyGetter(role = VALUE)
Map<String, CtExpression> getValues();

/** Get all values of {@link #getValues()}, plus the default ones defined in the annotation type. */
@DerivedProperty
Map<String, CtExpression> getAllValues();

/**
* Sets the annotation's type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ public Map<String, CtExpression> getValues() {
return Collections.unmodifiableMap(elementValues);
}

@Override
public Map<String, CtExpression> getAllValues() {
Map<String, CtExpression> values = new TreeMap();
// first, we put the default values
CtAnnotationType<?> annotationType = (CtAnnotationType) getAnnotationType().getTypeDeclaration();
for (CtAnnotationMethod m : annotationType.getAnnotationMethods()) {
values.put(m.getSimpleName(), m.getDefaultExpression());
}

// we override the values with ones of this expression
values.putAll(elementValues);
return Collections.unmodifiableMap(values);
}

private Object getReflectValue(String fieldname) {
try {
Class<?> c = getAnnotationType().getActualClass();
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ public void testModelBuildingAnnotationBoundUsage() throws Exception {
Bound actualAnnotation2 = (Bound) annot.getActualAnnotation();
assertEquals(10, actualAnnotation2.max());

// contract: getAllvalues
// only direct value, no default ones in getValues()
assertEquals(1, a.getValues().size());
assertEquals(0, annot.getValues().size());

// direct values and default ones in getValues()
assertEquals(1, a.getAllValues().size());
assertEquals(1, annot.getAllValues().size());

// the good value is selected, not the default value
assertEquals("8", a.getAllValues().get("max").toString());


}

@Test
Expand Down Expand Up @@ -198,6 +211,9 @@ public void testAnnotationParameterTypes() throws Exception {
assertEquals(1, annotations.size());

CtAnnotation<?> a = annotations.get(0);

assertEquals(15, a.getAllValues().size());

AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
Expand Down