Issue
The new expandWildcardImports step removes necessary imports in method call expressions, e.g.:
import com.alibaba.fastjson.*;
public class Demo {
public static void main() {
final JSONObject jsonObject = new JSONObject();
final Script inlineScript = JSON.toJSONString(jsonObject);
}
}
becomes
import com.alibaba.fastjson.JSONObject;
public class Demo {
public static void main() {
final JSONObject jsonObject = new JSONObject();
final Script inlineScript = JSON.toJSONString(jsonObject);
}
}
The import import com.alibaba.fastjson.JSON is missing and the code fails to compile.
Java version
openjdk 25.0.1 2025-10-21
OpenJDK Runtime Environment (build 25.0.1+8-Ubuntu-125.10)
OpenJDK 64-Bit Server VM (build 25.0.1+8-Ubuntu-125.10, mixed mode, sharing)
Solution
Code like JSON.toJSONString(jsonObject) seems to be a method call expression:
|
public void visit(final MethodCallExpr n, final Map<ImportDeclaration, Set<ImportDeclaration>> importMap) { |
|
// static imports |
|
ResolvedMethodDeclaration resolved = wrapUnsolvedSymbolException(n, MethodCallExpr::resolve); |
|
if (resolved.isStatic()) { |
|
matchTypeName(importMap, resolved.getQualifiedName(), true); |
|
} |
|
super.visit(n, importMap); |
|
} |
The current code above only checks the method (toJSONString), but not the scope of the method. Adding this resolved the issue for me:
n.getScope().ifPresent(s -> {
ResolvedType type = n.getSymbolResolver().calculateType(n.getScope().get());
if (type != null && type.isReference()) {
matchTypeName(importMap, type.asReferenceType().getQualifiedName(), false);
}
});
Issue
The new
expandWildcardImportsstep removes necessary imports in method call expressions, e.g.:becomes
The import
import com.alibaba.fastjson.JSONis missing and the code fails to compile.Java version
Solution
Code like
JSON.toJSONString(jsonObject)seems to be a method call expression:spotless/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java
Lines 181 to 188 in 8e776ec
The current code above only checks the method (
toJSONString), but not the scope of the method. Adding this resolved the issue for me: