Below is the code to compare the complex objects. Ok...it is not some magic stick which you can use to compare any of the objects of your custom class.
Yes, there are some conditions & limitations -
1) Your custom classes better to override equals()
2) You have to use the jdk1.7
3) If the objects which you are comparing also referencing other complex classes then it will not go down the hierarchy to find the fields having differences.
It will just tell about the fields in the current objects.
So why not, just check it once to know more about it :)
/**
* Below is the method to compare the fields of any type of objects passed to it.
* But the classes of passed objects must be comparable & must override the equals() of Object class
* to make those objects comparable.
* It is always better to override the equals() in your classes to keep future requirements in view.
* jdk1.7 is used because Objects class is introduced in jdk1.7 only.
* It returns a list of names of fields of the passed objects which have values different.
* @param obj1
* @param obj2
* @return list of field names
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static List<String> compare(Object obj1, Object obj2) throws IllegalArgumentException, IllegalAccessException{
List<Field> differentFields = new LinkedList<>();
List<String> differentField = new LinkedList<>();
if(obj1 != obj2){
Class clazz = obj1.getClass();
Field[] fields = clazz.getDeclaredFields();
for(Field f: fields){
boolean accessible = f.isAccessible();
if(! accessible){
f.setAccessible(true);
}
Object obj1f = f.get(obj1);
Object obj2f = f.get(obj2);
if(!Objects.equals(obj1f, obj2f)){
differentFields.add(f);
differentField.add(f.getName());
}
f.setAccessible(accessible);
}
}
return differentField;
}
}
Yes, there are some conditions & limitations -
1) Your custom classes better to override equals()
2) You have to use the jdk1.7
3) If the objects which you are comparing also referencing other complex classes then it will not go down the hierarchy to find the fields having differences.
It will just tell about the fields in the current objects.
So why not, just check it once to know more about it :)
/**
* Below is the method to compare the fields of any type of objects passed to it.
* But the classes of passed objects must be comparable & must override the equals() of Object class
* to make those objects comparable.
* It is always better to override the equals() in your classes to keep future requirements in view.
* jdk1.7 is used because Objects class is introduced in jdk1.7 only.
* It returns a list of names of fields of the passed objects which have values different.
* @param obj1
* @param obj2
* @return list of field names
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static List<String> compare(Object obj1, Object obj2) throws IllegalArgumentException, IllegalAccessException{
List<Field> differentFields = new LinkedList<>();
List<String> differentField = new LinkedList<>();
if(obj1 != obj2){
Class clazz = obj1.getClass();
Field[] fields = clazz.getDeclaredFields();
for(Field f: fields){
boolean accessible = f.isAccessible();
if(! accessible){
f.setAccessible(true);
}
Object obj1f = f.get(obj1);
Object obj2f = f.get(obj2);
if(!Objects.equals(obj1f, obj2f)){
differentFields.add(f);
differentField.add(f.getName());
}
f.setAccessible(accessible);
}
}
return differentField;
}
}