Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

expandWildcardImports: necessary imports in method call expressions removed

Open
#2,833 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
java
Domain
tooling

Research direction

Start in lib/src/javaParser/java/com/diffplug/spotless/glue/javaparser/ExpandWildcardsFormatterFunc.java around lines 181-188 and reproduce the shown Java example. Check the wildcard-expansion handling for method-call scopes, then verify that both JSONObject and JSON remain imported and the formatted code compiles.

Written by the indexing model from the issue text.

Description

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:

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:

			n.getScope().ifPresent(s -> {
				ResolvedType type = n.getSymbolResolver().calculateType(n.getScope().get());
				if (type != null && type.isReference()) {
					matchTypeName(importMap, type.asReferenceType().getQualifiedName(), false);
				}
			});
Dominant language
Java
Stars
5.7k
Forks
560
Avg merge
1d 13h
Merged PRs (30d)
43

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from diffplug/spotless

All issues in diffplug/spotless

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.