Hi Folks, hopefully this question is appropriate for the dev list.


I have been benchmarking EqualsBuilder and run into what must be a common 
gotcha.


The docs for EqualsBuilder suggests that one construct the equals method:-

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof MyClass == false) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        MyClass rhs = MyClass obj;
        return new EqualsBuilder().
                appendSuper(super.equals(obj)).
                append(field1, rhs.field1).
                append(fieldN, rhs.fieldN).
                isEquals();
    }

The trouble with this is it will never work as expected if the object's parent does not implement its own equals method, E.g. a simple pojo derived from Object.

The reason it fails to work as expected is that Object.equals() returns true if and only if this and obj refer to the same object.


At the very least the javadoc needs to be modified to high light this. (Don't call appendSuper(super.equals(obj)) unless you know the parents implement equals.)

Would a better solution be to depricate appendSuper(boolean) and perhaps implement an appendSuper(Object lhs, Object rhs) which detects if super is Object and handles it appropriately.


Or am I barking up the wrong tree?

Peter.






---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to