diffplug / diffplug/spotless

expandWildcardImports: necessary imports in method call expressions removed

Ouverte
#2,833 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Langage dominant
Java
Étoiles
5.6k
Forks
559
Merge moyen
1 j 14 h
PR mergées (30 j)
43

Description

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

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez dans lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java vers les lignes 181-188 et reproduisez l’exemple Java présenté. Vérifiez la gestion de l’expansion des jokers pour les scopes d’appels de méthode, puis vérifiez que JSONObject et JSON sont toujours importés et que le code formaté compile.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
java
Domaine
tooling
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
Clairement spécifiée
Accessibilité débutants
52/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.