Significant fields: equals does not use *field*, or it is stateless.

633 views
Skip to first unread message

landlordlll

unread,
Aug 18, 2017, 4:25:13 PM8/18/17
to equalsverifier
Hello Jan,

The subject provided is the error i'm getting for the following code:

Artist.java

import org.springframework.stereotype.Component;
import lombok.Data;

@Component
@Data
public class Artist {
  private static final String DEFAULT_COUNTRY = "Philippines";
  private String country = DEFAULT_COUNTRY;
  private String name;
  private String dateOfBirth;
}

Technique.java

 @Component
 @ConfigurationProperties(prefix = "artist.technique")
 @Data
 public static final class Technique extends Artist {
   private String medium;
 }

I try to pass the equals test with the following code:

EqualsVerifier.forClass(Technique.class).suppress(Warning.NONFINAL_FIELDS).withRedefinedSuperclass().verify();

It should work with the .withRedefinedSuperclass(), but I'm not sure why.. 

Please let me know if you need anything else. Thank you! 

Regards,
landlordlll

Jan Ouwens

unread,
Aug 21, 2017, 2:01:49 AM8/21/17
to equalsverifier
Hi!

I can reproduce the behaviour you describe. I have run Delombok ( https://projectlombok.org/features/delombok ) on your code, and it turns out that this is what Lombok generates for Technique's equals method:

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    @javax.annotation.Generated("lombok")
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof Technique)) return false;
        final Technique other = (Technique) o;
        if (!other.canEqual((java.lang.Object) this)) return false;
        final java.lang.Object this$medium = this.getMedium();
        final java.lang.Object other$medium = other.getMedium();
        if (this$medium == null ? other$medium != null : !this$medium.equals(other$medium)) return false;
        return true;
    }

As you can see, it doesn't look at country at all, and it doesn't call super.equals either. In other words, all of Artist's fields are ignored by Technique's equals method.

If you use @EqualsAndHashCode, you can pass it a parameter to look at the superclass, like this: @EqualsAndHashCode(callSuper=true)
There's probably a similar parameter for @Data, you'll have to check the documentation to make sure.


Cheers,
Jan


--
You received this message because you are subscribed to the Google Groups "equalsverifier" group.
To unsubscribe from this group and stop receiving emails from it, send an email to equalsverifie...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages