diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java index 5988de0515b..85e6f79bfd7 100755 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/OrderImportsTest.java @@ -22,14 +22,12 @@ import org.openrewrite.style.NamedStyles; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.SourceSpec; import static java.util.Collections.emptySet; import static java.util.Collections.singletonList; import static org.openrewrite.Tree.randomId; -import static org.openrewrite.java.Assertions.addTypesToSourceSet; -import static org.openrewrite.java.Assertions.java; -import static org.openrewrite.java.Assertions.srcMainJava; -import static org.openrewrite.java.Assertions.version; +import static org.openrewrite.java.Assertions.*; class OrderImportsTest implements RewriteTest { @@ -53,7 +51,7 @@ void multipleClassesWithTheSameNameButDifferentPackages() { """ import java.awt.List; import java.util.List; - + class Test {} """ ) @@ -87,9 +85,9 @@ void blankLinesNotFollowedByBlockArentAdded() { java( """ import java.util.List; - + import static java.util.Collections.*; - + class A {} """ ) @@ -133,7 +131,7 @@ void unfoldStar() { java( """ import java.util.*; - + class A { List list; List list2; @@ -141,7 +139,7 @@ class A { """, """ import java.util.List; - + class A { List list; List list2; @@ -158,7 +156,7 @@ void unfoldStarMultiple() { java( """ import java.util.*; - + class A { List list; Map map; @@ -167,7 +165,7 @@ class A { """ import java.util.List; import java.util.Map; - + class A { List list; Map map; @@ -196,18 +194,18 @@ void unfoldStaticStar() { java( """ import java.util.List; - + import static java.util.Collections.*; - + class A { List list = emptyList(); } """, """ import java.util.List; - + import static java.util.Collections.emptyList; - + class A { List list = emptyList(); } @@ -270,25 +268,25 @@ void springCloudFormat() { import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.zip.GZIPOutputStream; - + import javax.servlet.ReadListener; import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; - + import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.logging.Log; import reactor.core.publisher.Mono; - + import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.HttpHeaders; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.server.ServerWebExchange; - + import static java.util.Arrays.stream; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.toAsyncPredicate; - + class A {} """ ) @@ -340,7 +338,7 @@ void preservesStaticStarImportWhenRemovingUnused() { java( """ import static java.util.Collections.*; - + class Test { Object[] o = new Object[] { emptyList(), emptyMap(), emptySet() }; } @@ -362,7 +360,7 @@ public class MyCollections extends java.util.Collections {} java( """ import static my.MyCollections.*; - + class Test { Object[] o = new Object[] { emptyList(), emptyMap(), emptySet() }; } @@ -378,7 +376,7 @@ void preservesStaticMethodArguments() { java( """ import static java.util.Collections.*; - + class Test { Object[] o = new Object[] { emptyList(), emptyMap(), emptySet() }; } @@ -470,9 +468,9 @@ class Test { """, """ import static java.util.Collections.singletonList; - + import java.util.List; - + class Test { } """ @@ -510,10 +508,10 @@ void detectBlockPattern() { // org.slf4j should be detected as a block pattern, and not be moved to all other imports. import org.slf4j.Logger; import org.slf4j.LoggerFactory; - + import java.util.Arrays; import java.util.List; - + public class C { } """ @@ -554,14 +552,14 @@ void doNotFoldImports() { import java.util.HashSet; import java.util.List; import java.util.Set; - + import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; - + public class C { } """ @@ -668,12 +666,12 @@ void foldPackageWithExistingImports() { singletonList( new NamedStyles( randomId(), "test", "test", "test", emptySet(), singletonList( - ImportLayoutStyle.builder() - .packageToFold("java.util.*", false) - .importAllOthers() - .importStaticAllOthers() - .build() - ) + ImportLayoutStyle.builder() + .packageToFold("java.util.*", false) + .importAllOthers() + .importStaticAllOthers() + .build() + ) ) ))), java( @@ -686,4 +684,44 @@ void foldPackageWithExistingImports() { ) ); } + + @Test + void nestedInnerClass() { + // language=java + rewriteRun( + spec -> spec.recipe(new OrderImports(true)), + java( + """ + import org.openrewrite.java.tree.JContainer; + import org.openrewrite.java.tree.JLeftPadded; + import org.openrewrite.java.tree.JRightPadded; + import org.openrewrite.java.tree.JavaType; + import org.openrewrite.java.tree.JavaType.Variable; + import org.openrewrite.java.tree.Space; + + public class Foo { + Variable myVariable = null; + JLeftPadded myLeftPadded = null; + JRightPadded myRightPadded = null; + JContainer myContainer = null; + Space mySpace = null; + JavaType.Variable myVariable = null; + } + """, + """ + import org.openrewrite.java.tree.*; + import org.openrewrite.java.tree.JavaType.Variable; + + public class Foo { + Variable myVariable = null; + JLeftPadded myLeftPadded = null; + JRightPadded myRightPadded = null; + JContainer myContainer = null; + Space mySpace = null; + JavaType.Variable myVariable = null; + } + """ + ) + ); + } } 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 f269a88cc19..686b4ade52c 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 @@ -2035,4 +2035,37 @@ class CImpl { ) ); } + + @Test + void staticNestedInnerClass() { + // language=java + rewriteRun( + java( + """ + package a; + + public class A { + public final class B {} + public static class C {} + } + """, + SourceSpec::skip), + java( + """ + import a.*; + + public class Foo { + A method(A.B ab, A.C ac) {} + } + """, + """ + import a.A; + + public class Foo { + A method(A.B ab, A.C ac) {} + } + """ + ) + ); + } } 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 86e99d5f313..10b2a19e74e 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java @@ -106,12 +106,20 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon for (JavaType typeParameter : parameterized.getTypeParameters()) { JavaType.FullyQualified fq = TypeUtils.asFullyQualified(typeParameter); if (fq != null) { - typesByPackage.computeIfAbsent(fq.getPackageName(), f -> new HashSet<>()).add(fq); + typesByPackage.computeIfAbsent( + fq.getOwningClass() == null ? + fq.getPackageName() : + toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), + f -> new HashSet<>()).add(fq); } } } else if (javaType instanceof JavaType.FullyQualified) { JavaType.FullyQualified fq = (JavaType.FullyQualified) javaType; - typesByPackage.computeIfAbsent(fq.getPackageName(), f -> new HashSet<>()).add(fq); + typesByPackage.computeIfAbsent( + fq.getOwningClass() == null ? + fq.getPackageName() : + toFullyQualifiedName(fq.getOwningClass().getFullyQualifiedName()), + f -> new HashSet<>()).add(fq); } } @@ -152,7 +160,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon SortedSet targetMethodsAndFields = methodsAndFieldsByTypeName.get(modifiedTarget); Set staticClasses = null; - for (JavaType.FullyQualified maybeStatic : typesByPackage.getOrDefault(elem.getPackageName(), emptySet())) { + for (JavaType.FullyQualified maybeStatic : typesByPackage.getOrDefault(target, emptySet())) { if (maybeStatic.getOwningClass() != null && outerType.startsWith(maybeStatic.getOwningClass().getFullyQualifiedName())) { if (staticClasses == null) { staticClasses = new HashSet<>(); @@ -207,8 +215,9 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon changed = true; } } else { - Set types = typesByPackage.getOrDefault(elem.getPackageName(), new HashSet<>()); - Set typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(elem.getPackageName()), new HashSet<>()); + String target = qualid.getTarget().toString(); + Set types = typesByPackage.getOrDefault(target, new HashSet<>()); + Set typesByFullyQualifiedClassPath = typesByPackage.getOrDefault(toFullyQualifiedName(target), new HashSet<>()); Set combinedTypes = Stream.concat(types.stream(), typesByFullyQualifiedClassPath.stream()) .collect(Collectors.toSet()); JavaType.FullyQualified qualidType = TypeUtils.asFullyQualified(elem.getQualid().getType()); @@ -236,7 +245,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon changed = true; } else { - usedWildcardImports.add(elem.getQualid().getTarget().toString()); + usedWildcardImports.add(target); } } else if (combinedTypes.stream().noneMatch(c -> { if ("*".equals(elem.getQualid().getSimpleName())) { diff --git a/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java b/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java index d662b3403ae..b03fe350d51 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java @@ -788,11 +788,8 @@ private static String packageOrOuterClassName(JRightPadded anImport) { if (anImport.getElement().isStatic()) { return typeName; } else { - String className = anImport.getElement().getClassName(); - if (className.contains("$")) { - return anImport.getElement().getPackageName() + "." + - className.substring(0, className.lastIndexOf('$')) - .replace('$', '.'); + if (typeName.contains("$")) { + return typeName.substring(0, typeName.lastIndexOf('$')).replace('$', '.'); } return anImport.getElement().getPackageName(); }