Access annotated fields

后端 未结 1 1180
走了就别回头了
走了就别回头了 2021-02-07 08:19

I made a custom Annotation for my project which will be used only with fields, that is

@MyAnnotation int myVariable

I have another class which wi

1条回答
  •  闹比i
    闹比i (楼主)
    2021-02-07 08:23

    int getMyVariable(Foo foo) throws IllegalArgumentException, IllegalAccessException{
     for(Field f:foo.getClass().getDeclaredFields()){
      /**
       * Ensure the RetentionPolicy of 'MyAnnotation' is RUNTIME.
       */
       if(f.isAnnotationPresent(MyAnnotation.class)){
       return f.getInt(foo);
      } 
     }
     return -1;
    }
    

    0 讨论(0)
提交回复
热议问题