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

ReplaceConstantWithAnotherConstant not work when import is conflict #5224

Open
ckcd opened this issue Mar 27, 2025 · 0 comments · May be fixed by #5225
Open

ReplaceConstantWithAnotherConstant not work when import is conflict #5224

ckcd opened this issue Mar 27, 2025 · 0 comments · May be fixed by #5225
Assignees
Labels
bug Something isn't working

Comments

@ckcd
Copy link
Contributor

ckcd commented Mar 27, 2025

What version of OpenRewrite are you using?

I am using the latest rewrite (8.48.1)

How are you running OpenRewrite?

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

mvn org.openrewrite.maven:rewrite-maven-plugin:run ...

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

Run this test, it will failed, because currently we didn't consider the import conflict. For this case, already has a foo1.Bar so we cannot add another foo2.Bar

@Test
void shouldFullyQualifyWhenNewTypeIsAmbiguous() {
    rewriteRun(
      spec -> spec.recipe(new ReplaceConstantWithAnotherConstant("foo1.Bar.QUX1", "foo2.Bar.QUX1")),
      java(
        """
          package foo1;

          public class Bar {
              public static final String QUX1 = "QUX1_FROM_FOO1";
              public static final String QUX2 = "QUX1_FROM_FOO2";
          }
          """
      ),
      java(
        """
          package foo2;

          public class Bar {
              public static final String QUX1 = "QUX1_FROM_FOO1";
          }
          """
      ),
      java(
        """
          import foo1.Bar;

          class Test {
              void a() {
                  System.out.println(Bar.QUX1);
                  System.out.println(Bar.QUX2);
              }
          }
          """,
        """
          import foo1.Bar;

          class Test {
              void a() {
                  System.out.println(foo2.Bar.QUX1);
                  System.out.println(Bar.QUX2);
              }
          }
          """
      )
    );
}

What did you expect to see?

import foo1.Bar;

class Test {
    void a() {
        System.out.println(foo2.Bar.QUX1);
        System.out.println(Bar.QUX2);
    }
}

What did you see instead?

import foo1.Bar;
import foo2.Bar;

class Test {
    void a() {
        System.out.println(Bar.QUX1);
        System.out.println(Bar.QUX2);
    }
}

What is the full stack trace of any errors you encountered?

Unexpected result in "Test.java":
diff --git a/Test.java b/Test.java
index 58f9eb2..67b9d58 100644
--- a/Test.java
+++ b/Test.java
@@ -1,8 +1,9 @@ 
 import foo1.Bar;
+import foo2.Bar;
 
 class Test {
     void a() {
-        System.out.println(foo2.Bar.QUX1);
+        System.out.println(Bar.QUX1);
         System.out.println(Bar.QUX2);
     }
 }
\ No newline at end of file
] 
expected: 
  "import foo1.Bar;
  
  class Test {
      void a() {
          System.out.println(foo2.Bar.QUX1);
          System.out.println(Bar.QUX2);
      }
  }"
 but was: 
  "import foo1.Bar;
  import foo2.Bar;
  
  class Test {
      void a() {
          System.out.println(Bar.QUX1);
          System.out.println(Bar.QUX2);
      }
  }"
<Click to see difference>


	at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:621)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:510)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)
	at org.openrewrite.java.ReplaceConstantWithAnotherConstantTest.shouldFullyQualifyWhenNewTypeIsAmbiguous(ReplaceConstantWithAnotherConstantTest.java:281)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)

Are you interested in contributing a fix to OpenRewrite?

Sure.

@ckcd ckcd added the bug Something isn't working label Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

1 participant