tags:

views:

1866

answers:

4

Hi, I've been trying to grok the org.apache.commons.beanutils library for a method/idiom to evaluate for equality all properties between 2 instances i.e. a generic equals() method for beans.
Is there a simple way to do this usnig this library? Or am I going about this the wrong way? Thanks.

+4  A: 

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.

Rolf
+6  A: 

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.

Aaron Digulla
+1  A: 

As mentioned above, a reflection-based implementation will do what you want. I just wanted to warn you, that reflection is quite costly and such an implementation could be comparably slow. If you just need to do occasional comparisons, you will be fine. However, if you have huge datasets and frequent equality checks (e.g. filtering of big tables) you might get into trouble.

netzwerg
Good point indeed. jguru has a nice comparison online:http://www.jguru.com/faq/view.jsp?EID=246569
Rolf
A: 

Wooah, thanks guys! Very impressed with quality and speed of answers. Reflection based equality was indeed what I was meaning. I'll try the apache commons solution, if that doesn't work I'll roll my own.

ysouter
When you've implemented it succesfuly, don't forget to mark one of the answers as "answer". This will make SO a nice reference for others (and no, you don't have to choose mine ;-) )
Rolf