User does not override
equals
method defined in superclass ComparableEntity16
17@Document(collection = "users")
18@Getter @Setter
19public class User extends ComparableEntity implements UserDetails {2021 @Id22 private ObjectId id;2324 @CreatedDate25 private LocalDateTime createdAt;26 @LastModifiedDate27 private LocalDateTime modifiedAt;2829 private boolean enabled = true;3031 @Indexed(unique = true)32 private String username;33 private String password;34 @Indexed35 private String fullName;36 private Set<Role> authorities = new HashSet<>();3738 public User() {39 }4041 public User(String username, String password) {42 this.username = username;43 this.password = password;44 }4546 @Override47 public boolean isAccountNonExpired() {48 return enabled;49 }5051 @Override52 public boolean isAccountNonLocked() {53 return enabled;54 }5556 @Override57 public boolean isCredentialsNonExpired() {58 return enabled;59 }60}
Book does not override
equals
method defined in superclass ComparableEntity17
18@Document(collection = "books")
19@Getter @Setter
20public class Book extends ComparableEntity {2122 @Id23 private ObjectId id;2425 @CreatedBy26 private ObjectId creatorId;27 @LastModifiedBy28 private ObjectId modifierId;2930 @CreatedDate31 private LocalDateTime createdAt;32 @LastModifiedDate33 private LocalDateTime modifiedAt;3435 private String title;36 private String about;37 private String language;38 private Set<String> genres = new HashSet<>();39 private String isbn13;40 private String isbn10;41 private String publisher;42 private LocalDate publishDate;43 private int hardcover;4445 private Set<ObjectId> authorIds = new HashSet<>();4647}
Author does not override
equals
method defined in superclass ComparableEntity16
17@Document(collection = "authors")
18@Getter @Setter
19public class Author extends ComparableEntity {2021 @Id22 private ObjectId id;2324 @CreatedBy25 private ObjectId creatorId;26 @LastModifiedBy27 private ObjectId modifierId;2829 @CreatedDate30 private LocalDateTime createdAt;31 @LastModifiedDate32 private LocalDateTime modifiedAt;3334 private String fullName;35 private String about;36 private String nationality;37 private Set<String> genres = new HashSet<>();3839 private Set<ObjectId> bookIds = new HashSet<>();4041}
Description
This class extends a class that defines equals
and adds fields, but doesn't define equals
itself. Thus, equality on instances of this class will ignore its identity and its added fields.
Bad Practice
class Parent {
int field1 = 3;
@Override
public boolean equals(Object other) {
if (other is Parent && field1 == (Parent)other.field1)
// ...
}
}
class Child extends Parent {
int field2 = 5;
}
Here, comparison of Child
objects will use the equals
method implemented in Parent
.
Be sure this is what is intended, and that you don't need to override the equals
method. Even if you don't need to override the equals
method, consider overriding it anyway to document the fact that equality for the subclass works the same way as equality for the superclass.
Recommended
Override the equals method in the child class even if you intend the current behavior; it will make the decision not to include the extra field in the equality condition explicit.
class Child extends Parent {
int field2 = 5;
@Override
public boolean equals(Object other) {
return super.equals(other);
}
}
References
- Spotbugs - EQ_DOESNT_OVERRIDE_EQUALS