From 7c958965924ac87d251526a8b60359b10fba1a2c Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Tue, 27 Aug 2024 14:18:05 +0200 Subject: [PATCH 1/6] Test case that shows static import is removed leaving ambiguous reference behind --- .../java/RemoveUnusedImportsTest.java | 49 +++++++++++++++++++ 1 file changed, 49 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 a23297e0cb4..fa965eb9aeb 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 @@ -1814,4 +1814,53 @@ public abstract class Foo2 { ) ); } + + @Test + void staticAmbiguousImport() { + // language=java + rewriteRun( + java( + """ + package org.a; + public class ABC { + public static String A = "A"; + public static String B = "B"; + public static String C = "C"; + public static String ALL = "%s%s%s".formatted(A, B, C); + } + """ + ), + java( + """ + package org.b; + public class DEF { + public static String D = "D"; + public static String E = "E"; + public static String F = "F"; + public static String ALL = "%s%s%s".formatted(D, E, F); + } + """ + ), + java( + """ + package org.test; + + import static org.a.ABC.*; + import static org.b.DEF.*; + import static org.a.ABC.ALL; + + public class Test { + private String abc = ALL; + private String a = A; + private String b = B; + private String c = C; + private String d = D; + private String e = E; + private String f = F; + } + """ + ) + ); + } + } From f425e0f18bdb21a220e01e5a8df5d43abae43676 Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Thu, 29 Aug 2024 15:31:37 +0200 Subject: [PATCH 2/6] Prevent imports of static members from being removed when this causes ambiguous references --- .../openrewrite/java/RemoveUnusedImports.java | 11 +++++++- .../openrewrite/java/internal/TypesInUse.java | 26 ++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) 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 2055b2bc251..c185dbd6cac 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java @@ -256,7 +256,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon J.Import elem = anImport.imports.get(0).getElement(); if (!"*".equals(elem.getQualid().getSimpleName())) { if (elem.isStatic()) { - if (usedStaticWildcardImports.contains(elem.getTypeName())) { + if (usedStaticWildcardImports.contains(elem.getTypeName()) && !becomesAmbiguous(elem, typesByPackage)) { anImport.used = false; changed = true; } @@ -298,6 +298,15 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon return cu; } + private static boolean becomesAmbiguous(J.Import imprt, Map> typesByPackage) { + return typesByPackage.entrySet().stream() + .filter(entry -> !entry.getKey().equals(imprt.getPackageName())) + .flatMap(entry -> entry.getValue().stream()) + .flatMap(fq -> fq.getMembers().stream()) + .filter(member -> member.getFlags().contains(Flag.Static)) + .anyMatch(member -> member.getName().equals(imprt.getQualid().getSimpleName())); + } + private static final Set JAVA_LANG_CLASS_NAMES = new HashSet<>(Arrays.asList( "AbstractMethodError", "Appendable", diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java index ceb3d78773f..2c5bde1df63 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java @@ -21,6 +21,7 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.tree.Flag; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.JavaType; @@ -42,7 +43,7 @@ public class TypesInUse { public static TypesInUse build(JavaSourceFile cu) { FindTypesInUse findTypesInUse = new FindTypesInUse(); - findTypesInUse.visit(cu, 0); + findTypesInUse.visit(cu, cu); return new TypesInUse(cu, findTypesInUse.getTypes(), findTypesInUse.getDeclaredMethods(), @@ -51,19 +52,19 @@ public static TypesInUse build(JavaSourceFile cu) { } @Getter - public static class FindTypesInUse extends JavaIsoVisitor { + public static class FindTypesInUse extends JavaIsoVisitor { private final Set types = newSetFromMap(new IdentityHashMap<>()); private final Set declaredMethods = newSetFromMap(new IdentityHashMap<>()); private final Set usedMethods = newSetFromMap(new IdentityHashMap<>()); private final Set variables = newSetFromMap(new IdentityHashMap<>()); @Override - public J.Import visitImport(J.Import _import, Integer p) { + public J.Import visitImport(J.Import _import, JavaSourceFile cu) { return _import; } @Override - public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { + public J.Identifier visitIdentifier(J.Identifier identifier, JavaSourceFile cu) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class @@ -72,15 +73,26 @@ public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { // skip method name return identifier; } - return super.visitIdentifier(identifier, p); + return super.visitIdentifier(identifier, cu); } @Override - public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { + public @Nullable JavaType visitType(@Nullable JavaType javaType, JavaSourceFile cu) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { - variables.add((JavaType.Variable) javaType); + JavaType.Variable jType = (JavaType.Variable) javaType; + variables.add(jType); + if (jType.getOwner() != null && jType.getOwner() instanceof JavaType.Class) { + JavaType.Class owner = (JavaType.Class) jType.getOwner(); + String ownerPackage = owner.getFullyQualifiedName().substring(0, owner.getFullyQualifiedName().lastIndexOf(".")); + // If we're accessing a variable that has the static flag and is not owned by the + // CompilationUnit we are visiting we should add the owning class of the variable as a used type + if (jType.getFlags().contains(Flag.Static) + && cu.getPackageDeclaration() != null && !ownerPackage.equals(cu.getPackageDeclaration().getPackageName())) { + types.add(jType.getOwner()); + } + } } else if (javaType instanceof JavaType.Method) { if (cursor.getValue() instanceof J.MethodDeclaration) { declaredMethods.add((JavaType.Method) javaType); From 93b3fcd54774ae0cec6318936ff59faf8d79d0ee Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Mon, 2 Sep 2024 15:54:03 +0200 Subject: [PATCH 3/6] First small fix --- .../java/org/openrewrite/java/internal/TypesInUse.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java index 2c5bde1df63..edaa3b6d785 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java @@ -85,7 +85,13 @@ public J.Identifier visitIdentifier(J.Identifier identifier, JavaSourceFile cu) variables.add(jType); if (jType.getOwner() != null && jType.getOwner() instanceof JavaType.Class) { JavaType.Class owner = (JavaType.Class) jType.getOwner(); - String ownerPackage = owner.getFullyQualifiedName().substring(0, owner.getFullyQualifiedName().lastIndexOf(".")); + String ownerPackage; + if (owner.getFullyQualifiedName().contains(".")) { + ownerPackage = owner.getFullyQualifiedName().substring(0, owner.getFullyQualifiedName().lastIndexOf(".")); + } else { + ownerPackage = owner.getFullyQualifiedName(); + } + // If we're accessing a variable that has the static flag and is not owned by the // CompilationUnit we are visiting we should add the owning class of the variable as a used type if (jType.getFlags().contains(Flag.Static) From ac3484eb10f42380e5e696878728c8575ee1ee0d Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Tue, 3 Sep 2024 09:57:41 +0200 Subject: [PATCH 4/6] Destream and invert --- .../openrewrite/java/RemoveUnusedImports.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) 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 c185dbd6cac..f2195fe2002 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java @@ -256,7 +256,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon J.Import elem = anImport.imports.get(0).getElement(); if (!"*".equals(elem.getQualid().getSimpleName())) { if (elem.isStatic()) { - if (usedStaticWildcardImports.contains(elem.getTypeName()) && !becomesAmbiguous(elem, typesByPackage)) { + if (usedStaticWildcardImports.contains(elem.getTypeName()) && doesNotResolveFromOtherType(elem, typesByPackage)) { anImport.used = false; changed = true; } @@ -298,13 +298,21 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon return cu; } - private static boolean becomesAmbiguous(J.Import imprt, Map> typesByPackage) { - return typesByPackage.entrySet().stream() - .filter(entry -> !entry.getKey().equals(imprt.getPackageName())) - .flatMap(entry -> entry.getValue().stream()) - .flatMap(fq -> fq.getMembers().stream()) - .filter(member -> member.getFlags().contains(Flag.Static)) - .anyMatch(member -> member.getName().equals(imprt.getQualid().getSimpleName())); + private static boolean doesNotResolveFromOtherType(J.Import imprt, Map> typesByPackage) { + for (Map.Entry> entry : typesByPackage.entrySet()) { + if (!entry.getKey().equals(imprt.getPackageName())) { + for (JavaType.FullyQualified fq : entry.getValue()) { + for (JavaType.Variable member : fq.getMembers()) { + if (member.getFlags().contains(Flag.Static)) { + if (member.getName().equals(imprt.getQualid().getSimpleName())) { + return false; + } + } + } + } + } + } + return true; } private static final Set JAVA_LANG_CLASS_NAMES = new HashSet<>(Arrays.asList( From ea73d4fd7502abc4afd4446aaa24f2ddee0084df Mon Sep 17 00:00:00 2001 From: Laurens Westerlaken Date: Thu, 5 Sep 2024 10:23:44 +0200 Subject: [PATCH 5/6] Fixed final test --- .../org/openrewrite/java/internal/TypesInUse.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java index edaa3b6d785..385bef4e2d8 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java @@ -52,19 +52,19 @@ public static TypesInUse build(JavaSourceFile cu) { } @Getter - public static class FindTypesInUse extends JavaIsoVisitor { + public static class FindTypesInUse extends JavaIsoVisitor { private final Set types = newSetFromMap(new IdentityHashMap<>()); private final Set declaredMethods = newSetFromMap(new IdentityHashMap<>()); private final Set usedMethods = newSetFromMap(new IdentityHashMap<>()); private final Set variables = newSetFromMap(new IdentityHashMap<>()); @Override - public J.Import visitImport(J.Import _import, JavaSourceFile cu) { + public J.Import visitImport(J.Import _import, J cu) { return _import; } @Override - public J.Identifier visitIdentifier(J.Identifier identifier, JavaSourceFile cu) { + public J.Identifier visitIdentifier(J.Identifier identifier, J cu) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class @@ -77,7 +77,7 @@ public J.Identifier visitIdentifier(J.Identifier identifier, JavaSourceFile cu) } @Override - public @Nullable JavaType visitType(@Nullable JavaType javaType, JavaSourceFile cu) { + public @Nullable JavaType visitType(@Nullable JavaType javaType, J cu) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { @@ -92,10 +92,11 @@ public J.Identifier visitIdentifier(J.Identifier identifier, JavaSourceFile cu) ownerPackage = owner.getFullyQualifiedName(); } + JavaSourceFile jsf = (JavaSourceFile) cu; // If we're accessing a variable that has the static flag and is not owned by the // CompilationUnit we are visiting we should add the owning class of the variable as a used type if (jType.getFlags().contains(Flag.Static) - && cu.getPackageDeclaration() != null && !ownerPackage.equals(cu.getPackageDeclaration().getPackageName())) { + && jsf.getPackageDeclaration() != null && !ownerPackage.equals(jsf.getPackageDeclaration().getPackageName())) { types.add(jType.getOwner()); } } From ff379fa8a33117b319103fca5c1022a785cd1b63 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 6 Sep 2024 18:47:34 +0200 Subject: [PATCH 6/6] Revert change to TypeInUse and detect from variables only --- .../java/RemoveUnusedImportsTest.java | 67 +++++++++++++++---- .../openrewrite/java/RemoveUnusedImports.java | 45 ++++++++----- .../openrewrite/java/internal/TypesInUse.java | 33 ++------- 3 files changed, 90 insertions(+), 55 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 fa965eb9aeb..70637a63134 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 @@ -22,6 +22,7 @@ import org.openrewrite.style.NamedStyles; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; +import org.openrewrite.test.SourceSpec; import org.openrewrite.test.TypeValidation; import static java.util.Collections.emptySet; @@ -1380,22 +1381,24 @@ void removeMultipleWildcardImports() { import java.util.*; import static java.util.Collections.emptyList; + import static java.util.Collections.singletonList; class A { Collection c = emptyList(); Set s = new HashSet<>(); - List l = singletonList("a","b","c"); + List l = singletonList("a"); } """, """ import java.util.*; import static java.util.Collections.emptyList; + import static java.util.Collections.singletonList; class A { Collection c = emptyList(); Set s = new HashSet<>(); - List l = singletonList("a","b","c"); + List l = singletonList("a"); } """ ) @@ -1449,7 +1452,7 @@ void removeDirectImportWithWildcardImport() { class A { Collection c = emptyList(); Set s = emptySet(); - List l = singletonList("c","b","a"); + List l = singletonList("c"); Iterator i = emptyIterator(); } """, @@ -1461,7 +1464,7 @@ class A { class A { Collection c = emptyList(); Set s = emptySet(); - List l = singletonList("c","b","a"); + List l = singletonList("c"); Iterator i = emptyIterator(); } """ @@ -1822,34 +1825,75 @@ void staticAmbiguousImport() { java( """ package org.a; - public class ABC { + public class Abc { public static String A = "A"; public static String B = "B"; public static String C = "C"; public static String ALL = "%s%s%s".formatted(A, B, C); } - """ + """, + SourceSpec::skip ), java( """ package org.b; - public class DEF { + public class Def { public static String D = "D"; public static String E = "E"; public static String F = "F"; public static String ALL = "%s%s%s".formatted(D, E, F); } + """, + SourceSpec::skip + ), + // No change when removal would cause ambiguity + java( + """ + package org.test; + + import static org.a.Abc.*; + import static org.a.Abc.ALL; + import static org.b.Def.*; + + public class Foo { + private String abc = ALL; + private String a = A; + private String b = B; + private String c = C; + private String d = D; + private String e = E; + private String f = F; + } """ ), + // Do still remove unambiguous imports java( """ package org.test; - import static org.a.ABC.*; - import static org.b.DEF.*; - import static org.a.ABC.ALL; + import static org.a.Abc.*; + import static org.a.Abc.A; + import static org.a.Abc.ALL; + import static org.b.Def.*; - public class Test { + public class Bar { + private String abc = ALL; + private String a = A; + private String b = B; + private String c = C; + private String d = D; + private String e = E; + private String f = F; + } + """, + """ + package org.test; + + import static org.a.Abc.*; + import static org.a.Abc.ALL; + import static org.b.Def.*; + + public class Bar { private String abc = ALL; private String a = A; private String b = B; @@ -1862,5 +1906,4 @@ public class Test { ) ); } - } 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 f2195fe2002..856ca707f2f 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveUnusedImports.java @@ -169,7 +169,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon anImport.used = true; usedStaticWildcardImports.add(elem.getTypeName()); } else if (((methodsAndFields == null ? 0 : methodsAndFields.size()) + - (staticClasses == null ? 0 : staticClasses.size())) < layoutStyle.getNameCountToUseStarImport()) { + (staticClasses == null ? 0 : staticClasses.size())) < layoutStyle.getNameCountToUseStarImport()) { // replacing the star with a series of unfolded imports anImport.imports.clear(); @@ -199,8 +199,8 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon usedStaticWildcardImports.add(elem.getTypeName()); } } else if (staticClasses != null && staticClasses.stream().anyMatch(c -> elem.getTypeName().equals(c.getFullyQualifiedName())) || - (methodsAndFields != null && methodsAndFields.contains(qualid.getSimpleName())) || - (targetMethodsAndFields != null && targetMethodsAndFields.contains(qualid.getSimpleName()))) { + (methodsAndFields != null && methodsAndFields.contains(qualid.getSimpleName())) || + (targetMethodsAndFields != null && targetMethodsAndFields.contains(qualid.getSimpleName()))) { anImport.used = true; } else { anImport.used = false; @@ -252,11 +252,13 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon } // Do not use direct imports that are imported by a wildcard import + Set ambiguousStaticImportNames = getAmbiguousStaticImportNames(cu); for (ImportUsage anImport : importUsage) { J.Import elem = anImport.imports.get(0).getElement(); if (!"*".equals(elem.getQualid().getSimpleName())) { if (elem.isStatic()) { - if (usedStaticWildcardImports.contains(elem.getTypeName()) && doesNotResolveFromOtherType(elem, typesByPackage)) { + if (usedStaticWildcardImports.contains(elem.getTypeName()) && + !ambiguousStaticImportNames.contains(elem.getQualid().getSimpleName())) { anImport.used = false; changed = true; } @@ -278,7 +280,7 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon for (int i = 0; i < importGroup.size(); i++) { JRightPadded anImport = importGroup.get(i); if (i == 0 && lastUnusedImportSpace != null && anImport.getElement().getPrefix().getLastWhitespace() - .chars().filter(c -> c == '\n').count() <= 1) { + .chars().filter(c -> c == '\n').count() <= 1) { anImport = anImport.withElement(anImport.getElement().withPrefix(lastUnusedImportSpace)); } imports.add(anImport); @@ -298,21 +300,30 @@ public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionCon return cu; } - private static boolean doesNotResolveFromOtherType(J.Import imprt, Map> typesByPackage) { - for (Map.Entry> entry : typesByPackage.entrySet()) { - if (!entry.getKey().equals(imprt.getPackageName())) { - for (JavaType.FullyQualified fq : entry.getValue()) { - for (JavaType.Variable member : fq.getMembers()) { - if (member.getFlags().contains(Flag.Static)) { - if (member.getName().equals(imprt.getQualid().getSimpleName())) { - return false; - } - } - } + private static Set getAmbiguousStaticImportNames(J.CompilationUnit cu) { + Set typesWithWildcardImport = new HashSet<>(); + for (J.Import elem : cu.getImports()) { + if ("*".equals(elem.getQualid().getSimpleName())) { + typesWithWildcardImport.add(elem.getTypeName()); + } + } + Set qualifiedTypes = new HashSet<>(); + for (JavaType.Variable variable : cu.getTypesInUse().getVariables()) { + JavaType.FullyQualified fq = TypeUtils.asFullyQualified(variable.getOwner()); + if (fq != null && typesWithWildcardImport.contains(fq.getFullyQualifiedName())) { + qualifiedTypes.add(fq); + } + } + Set seen = new HashSet<>(); + Set ambiguous = new HashSet<>(); + for (JavaType.FullyQualified fq : qualifiedTypes) { + for (JavaType.Variable member : fq.getMembers()) { + if (!seen.add(member.getName())) { + ambiguous.add(member.getName()); } } } - return true; + return ambiguous; } private static final Set JAVA_LANG_CLASS_NAMES = new HashSet<>(Arrays.asList( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java index 385bef4e2d8..ceb3d78773f 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/TypesInUse.java @@ -21,7 +21,6 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.tree.Flag; import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaSourceFile; import org.openrewrite.java.tree.JavaType; @@ -43,7 +42,7 @@ public class TypesInUse { public static TypesInUse build(JavaSourceFile cu) { FindTypesInUse findTypesInUse = new FindTypesInUse(); - findTypesInUse.visit(cu, cu); + findTypesInUse.visit(cu, 0); return new TypesInUse(cu, findTypesInUse.getTypes(), findTypesInUse.getDeclaredMethods(), @@ -52,19 +51,19 @@ public static TypesInUse build(JavaSourceFile cu) { } @Getter - public static class FindTypesInUse extends JavaIsoVisitor { + public static class FindTypesInUse extends JavaIsoVisitor { private final Set types = newSetFromMap(new IdentityHashMap<>()); private final Set declaredMethods = newSetFromMap(new IdentityHashMap<>()); private final Set usedMethods = newSetFromMap(new IdentityHashMap<>()); private final Set variables = newSetFromMap(new IdentityHashMap<>()); @Override - public J.Import visitImport(J.Import _import, J cu) { + public J.Import visitImport(J.Import _import, Integer p) { return _import; } @Override - public J.Identifier visitIdentifier(J.Identifier identifier, J cu) { + public J.Identifier visitIdentifier(J.Identifier identifier, Integer p) { Object parent = Objects.requireNonNull(getCursor().getParent()).getValue(); if (parent instanceof J.ClassDeclaration) { // skip type of class @@ -73,33 +72,15 @@ public J.Identifier visitIdentifier(J.Identifier identifier, J cu) { // skip method name return identifier; } - return super.visitIdentifier(identifier, cu); + return super.visitIdentifier(identifier, p); } @Override - public @Nullable JavaType visitType(@Nullable JavaType javaType, J cu) { + public @Nullable JavaType visitType(@Nullable JavaType javaType, Integer p) { if (javaType != null && !(javaType instanceof JavaType.Unknown)) { Cursor cursor = getCursor(); if (javaType instanceof JavaType.Variable) { - JavaType.Variable jType = (JavaType.Variable) javaType; - variables.add(jType); - if (jType.getOwner() != null && jType.getOwner() instanceof JavaType.Class) { - JavaType.Class owner = (JavaType.Class) jType.getOwner(); - String ownerPackage; - if (owner.getFullyQualifiedName().contains(".")) { - ownerPackage = owner.getFullyQualifiedName().substring(0, owner.getFullyQualifiedName().lastIndexOf(".")); - } else { - ownerPackage = owner.getFullyQualifiedName(); - } - - JavaSourceFile jsf = (JavaSourceFile) cu; - // If we're accessing a variable that has the static flag and is not owned by the - // CompilationUnit we are visiting we should add the owning class of the variable as a used type - if (jType.getFlags().contains(Flag.Static) - && jsf.getPackageDeclaration() != null && !ownerPackage.equals(jsf.getPackageDeclaration().getPackageName())) { - types.add(jType.getOwner()); - } - } + variables.add((JavaType.Variable) javaType); } else if (javaType instanceof JavaType.Method) { if (cursor.getValue() instanceof J.MethodDeclaration) { declaredMethods.add((JavaType.Method) javaType);