Title: Reflection geting value from method argument annotation
Tags: java;reflection;annotations
Question: Sample code:
public interface TestClass {
@AnnoTest
public Object getTestObject( @AnnoTestArg("id") Integer postId );
How can i get value from @AnnoTestArg annotation ? I know how to check is argument annotated but, i can't check annotation value.
That's my code:
public void build(...) {
Annotation[][] anno = pm.getMethod().getParameterAnnotations();
for( Annotation a : anno[argNumber] ) {
if( a.equals(AnnoTestArg.class) ) {
// value ?
}
return connector;

Here is the accepted answer: Annotation is the superinterface of all annotation types. Assuming your AnnoTestArg annotation is something like
@Target(value = ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@interface AnnoTestArg {
String value();
you can just cast the Annotation value to AnnoTestArg and invoke the appropriate method
for (Annotation a : anno[0]) {
if (a instanceof AnnoTestArg) {
AnnoTestArg arg = (AnnoTestArg) a;
System.out.println(arg.value());


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:04/03/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><reflection><annotations>', 'Title': 'Reflection geting value from method argument annotation', 'ViewCount': 73, 'id': 29436566, 'user_AboutMe': "<p>I'm focused on Spring Framework, but currently fascinated front-end development using AngularJS. :)</p>\n", 'user_AccountId': 3765641.0, 'user_DisplayName': 'kris14an', 'user_Reputation': 731.0, 'user_Views': 59.0, 'user_id': 3129252.0}}