From 22369816c82e485974bdd80b4885af2d85003ab2 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Tue, 10 Sep 2024 11:10:53 +0200 Subject: [PATCH] `RemoveUnusedImports` should not remove imports for used nested classes (#4479) * Add testcases for nested imports * Apply suggestions from code review * Move the fix to RemoveUnusedImports recipe * Revert javadoc * Remove unnecessary cast --------- Co-authored-by: Tim te Beek --- .../java/RemoveUnusedImportsTest.java | 77 +++++++++++++++++++ .../openrewrite/java/RemoveUnusedImports.java | 4 +- 2 files changed, 79 insertions(+), 2 deletions(-) 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 70637a63134..cae81b2e3b7 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 @@ -1906,4 +1906,81 @@ public class Bar { ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/3283") + @Test + void nestedImport() { + rewriteRun( + java( + """ + package foo; + + public interface J { + final class One implements J {} + final class Two implements J {} + final class Three implements J {} + final class Four implements J {} + final class Five implements J {} + final class Six implements J {} + } + """ + ), + java( + """ + package bar; + + import foo.J; + import foo.J.*; + + class Quz { + void test() { + J j = null; + One one = null; + Two two = null; + Three three = null; + Four four = null; + Five five = null; + Six six = null; + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite/issues/3283") + @Test + void nestedImportStaticInnerClass() { + rewriteRun( + java( + """ + package com.a.b.c; + public final class ParentBaseClass { + public static abstract class BaseImplClass { + void foo() { + } + } + } + """ + ), + java( + """ + import com.a.b.c.ParentBaseClass.*; + public class MyClass extends BaseImplClass { + @Override + public void foo() { + } + } + """, + """ + import com.a.b.c.ParentBaseClass.BaseImplClass; + public class MyClass extends BaseImplClass { + @Override + public void foo() { + } + } + """ + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java index 856ca707f2f..86e99d5f313 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java @@ -226,7 +226,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon // add each unfolded import combinedTypes.stream().map(JavaType.FullyQualified::getClassName).sorted().distinct().forEach(type -> anImport.imports.add(new JRightPadded<>(elem - .withQualid(qualid.withName(name.withSimpleName(type))) + .withQualid(qualid.withName(name.withSimpleName(type.substring(type.lastIndexOf('.') + 1)))) .withPrefix(Space.format("\n")), Space.EMPTY, Markers.EMPTY)) ); @@ -236,7 +236,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon changed = true; } else { - usedWildcardImports.add(elem.getPackageName()); + usedWildcardImports.add(elem.getQualid().getTarget().toString()); } } else if (combinedTypes.stream().noneMatch(c -> { if ("*".equals(elem.getQualid().getSimpleName())) {