diffplug / diffplug/spotless

expandWildcardImports: necessary imports in method call expressions removed

オープン
#2,833 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Java
スター
5.7k
フォーク
561
平均マージ
1日 14時間
マージ済み PR(30日)
43

説明

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

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java の 181~188 行付近から始め、示されている Java の例を再現します。メソッド呼び出しスコープに対するワイルドカード展開の処理を確認し、その後、JSONObject と JSON の両方が引き続きインポートされ、フォーマットされたコードがコンパイルできることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
java
領域
tooling
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
52/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。