Kevin Binswanger created LANG-1341:
--------------------------------------
Summary: equalsBuilder.appendSuper() always fails if one of the
two classes is a direct subtype of Object
Key: LANG-1341
URL: https://issues.apache.org/jira/browse/LANG-1341
Project: Commons Lang
Issue Type: Bug
Reporter: Kevin Binswanger
Priority: Minor
If you use EqualsBuilder.appendSuper(super.equals(Object)) on an Object whose
direct supertype is Object, it will succeed if and only if the two references
are the same (because that's what Object.equals does).
Reading it out loud, this is obvious, but I think the Javadoc for EqualsBuilder
encourages you to make this mistake. In particular, here's some text in the
summary of the Javadoc:
{quote}Typical use for the code is as follows:{quote}
{code}
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj.getClass() != getClass()) {
return false;
}
MyClass rhs = (MyClass) obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(field1, rhs.field1)
.append(field2, rhs.field2)
.append(field3, rhs.field3)
.isEquals();
}{code}
There's also no mention of this in the javadocs for the appendSuper() method
itself.
My suggestion is to fix the example at the beginning. The way HashCodeBuilder
does this, which I think is the best, is to leave appendSuper out of the
example and adds this line after: {quote}If required, the superclass hashCode()
can be added using appendSuper(int).{quote}
For completeness, here's a snippet of code that illustrates this problem.
{code}
public class Thing {
public String text;
@Override
public boolean equals(final Object other) {
if (other == null) {
return false;
}
if (other == this) {
return true;
}
if (other.getClass() != getClass()) {
return false;
}
final Thing thing = (Thing) other;
return new EqualsBuilder()
.appendSuper(super.equals(other))
.append(text, thing.text)
.isEquals();
}
}{code}
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)