mercredi 29 août 2018

Recursively modify all fields in an object annotated with a given annotation

Given an input object, I need to find all fields in it with a given annotation and modify it with some value.

This annotation could also be present in an object present inside the given input object.

public class A {
  private B b;

  @MyAnnotation
  private String s;

}

public class B {

    @MyAnnotation
    private String a;

    private String b;
}

Output, in this case, should be fields s and a and I need to modify the same with some String. I found fieldUtils from apache which gives annotation on a given class, and I think I can add a simple DFS search to find all fields with the given annotation. The problem comes while setting the value using reflection, I need to specify the object as part of field.set(). I'm not sure which object should be used while setting the value of a and how to get that in a generic manner (in case I need to retrieve instance of B from A in this example)





Aucun commentaire:

Enregistrer un commentaire