0

I'm using custom annotations in my App. I have a requirement where I need to read the value of an annotated field using reflection. For example

    @CheckNull
    String name;

I can have fileds declared like this. So, in some other class where I process this annotation, I need to read value of name field,

Is it possible?

Please don't make it duplicate of this question Because, as per the accepted answer

for(Field field : cls.getDeclaredFields()){
  Class type = field.getType();
  String name = field.getName();
  Annotation[] annotations = field.getDeclaredAnnotations();
 }

It reads the type , name of the field and the annotations present in the field. My requirement is different. Say, if I have a field

   @CheckNull
   private String name;

And if someone has assinged it a value

     name = "doe";

What I need to read is the value "doe". Not the annotatios, type or filed name

doe
  • 971
  • 2
  • 11
  • 27
  • sure, you can iterate through all fields, and check if the field is annotated with your annotation. Then you can read the field value with reflection – Vladyslav Matviienko Jun 11 '18 at 07:23
  • Also see https://docs.oracle.com/javase/tutorial/reflect/member/fieldValues.html, https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#method_summary – Radiodef Jun 11 '18 at 15:06
  • The accepted answer is a combination of two of the duplicates. – ivan_pozdeev Jun 11 '18 at 21:41

0 Answers0