I need to write some generic solution to find out what properties in two objects have changed and return the changed properties (not the value).
class Student {
public String name;
public Address address;
public int age;
}
class Address {
public String hno;
public String street;
public int pin;
}
public class Test {
public static void main(String... arg) {
Student s1 = new Student();
s1.name = "Krishna";
s1.age = 30;
s1.address = new Address();
s1.address.hno = "2-2-22";
s1.address.street = "somewhere";
s1.address.pin = 123;
Student s2 = new Student();
s2.name = "Krishna";
s2.age = 20;
s2.address = new Address();
s2.address.hno = "1-1-11";
s2.address.street = "nowhere";
s2.address.pin = 123;
List<String> = difference(s1, s2);
// expected result
// Student.age
// Student.address.hno
// Student.address.street
}
}
Can anyone please suggest some solution?
PS
Overriding equals/hashcode is not an option for me.
I have written the below code but I am not sure how to identify if a type is complex (for example Address)
private static List<String> difference(Student s1, Student s2) throws IllegalArgumentException, IllegalAccessException {
for(Field field: Student.class.getDeclaredFields()) {
System.out.println(field.getName() + " " +field.get(s1).equals(field.get(s2)));
}
return null;
}