Skip to content
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

UseStaticImport should not import duplicate method #4776

Open
protocol7 opened this issue Dec 12, 2024 · 1 comment
Open

UseStaticImport should not import duplicate method #4776

protocol7 opened this issue Dec 12, 2024 · 1 comment
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@protocol7
Copy link
Contributor

protocol7 commented Dec 12, 2024

What version of OpenRewrite are you using?

  • org.openrewrite:rewrite-core:8.41.3

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

  @Test
  void foo() {
    rewriteRun(
        spec -> spec.recipe(new UseStaticImport("org.hamcrest.Matchers *(..)")),
        // language=java
        java(
            """
            package com.helloworld;

            import static org.hamcrest.core.IsEqual.equalTo;

            public class Foo {
              public static void foo() {
                  equalTo("foo");
                  org.hamcrest.Matchers.equalTo("bar");
              }
            }
            """));
  }

Results in:

1) foo() (com.spotify.bazel.rewrite.recipes.KnownBugsTest)
org.opentest4j.AssertionFailedError: [Expected recipe to complete in 0 cycles, but took at least one more cycle. Between the last two executed cycles there were changes to "com/helloworld/Foo.java"]
expected:
  "package com.helloworld;

  import static org.hamcrest.core.IsEqual.equalTo;

  public class Foo {
    public static void foo() {
        equalTo("foo");
        org.hamcrest.Matchers.equalTo("bar");
    }
  }"
 but was:
  "package com.helloworld;

  import static org.hamcrest.Matchers.equalTo;
  import static org.hamcrest.core.IsEqual.equalTo;

  public class Foo {
    public static void foo() {
        equalTo("foo");
        equalTo("bar");
    }
  }"
	at org.openrewrite.test.LargeSourceSetCheckingExpectedCycles.afterCycle(LargeSourceSetCheckingExpectedCycles.java:97)
	at org.openrewrite.RecipeScheduler.runRecipeCycles(RecipeScheduler.java:98)
	at org.openrewrite.RecipeScheduler.scheduleRun(RecipeScheduler.java:41)
	at org.openrewrite.Recipe.run(Recipe.java:378)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:376)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
...

This then causes compilation to fail with error: reference to equalTo is ambiguous.

Since there is already an equalTo method imported and used, the recipe should not also try to statically import org.hamcrest.Matchers.equalTo. This is similar to

@protocol7 protocol7 added the bug Something isn't working label Dec 12, 2024
@timtebeek
Copy link
Contributor

Thanks for the report @protocol7 ! Indeed likely needs a similar precondition to what you had contributed previously

We had the same use case as the Hamcrest example you used above, but used a different recipe instead:
https://github.com/openrewrite/rewrite-testing-frameworks/blob/433685ea3c32f39df471a26b9b18be840d3bca77/src/main/resources/META-INF/rewrite/hamcrest.yml#L50-L56

  # First change `is(..)` to `Matchers.is(..)` for consistent matching
  - org.openrewrite.java.ChangeMethodTargetToStatic:
      methodPattern: org.hamcrest.core.* *(..)
      fullyQualifiedTargetTypeName: org.hamcrest.Matchers
  - org.openrewrite.java.ChangeMethodTargetToStatic:
      methodPattern: org.hamcrest.collection.* *(..)
      fullyQualifiedTargetTypeName: org.hamcrest.Matchers

Do know that we offer prepackaged recipes to move away from Hamcrest as well, in case that helps you there

  • org.openrewrite.java.testing.hamcrest.MigrateHamcrestToJUnit5
  • org.openrewrite.java.testing.hamcrest.MigrateHamcrestToAssertJ

@timtebeek timtebeek added the good first issue Good for newcomers label Dec 13, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Status: Backlog
Development

No branches or pull requests

2 participants