Skip to content

Commit bf76643

Browse files
committed
prevent false resolving of wildcard subpackages
1 parent a7e043c commit bf76643

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

lib/src/javaParser/java/com/diffplug/spotless/glue/javaParser/ExpandWildcardsFormatterFunc.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,15 @@ public void visit(final ClassOrInterfaceType n,
135135

136136
private void matchTypeName(Map<ImportDeclaration, Set<ImportDeclaration>> importMap, String qualifiedName,
137137
boolean isStatic) {
138+
int lastDot = qualifiedName.lastIndexOf('.');
139+
if (lastDot < 0) {
140+
return;
141+
}
142+
143+
String packageName = qualifiedName.substring(0, lastDot);
138144
for (var entry : importMap.entrySet()) {
139145
if (entry.getKey().isStatic() == isStatic
140-
&& qualifiedName.startsWith(entry.getKey().getName().asString())) {
146+
&& packageName.equals(entry.getKey().getName().asString())) {
141147
entry.getValue().add(new ImportDeclaration(qualifiedName, isStatic, false));
142148
break;
143149
}

lib/src/main/java/com/diffplug/spotless/java/ExpandWildcardImportsStep.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public State(Collection<File> typeSolverClasspath, JarState jarState) {
7373
FormatterFunc toFormatter() {
7474
try {
7575
Class<?> formatterFunc = jarState.getClassLoader()
76-
.loadClass("com.diffplug.spotless.glue.javaParser.ExpandWildcardsFormatterFunc");
76+
.loadClass("com.diffplug.spotless.glue.javaparser.ExpandWildcardsFormatterFunc");
7777
Constructor<?> constructor = formatterFunc.getConstructor(Collection.class);
7878
return (FormatterFunc) constructor.newInstance(typeSolverClasspath);
7979
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException

testlib/src/main/resources/java/expandwildcardimports/JavaClassWithWildcardsFormatted.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.util.Date;
55
import java.util.List;
66
import java.util.Map;
77
import java.util.Optional;
8+
import java.util.concurrent.Callable;
89
import foo.bar.baz.AnotherImportedClass;
910
import org.example.SomeAnnotation;
1011

@@ -26,7 +27,7 @@ public class JavaClassWithWildcards {
2627
/**
2728
* Some JavaDoc
2829
*/
29-
public Optional<Integer> testMethod() {
30+
public Optional<Integer> testMethod(Callable callable) {
3031
AnotherClassInSamePackage test1 = null;
3132
AnotherImportedClass test2 = null;
3233
return Optional.empty();

testlib/src/main/resources/java/expandwildcardimports/JavaClassWithWildcardsUnformatted.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package foo.bar;
22

33
import java.util.*;
4+
import java.util.concurrent.Callable;
45
import foo.bar.baz.*;
56
import org.example.*;
67
import java.io.*;
@@ -23,7 +24,7 @@ public class JavaClassWithWildcards {
2324
/**
2425
* Some JavaDoc
2526
*/
26-
public Optional<Integer> testMethod() {
27+
public Optional<Integer> testMethod(Callable callable) {
2728
AnotherClassInSamePackage test1 = null;
2829
AnotherImportedClass test2 = null;
2930
return Optional.empty();

0 commit comments

Comments
 (0)