Skip to content

Commit dcdfca0

Browse files
monperrussurli
authored andcommitted
feat: one can get annotation values directly (#2036)
* feature: one can get annotation values directly * Commit by Martin Monperrus on 06 June 2018
1 parent 62e31c0 commit dcdfca0

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/main/java/spoon/reflect/declaration/CtAnnotation.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtS
6868
CtTypeReference<A> getAnnotationType();
6969

7070
/**
71-
* Gets a value for a given key without any conversion.
71+
* Gets a value, as a CtExpression, for a given key without any conversion.
72+
*
73+
* If you need the actual value (eg an integer and not a literal, see {@link #getValueAsObject(String)} and similar methods.
74+
*
7275
* Note that this value type does not necessarily corresponds to the annotation
7376
* type member. For example, in case the annotation type expect an array of Object,
7477
* and a single value is given, Spoon will return only the object without the CtNewArray.
@@ -81,6 +84,18 @@ public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtS
8184
@PropertyGetter(role = VALUE)
8285
<T extends CtExpression> T getValue(String key);
8386

87+
/** Returns the actual value of an annotation property */
88+
@DerivedProperty
89+
Object getValueAsObject(String key);
90+
91+
/** Returns the actual value of an annotation property, as an integer (utility method) */
92+
@DerivedProperty
93+
int getValueAsInt(String key);
94+
95+
/** Returns the actual value of an annotation property, as a String (utility method) */
96+
@DerivedProperty
97+
String getValueAsString(String key);
98+
8499
/**
85100
* Gets a value for a given key and try to fix its type based on the
86101
* annotation type. For example, if the annotation type member expects an array of String,

src/main/java/spoon/support/reflect/declaration/CtAnnotationImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,22 @@ public <T extends CtExpression> T getValue(String key) {
312312
return (T) getValueAsExpression(key);
313313
}
314314

315-
private Object getValueAsObject(String key) {
315+
@Override
316+
public int getValueAsInt(String key) {
317+
Object val = getValueAsObject(key);
318+
if (val == null) {
319+
throw new IllegalStateException(key + " not in the annotation");
320+
}
321+
return (int) val;
322+
}
323+
324+
@Override
325+
public String getValueAsString(String key) {
326+
return (String) getValueAsObject(key);
327+
}
328+
329+
@Override
330+
public Object getValueAsObject(String key) {
316331
CtExpression expr = getWrappedValue(key);
317332
Object ret = convertElementToRuntimeObject(expr);
318333
Class<?> type = getElementType(key);

src/test/java/spoon/test/annotation/AnnotationTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,16 @@ public void testAnnotationParameterTypes() throws Exception {
199199

200200
CtAnnotation<?> a = annotations.get(0);
201201
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
202+
assertEquals(42, a.getValueAsInt("integer"));
202203
assertEquals(42, annot.integer());
203204
assertEquals(1, annot.integers().length);
204205
assertEquals(42, annot.integers()[0]);
206+
assertEquals("Hello World!", a.getValueAsString("string"));
205207
assertEquals("Hello World!", annot.string());
206208
assertEquals(2, annot.strings().length);
207209
assertEquals("Hello", annot.strings()[0]);
208210
assertEquals("World", annot.strings()[1]);
211+
assertEquals(Integer.class, a.getValueAsObject("clazz"));
209212
assertEquals(Integer.class, annot.clazz());
210213
assertEquals(2, annot.classes().length);
211214
assertEquals(Integer.class, annot.classes()[0]);

0 commit comments

Comments
 (0)