how to generically compare entire java beans?

后端 未结 4 931
遥遥无期
遥遥无期 2020-12-02 01:58

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 equ

相关标签:
4条回答
  • 2020-12-02 02:02

    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.

    0 讨论(0)
  • 2020-12-02 02:12

    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.

    0 讨论(0)
  • 2020-12-02 02:17

    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.

    0 讨论(0)
  • 2020-12-02 02:28

    Or, although not a direct answer to your question, but it might be an answer to your problem (i.e. remove the effort of doing boilerplate code while being super fast)

    if you use Eclipse, the following steps will auto generate the hashCode and equals for you:

    Source > Generate hashCode and equals...

    and then select the fields, it's super effective! :D

    Cheers and I hope it helps whoever comes here with the purpose of cutting some time writing boilerplate.

    PS: I'm sure other popular IDEs must have similar features.

    0 讨论(0)
提交回复
热议问题