I have a class as follows:
public class MyClass {
@JsonProperty("my_id")
private String id;
public getId() {
return this.id;
}
public setId(String id) {
this.id = id;
}
}
I have another class where I have the JsonProperty [my_id] information with me. With this information, I would like to trigger the getId() and setId() methods.
public class AnotherClass {
// jsonProperty value is "my_id"
public myMethod(MyClass myClass, String jsonProperty, String newId) {
// call the setter method setId(newId) of myClass
// call the getter method getId() of myClass
}
}
I do understand that this is a classic case of Reflection in Java but I am unable to implement the same even after going through hours of documentation and resources on reflection. Can someone please help me here?
Would it also be possible to use Jackson ObjectMapper or Google Gson to get the desired result?
Edit:
Two of the solutions provided by "@gmrm" and "@suraj tomar" does the intended task [Thank you both]. But, both solutions are forced to iterate over each of the available fields. Instead of iterating over all the "Fields", isn't there a way to simply fetch the Field I am looking for based on the JsonProperty name? As an example:
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);
}
}
}
The solution above works. Yet, I would like to avoid the loop below, if at all it is possible.
for (Field field : MyClass.class.getDeclaredFields())