-
Notifications
You must be signed in to change notification settings - Fork 743
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend SuperEqualsIsObjectEquals
to cover hashCode
, renaming it to "SuperCallToObjectMethod
."
#4155
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||||
Implementations of `equals` and `hashCode` should usually not delegate to | ||||||
`Object.equals` and `Object.hashCode`. | ||||||
|
||||||
Those two methods implement equality based on object identity. That | ||||||
implementation *is* sometimes what the author intended. (This check attempts to | ||||||
identify those cases and *not* report a warning for them. But in some cases, it | ||||||
still produces a warning when it shouldn't.) | ||||||
|
||||||
But when `super.equals` and `super.hashCode` call the methods defined in | ||||||
`Object`, the developer often did *not* intend to use object identity. Often, | ||||||
developers write something like: | ||||||
|
||||||
```java | ||||||
private final int id; | ||||||
|
||||||
@Override | ||||||
public boolean equals(Object obj) { | ||||||
if (obj instanceof Foo) { | ||||||
return super.equals(obj) && id == ((Foo) obj).id; | ||||||
} | ||||||
return obj; | ||||||
} | ||||||
|
||||||
@Override | ||||||
public int hashCode() { | ||||||
return super.hashCode() ^ id; | ||||||
} | ||||||
``` | ||||||
|
||||||
This appears to be an attempt to define equality in terms of the `id` field in | ||||||
this class and any fields in the superclass. However, when the superclass that | ||||||
defines `equals` or `hashCode` is `Object`, the code instead defines equality in | ||||||
terms of a mix of object identity and field values. The result is equivalent to | ||||||
defining it in terms of identity alone—which is equivalent to not overriding | ||||||
`equals` and `hashCode` at all! | ||||||
|
||||||
Typically, the code should be rewritten to remove the `super` calls entirely: | ||||||
|
||||||
```java | ||||||
private final int id; | ||||||
|
||||||
@Override | ||||||
public boolean equals(Object obj) { | ||||||
if (obj instanceof Foo) { | ||||||
return id == ((Foo) obj).id; | ||||||
} | ||||||
return obj; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public int hashCode() { | ||||||
return id; | ||||||
} | ||||||
``` | ||||||
|
||||||
Note that the suggested edits for this check instead preserve behavior, which | ||||||
likely means preserving bugs! However, in cases in which object identity *is* | ||||||
intended, we recommend applying the suggested edit to make that behavior | ||||||
explicit in the code: | ||||||
|
||||||
```java | ||||||
// This class's definition of equality is unusual and perhaps not ideal. | ||||||
// But it is at least explicit. | ||||||
|
||||||
private final Integer id; | ||||||
|
||||||
@Override | ||||||
public boolean equals(Object obj) { | ||||||
if (obj instanceof Foo) { | ||||||
if (id == null) { | ||||||
return this == obj; | ||||||
} | ||||||
return id.equals(((Foo) obj).id); | ||||||
} | ||||||
return obj; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
|
||||||
@Override | ||||||
public int hashCode() { | ||||||
return id != null ? id : System.identityHashCode(this); | ||||||
} | ||||||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.