You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Benedikt Ritter (JIRA)" <ji...@apache.org> on 2017/06/24 17:01:03 UTC

[jira] [Commented] (LANG-1341) equalsBuilder.appendSuper() has incorrect behavior if the object you call equals() on is a direct supertype of Object

    [ https://issues.apache.org/jira/browse/LANG-1341?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16062061#comment-16062061 ] 

Benedikt Ritter commented on LANG-1341:
---------------------------------------

Patches welcome!

> equalsBuilder.appendSuper() has incorrect behavior if the object you call equals() on is a direct supertype 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)