expandWildcardImports: necessary imports in method call expressions removed
- Vorherrschende Sprache
- Java
- Sterne
- 5.6k
- Forks
- 559
- Ø Merge
- 1 T. 14 Std.
- Gemergte PRs (30 T.)
- 43
Beschreibung
## Issue
The new `expandWildcardImports` step removes necessary imports in method call expressions, e.g.:
```java
import com.alibaba.fastjson.*;
public class Demo {
public static void main() {
final JSONObject jsonObject = new JSONObject();
final Script inlineScript = JSON.toJSONString(jsonObject);
}
}
```
becomes
```java
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:
https://github.com/diffplug/spotless/blob/8e776ec835b443b2c7d7e9e662aac268fa270050/lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java#L181-L188
The current code above only checks the method (`toJSONString`), but not the scope of the method. Adding this resolved the issue for me:
```java
n.getScope().ifPresent(s -> {
ResolvedType type = n.getSymbolResolver().calculateType(n.getScope().get());
if (type != null && type.isReference()) {
matchTypeName(importMap, type.asReferenceType().getQualifiedName(), false);
}
});
```
Beitragsleitfaden
Rechercherichtung
Beginne in lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java etwa bei den Zeilen 181–188 und reproduziere das gezeigte Java-Beispiel. Überprüfe die Behandlung der Wildcard-Erweiterung für Method-Call-S Scopes und stelle anschließend sicher, dass sowohl JSONObject als auch JSON weiterhin importiert sind und der formatierte Code kompiliert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- java
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 52/100