PreferJavaUtilObjectsEquals does not check for existing equals method · Issue #434 · openrewrite/rewrite-migrate-java · GitHub
Skip to content

PreferJavaUtilObjectsEquals does not check for existing equals method #434

Open

Description

@timo-abele

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.

Activity

  1. timtebeek commented on May 6, 2024

    @timtebeek
    Member

    Hi! Thanks for the report; I'm looking at your samples and how we've implemented this recipe

    With the example above I see one equals(Object) method and an import of an equals(Object, Object) method, that I think would not clash here because of the differing number of arguments; Does your experience there differ?

  2. timo-abele commented on May 6, 2024

    @timo-abele
    Author

    I found the original code again! The number of arguments does seem to be an issue for Java, the (IntelliJ) error message is "Expected 1 arguments but found 2", so I assume java cannot handle the same method name, defined in a class and statically imported from somewhere else with a different number of parameters.
    Maven error is:

    [ERROR] /C:/Users/.../A.java:[63,16] method equals in class com.myorg.A cannot be applied to given types;
      required: java.lang.Object
      found: com.myorg.Foo,com.myorg.Foo
      reason: actual and formal argument lists differ in length
    

    I've observed this in the original code with a custom type (Foo) and Strings. I hope this helps, let me know if you can't reproduce this.

  3. moved this to Backlog in OpenRewriteon Aug 9, 2024
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

    bugSomething isn't workingguava

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions