From 7f14d4a37580372e2ae4cb450363b0edf9ffbd1e Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Tue, 10 Sep 2024 12:04:52 +0200 Subject: [PATCH] `RemoveUnusedImports` should retain explicit import when there are conflicting classes in the same package (#4482) * Add testcase * Apply feedback * Apply feedback: * fix whitespace * clearer test method name * clearer class name * Apply suggestions from code review --------- Co-authored-by: Tim te Beek --- .../java/RemoveUnusedImportsTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java index cae81b2e3b7..f269a88cc19 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/RemoveUnusedImportsTest.java @@ -1983,4 +1983,56 @@ public void foo() { ) ); } + + @Test + void retainExplicitImportWhenConflictingClassInSamePackage() { + rewriteRun( + java( + """ + package com.a; + + class ConflictingClass { + } + """, + SourceSpec::skip + ), + java( + """ + package com.b; + + public class ConflictingClass { + public ConflictingClass() { + } + } + """, + SourceSpec::skip + ), + java( + """ + package com.c; + + import com.b.ConflictingClass; + + public class ImplProvider { + static ConflictingClass getImpl(){ + return new ConflictingClass(); + } + } + """, + SourceSpec::skip + ), + java( + """ + package com.a; + + import com.b.ConflictingClass; + import com.c.ImplProvider; + + class CImpl { + ConflictingClass impl = ImplProvider.getImpl(); + } + """ + ) + ); + } }