-
Notifications
You must be signed in to change notification settings - Fork 451
Closed
Labels
bugSomething isn't workingSomething isn't workingtest providedAlready replicated with a unit test, using JUnit pioneer's ExpectedToFailAlready replicated with a unit test, using JUnit pioneer's ExpectedToFail
Description
What version of OpenRewrite are you using?
org.openrewrite:rewrite-core:8.56.1
What is the smallest, simplest way to reproduce the problem?
When using RemoveUnusedImports and GoogleJavaFormat together with overlapping wildcard imports, you may end up with ambiguous imports, making the code no longer compile.
Here's a test case:
@Test
public void wildcardImports() {
JavaParser.Builder<?, ?> parser =
JavaParser.fromJavaVersion()
.styles(ClasspathLoader.loadStyle("org.openrewrite.java.GoogleJavaFormat"));
rewriteRun(
spec ->
spec.recipe(new RemoveUnusedImports()).parser(parser).expectedCyclesThatMakeChanges(2),
java(
"""
package com.helloworld;
import java.sql.*;
import java.sql.Date;
import java.util.*;
public final class Main {
public static void main(final String... args) {
final Date date = new Date(123);
final java.util.Date date2 = new java.util.Date(123);
}
}
""",
"""
package com.helloworld;
import java.sql.Date;
public final class Main {
public static void main(final String... args) {
final Date date = new Date(123);
final java.util.Date date2 = new java.util.Date(123);
}
}
"""));
}
Resulting in:
org.opentest4j.AssertionFailedError: [Unexpected result in "com/helloworld/Main.java":
diff --git a/com/helloworld/Main.java b/com/helloworld/Main.java
index e2bf8f5..4374a31 100644
--- a/com/helloworld/Main.java
+++ b/com/helloworld/Main.java
@@ -1,6 +1,7 @@
package com.helloworld;
import java.sql.Date;
+import java.util.Date;
public final class Main {
]
expected:
"package com.helloworld;
import java.sql.Date;
public final class Main {
public static void main(final String... args) {
final Date date = new Date(123);
final java.util.Date date2 = new java.util.Date(123);
}
}"
but was:
"package com.helloworld;
import java.sql.Date;
import java.util.Date;
public final class Main {
public static void main(final String... args) {
final Date date = new Date(123);
final java.util.Date date2 = new java.util.Date(123);
}
}"
at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:622)
at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:511)
at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingtest providedAlready replicated with a unit test, using JUnit pioneer's ExpectedToFailAlready replicated with a unit test, using JUnit pioneer's ExpectedToFail
Type
Projects
Status
Done