Skip to content

feature: one can get annotation values directly #2036

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

Merged
merged 2 commits into from
Jun 7, 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
17 changes: 16 additions & 1 deletion src/main/java/spoon/reflect/declaration/CtAnnotation.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public interface CtAnnotation<A extends Annotation> extends CtExpression<A>, CtS
CtTypeReference<A> getAnnotationType();

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

/** Returns the actual value of an annotation property */
@DerivedProperty
Object getValueAsObject(String key);

/** Returns the actual value of an annotation property, as an integer (utility method) */
@DerivedProperty
int getValueAsInt(String key);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the key does not exist? We would get a ClassCastException?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would return null. In Java cast(null) returns null.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will work with the string but not int as an int can never return null.
Actually I just tested it and it throws a NPE. Could you add a check?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/** Returns the actual value of an annotation property, as a String (utility method) */
@DerivedProperty
String getValueAsString(String key);

/**
* Gets a value for a given key and try to fix its type based on the
* annotation type. For example, if the annotation type member expects an array of String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,22 @@ public <T extends CtExpression> T getValue(String key) {
return (T) getValueAsExpression(key);
}

private Object getValueAsObject(String key) {
@Override
public int getValueAsInt(String key) {
Object val = getValueAsObject(key);
if (val == null) {
throw new IllegalStateException(key + " not in the annotation");
}
return (int) val;
}

@Override
public String getValueAsString(String key) {
return (String) getValueAsObject(key);
}

@Override
public Object getValueAsObject(String key) {
CtExpression expr = getWrappedValue(key);
Object ret = convertElementToRuntimeObject(expr);
Class<?> type = getElementType(key);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,16 @@ public void testAnnotationParameterTypes() throws Exception {

CtAnnotation<?> a = annotations.get(0);
AnnotParamTypes annot = (AnnotParamTypes) a.getActualAnnotation();
assertEquals(42, a.getValueAsInt("integer"));
assertEquals(42, annot.integer());
assertEquals(1, annot.integers().length);
assertEquals(42, annot.integers()[0]);
assertEquals("Hello World!", a.getValueAsString("string"));
assertEquals("Hello World!", annot.string());
assertEquals(2, annot.strings().length);
assertEquals("Hello", annot.strings()[0]);
assertEquals("World", annot.strings()[1]);
assertEquals(Integer.class, a.getValueAsObject("clazz"));
assertEquals(Integer.class, annot.clazz());
assertEquals(2, annot.classes().length);
assertEquals(Integer.class, annot.classes()[0]);
Expand Down