diffplug / diffplug/spotless

expandWildcardImports: necessary imports in method call expressions removed

Aperta
#2,833 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
5.6k
Fork
559
Merge medio
1g 14h
PR unite (30g)
43

Descrizione

## 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);
}
});
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia in lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java intorno alle righe 181-188 e riproduci l'esempio Java mostrato. Controlla la gestione dell'espansione dei wildcard per gli scope delle chiamate di metodo, quindi verifica che sia JSONObject sia JSON siano ancora importati e che il codice formattato venga compilato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
java
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
52/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.