openrewrite / openrewrite/rewrite-migrate-java

PreferJavaUtilObjectsEquals does not check for existing equals method

Open
#434 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug guava
Dominant language
Java
Stars
156
Forks
130
Avg merge
20h 57m
Merged PRs (30d)
21

Description

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

                <plugin>
                    <groupId>org.openrewrite.maven</groupId>
                    <artifactId>rewrite-maven-plugin</artifactId>
                    <version>5.23.1</version>
                    <configuration>
                        <activeRecipes>
                            <recipe>org.openrewrite.java.migrate.guava.NoGuavaJava11</recipe>
                        </activeRecipes>
                        <failOnDryRunResults>true</failOnDryRunResults>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.openrewrite.recipe</groupId>
                            <artifactId>rewrite-migrate-java</artifactId>
                            <version>2.10.0</version>
                        </dependency>
                    </dependencies>
                </plugin>

What is the smallest, simplest way to reproduce the problem?

I haven't compiled this, but I hope the point gets across: the unconditional static import of java.util.Objects.equals is shadowed by the existing equals method

import static com.google.common.base.Objects.equal;

class A {
    int foo;
    int ba;

    @Override
    boolean equals(A other) {
        //omitting the nullchecks
        return equal(foo, other.foo) && equal(ba, other.ba);
    }
}

What did you expect to see?

import java.util.Objects;

class A {
    int foo;
    int ba;

    @Override
    boolean equals(A other) {
        //omitting the nullchecks
        return Objects.equals(foo, other.foo) && Objects.equals(ba, other.ba);
    }
}

What did you see instead?

import static java.util.Objects.equals; //unused!

class A {
    int foo;
    int ba;

    @Override
    boolean equals(A other) {
        //omitting the nullchecks
        return equals(foo, other.foo) && equals(ba, other.ba);
    }
}

Are you interested in contributing a fix to OpenRewrite?

No, just letting you know.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate the PreferJavaUtilObjectsEquals recipe and any related tests, then reproduce the Java snippet from the issue. Done means the migration produces qualified Objects.equals calls without introducing a shadowed or unused static import when an existing equals method is present.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.