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

Conversation

monperrus
Copy link
Collaborator

Two advantages:

  • this eases the API: direct annot.getValueAsObject("foo") instead of ((CAST)annot.getActualAnnotation()).foo()
  • this enables getting the annotation value even if the annotation binary class file is not in the classpath


/** 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

@pvojtechovsky
Copy link
Collaborator

Thank You Martin.

@spoon-bot
Copy link
Collaborator

API changes: 3 (Detected by Revapi)

Old API: fr.inria.gforge.spoon:spoon-core:jar:6.3.0-20180606.145239-122 / New API: fr.inria.gforge.spoon:spoon-core:jar:6.3.0-SNAPSHOT

Method was added to an interface.
Old none
New method CtAnnotation#getValueAsInt(String)
Breaking binary: non_breaking,
Method was added to an interface.
Old none
New method CtAnnotation#getValueAsObject(String)
Breaking binary: non_breaking,
Method was added to an interface.
Old none
New method CtAnnotation#getValueAsString(String)
Breaking binary: non_breaking,

@surli surli merged commit dcdfca0 into INRIA:master Jun 7, 2018
@surli surli mentioned this pull request Jun 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants