how to generically compare entire java beans?
Solution 1:
Try EqualsBuilder.reflectionEquals() of commons-lang. EqualsBuilder has a set of methods to include all fields, all non-transient fields and all but certain fields.
If all else fails, the code could serve as a good example how to implement this.
Solution 2:
To answer your question directly, you could use reflection to do equality checking of beans. There are a few snags you need to be aware of.
There are rules regarding the behaviour of equals() and hashcode(). These rules talk about symmetry, consitency and reflexiveness which may be hard to do when your equals method behaves dynamically based on the other object you're passing in.
Interesting read: http://www.geocities.com/technofundo/tech/java/equalhash.html
Generally speaking, I think you are better off creating your own hashcode and equals methods. There are a fwe good plugins which can automatically generate that code for you based on the class properties.
Having said all this, here are some (old style) methods for getting getters, setters and properties I wrote a long time ago:
private Map getPrivateFields(Class clazz, Map getters, Map setters) {
Field[] fields = clazz.getDeclaredFields();
Map m = new HashMap();
for (int i = 0; i < fields.length; i++) {
int modifiers = fields[i].getModifiers();
if (Modifier.isPrivate(modifiers) && !Modifier.isStatic(modifiers) && !Modifier.isFinal(modifiers)) {
String propName = fields[i].getName();
if (getters.get(propName) != null && setters.get(propName) != null) {
m.put(fields[i].getName(), fields[i]);
}
}
}
return m;
}
The Getters:
private Map getGetters(Class clazz) {
Method[] methods = clazz.getMethods();
Map m = new HashMap();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().startsWith("get")) {
int modifiers = methods[i].getModifiers();
if (validAccessMethod(modifiers)) {
m.put(getPropertyName(methods[i].getName()), methods[i]);
}
}
}
return m;
}
And the Setters:
private Map getSetters(Class clazz, Map getters) {
Method[] methods = clazz.getMethods();
Map m = new HashMap();
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().startsWith("set")) {
int modifiers = methods[i].getModifiers();
String propName = getPropertyName(methods[i].getName());
Method getter = (Method) getters.get(propName);
if (validAccessMethod(modifiers) && getter != null) {
Class returnType = getter.getReturnType();
Class setType = methods[i].getParameterTypes()[0];
int numTypes = methods[i].getParameterTypes().length;
if (returnType.equals(setType) && numTypes == 1) {
m.put(propName, methods[i]);
}
}
}
}
return m;
}
Maybe you can use this to roll your own.
Edit: Ofcourse the reflectionbuilder in Aaron Digulla's answer is much better than my handywork.