GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `OverrideEqualsAndHashCodeOnComparableRecipe` · Issue #705 · openrewrite/rewrite-static-analysis

OverrideEqualsAndHashCodeOnComparableRecipe #705

New issue
Copy link
New issue
Copy link
Closed
Closed
OverrideEqualsAndHashCodeOnComparableRecipe#705
Copy link

Description

@Pankraz76
Pankraz76
opened on Aug 9, 2025
Issue body actions

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.

  • [java] Fix #5837: New Rule: OverrideBothEqualsAndHashCodeOnComparable pmd/pmd#5856

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

Are you interested in contributing this recipe to OpenRewrite?

Activity

  1. moderne-meeseeks
    added this to OpenRewriteon Aug 9, 2025
  2. Pankraz76 commented on Aug 9, 2025

    @Pankraz76
    Pankraz76
    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?

  3. timtebeek commented on Aug 9, 2025

    @timtebeek
    timtebeek
    on Aug 9, 2025
    Member
    More actions

    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?

  4. timtebeek
    added
    questionFurther information is requested
    on Aug 9, 2025
  5. zbynek commented on Aug 9, 2025

    @zbynek
    zbynek
    on Aug 9, 2025
    More actions

    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.

  6. Pankraz76 commented on Aug 10, 2025

    @Pankraz76
    Pankraz76
    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?
  7. zbynek commented on Aug 10, 2025

    @zbynek
    zbynek
    on Aug 10, 2025
    More actions

    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).

  8. Pankraz76 commented on Aug 10, 2025

    @Pankraz76
    Pankraz76
    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.

  9. github-actions
    removed
    questionFurther information is requested
    on Aug 11, 2025
  10. timtebeek
    closed this as completedon Aug 16, 2025
  11. github-project-automation
    moved this to Done in OpenRewriteon Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • OpenRewrite
      Status
      Done
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.