GeezerVison is showing github.com — `OverrideEqualsAndHashCodeOnComparableRecipe` · Issue #705 · openrewrite/rewrite-static-analysis
OverrideEqualsAndHashCodeOnComparableRecipe #705
Description
What problem are you trying to solve?
The issue addresses cases where a class implements Comparable but doesn't override equals() (and potentially hashCode()), which can lead to inconsistent behavior when used with sorted collections. This is related to PMD issue #5837 which proposed a new rule to detect this situation.
The recipe would focus on generating an equals() method that leverages compareTo() when missing, as this is a safe transformation. Generating hashCode() is more complex and may not be suitable for automated implementation.
What precondition(s) should be checked before applying this recipe?
Describe the situation before applying the recipe
public class Bar implements Comparable<Bar> { // poor - missing equals() and hashCode()
public int compareTo(Bar other) {
// some comparison
}
}
public class Baz implements Comparable<Baz> { // poor - missing hashCode()
public int compareTo(Baz other) {
// some comparison
}
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
return compareTo((Baz) o) == 0;
}
}
Describe the situation after applying the recipe
public class Foo implements Comparable<Foo> { // correct
public int compareTo(Foo other) {
// some comparison
}
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
return compareTo((Foo) o) == 0;
}
public int hashCode() {
// return hash code 1Code has comments. Press enter to view.
}
}
Have you considered any alternatives or workarounds?
Any additional context
ActivityPankraz76 commented on Aug 9, 2025 ContributorAuthor More actions assuming this is something we can not fix, as we can not tell for sure how to impl. the eq and hq, right?
@timtebeek
@zbynek
@adangel
@iddeepak
So just a comment or failing the build, whats a good thing to do here? timtebeek commented on Aug 9, 2025 I'm not sure we'd want to synthesize an equals/hashcode implementation here, as that might get complicated quite quickly. For a recipe request to be viable it would need to include the automated steps you're proposing, whereas right now it's light on detail of how you'd go about with implementing those methods. Can you provide those details, or close this issue if there's not guaranteed safe implementation we can do that does not involve the user making any decisions? zbynek commented on Aug 9, 2025 I guess generating equals that uses compareTo like this:
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
return compareTo((Baz) o) == 0;
}
could be helpful for the case with existing compareTo and missing equals, that will make the PMD check at least partially happy. Maybe you can update the issue description to focus on this part? The other part (generating hashCode from either compareTo or equals) is probably not well suited for a recipe. Pankraz76 commented on Aug 10, 2025 ContributorAuthor More actions
I guess generating equals that uses compareTo like this:
Yes eq can be implemented, by leveraging compareTo, but what about hashCode ?
- Failing build to require manual fixup?
- Or giving comment to manually check, as its seen around when the recipes fail to make proper change?
zbynek commented on Aug 10, 2025
Failing build to require manual fixup?
If there's nothing to rewrite, it should be just ignored. There are other tools for detecting problems that the user should fix manually (for this particular violation PMD after pmd/pmd#5856, SpotBugs using EQ_COMPARETO_USE_OBJECT_EQUALS). Pankraz76 commented on Aug 10, 2025 ContributorAuthor More actions
other tools for detecting problems
yes agree.
Assuming some would like to go with one tool, not having to maintain a configuration for special cases like this.
MetadataMetadataStatusShow more project fields
DevelopmentNo branches or pull requestsIssue actions
|