commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (LANG-1341) equalsBuilder.appendSuper() has incorrect behavior if the object you call equals() on is a direct supertype of Object
Date Mon, 20 Aug 2018 17:23:00 GMT

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

ASF GitHub Bot commented on LANG-1341:
--------------------------------------

Github user britter commented on a diff in the pull request:

    https://github.com/apache/commons-lang/pull/343#discussion_r211340995
  
    --- Diff: src/main/java/org/apache/commons/lang3/builder/EqualsBuilder.java ---
    @@ -25,10 +29,6 @@
     import java.util.List;
     import java.util.Set;
     
    -import org.apache.commons.lang3.ArrayUtils;
    -import org.apache.commons.lang3.ClassUtils;
    -import org.apache.commons.lang3.tuple.Pair;
    -
    --- End diff --
    
    Please don't reorganize imports


> 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
(v7.6.3#76005)

Mime
View raw message