Skip to content

UseStaticImport should not import duplicate method #4776

Open
@protocol7

Description

@protocol7

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions