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 09:43:00 GMT

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

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

Github user coveralls commented on the issue:

    https://github.com/apache/commons-lang/pull/343
  
    
    [![Coverage Status](https://coveralls.io/builds/18566083/badge)](https://coveralls.io/builds/18566083)
    
    Coverage remained the same at 95.25% when pulling **c70b9bd9a98cde2f768c3b0894ec3b4ac63eab99
on swingfly:master** into **8f88799d4774c7c9f4be03d84b97ff3301e64479 on apache:master**.



> 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