Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>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.</p> <p>Interesting read: <a href="http://www.geocities.com/technofundo/tech/java/equalhash.html" rel="nofollow noreferrer">http://www.geocities.com/technofundo/tech/java/equalhash.html</a></p> <p>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.</p> <p>Having said all this, here are some (old style) methods for getting getters, setters and properties I wrote a long time ago:</p> <pre><code>private Map getPrivateFields(Class clazz, Map getters, Map setters) { Field[] fields = clazz.getDeclaredFields(); Map m = new HashMap(); for (int i = 0; i &lt; fields.length; i++) { int modifiers = fields[i].getModifiers(); if (Modifier.isPrivate(modifiers) &amp;&amp; !Modifier.isStatic(modifiers) &amp;&amp; !Modifier.isFinal(modifiers)) { String propName = fields[i].getName(); if (getters.get(propName) != null &amp;&amp; setters.get(propName) != null) { m.put(fields[i].getName(), fields[i]); } } } return m; } </code></pre> <p>The Getters:</p> <pre><code>private Map getGetters(Class clazz) { Method[] methods = clazz.getMethods(); Map m = new HashMap(); for (int i = 0; i &lt; 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; } </code></pre> <p>And the Setters:</p> <pre><code>private Map getSetters(Class clazz, Map getters) { Method[] methods = clazz.getMethods(); Map m = new HashMap(); for (int i = 0; i &lt; 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) &amp;&amp; getter != null) { Class returnType = getter.getReturnType(); Class setType = methods[i].getParameterTypes()[0]; int numTypes = methods[i].getParameterTypes().length; if (returnType.equals(setType) &amp;&amp; numTypes == 1) { m.put(propName, methods[i]); } } } } return m; } </code></pre> <p>Maybe you can use this to roll your own.</p> <p><strong>Edit:</strong> Ofcourse the <a href="http://commons.apache.org/lang/api/org/apache/commons/lang/builder/EqualsBuilder.html#reflectionEquals(java.lang.Object,%20java.lang.Object)" rel="nofollow noreferrer">reflectionbuilder</a> in <a href="https://stackoverflow.com/questions/472626/how-to-generically-compare-entire-java-beans#472705">Aaron Digulla's answer</a> is much better than my handywork. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload