Call JsonProperty setter method from a dependent class

Viewed 39

I have a class as follows:

public class MyClass {
   @JsonProperty("my_id")
   private String id;

   @JsonProperty("my_list")
    private List<SecondClass> myList;

   public getId() {
      return this.id;
   }

   public setId(String id) {
      this.id = id;
   }

   public getMyList() {
      return this.myList;
   }

   public setMyList(List<SecondClass> myList) {
      this.myList = myList;
   }
}

My class has a dependency on another class called SecondClass [through the List entity]

public class SecondClass {
   @JsonProperty("my_name")
   private String name;

   public getName() {
      return this.name;
   }

   public setName(String name) {
      this.name = name;
   }
}

I know how to access the getters and setters of "MyClass" using Reflection based on the JsonProperty name, as shown below:

public void myMethod(MyClass myClass, String jsonProperty, String newId) throws IllegalAccessException {
    for (Field field : MyClass.class.getDeclaredFields()) {
        JsonProperty jsonPropAnnotation = field.getAnnotation(JsonProperty.class);
        if (jsonPropAnnotation != null)
            if (jsonPropAnnotation.value().equals(jsonProperty)) {
                field.setAccessible(true);
                field.set(myClass, newId);
            }
    }
}

But, my question is, is there a way to fetch the getter and setter from SecondClass via MyClass based on the JsonProperty name using Reflection?

As an example I would like to call getList() based on JsonProperty value "my_list" and then setName() based on the JsonProperty value "my_name".

Is this a possibility using reflection?

1 Answers

I am not sure what you want to achieve and how you will pass the values to set but all this is possible. Please check below code piece for your problem. If you can explain me the full requirement then may be I can help you in implementing the best solution but for this ask below code will also work -

public class Main {
public static void main(String[] args) throws IllegalAccessException {
    MyClass myClass = new MyClass();
    List<SecondClass> secondClasses = new ArrayList<>();
    myClass.setId("1234");
    SecondClass sc1 = new SecondClass();
    sc1.setName("Name1");
    secondClasses.add(sc1);

    myMethod(myClass, "my_id", "1234");
    System.out.println(myClass.getId());
    for (SecondClass secondClass : secondClasses) {
        SecondClass secondClass1 = new SecondClass();
        myMethod(secondClass1, "my_name", secondClass);
        System.out.println(secondClass1.getName());
    }

}

public static void myMethod(Object object, String jsonProperty, Object value) throws IllegalAccessException {
    for (Field field : object.getClass().getDeclaredFields()) {

        JsonProperty jsonPropAnnotation = field.getAnnotation(JsonProperty.class);
        if (jsonPropAnnotation != null)
            if (jsonPropAnnotation.value().equals(jsonProperty)) {
                if (field.getType().equals(String.class) || field.getType().equals(Double.class)) {
                    field.setAccessible(true);
                    if (value.getClass().equals(String.class)) {
                        field.set(object, value);
                    }else{
                        field.set(object, field.get(value));
                    }
                }

            }
    }
}
}
Related