I want to compare two Java objects without overriding equals method. Since I need to override equals method in n number of Classes I have, I am in need of a common utility method where we can compare two Java objects.
Something like:
A a1,a2;
B b1,b2;
C c1,c2;
-----
-----
boolean isEqual1 = new ObjectComparator().isEquals(a1 , a2);
boolean isEqual2 = new ObjectComparator().isEquals(b1 , b2);
boolean isEqual3 = new ObjectComparator().isEquals(c1 , c2);
Please help me out to write a common utility for comparing any Java objects
Hope by using Field class, and getClass method we can achieve it. Please guide me.
Have a look at
EqualsBuilder.reflectionEqualsin the Apache Commons library.From the documentation:
So, in your example it would look like: