spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Feature Request: Add a FormatterStep to Expand Star Imports

Open HamedTaghani opened this issue 4 months ago • 2 comments

Hello Spotless maintainers, I'm a new potential contributor and I've been exploring the features of Spotless and comparing them to other code quality tools like checkstyle. I'm very impressed with the focus on automated formatting and I would like to propose a new FormatterStep for Java.

The proposed feature would automatically expand star imports (e.g., import java.util.*) into explicit, individual class imports (e.g., import java.util.List;). This aligns well with the Spotless philosophy of automatically fixing common style violations.

Motivation: Many style guides and tools (like Checkstyle's AvoidStarImport check) recommend against star imports for reasons of clarity and avoiding potential naming conflicts.

Implementing this as a FormatterStep would give users a seamless way to enforce this style rule, turning a linter check into an automated fix.

Example of the proposed change: Before:

import java.util.*;

public class MyClass {
    List<String> list = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
}

After:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyClass {
    List<String> list = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
}

I believe this feature would be a valuable addition to Spotless. I am interested in implementing this and submitting a pull request if team thinks it's a worthwhile feature.

Please let me know if this is a feature you would be open to.

HamedTaghani avatar Aug 10 '25 03:08 HamedTaghani

@HamedTaghani Welcome!

This has been explored already in google-java-format, one of our dependencies, and sadly they believe you would need to compile the file and have access to all classpath dependencies to expand wildcard imports into normal ones, which means anyone willing to undertake this task needs to be familiar with Java compilers:

  • https://github.com/google/google-java-format/issues/113#issuecomment-272639684
  • https://github.com/google/google-java-format/issues/956#issuecomment-1675183035
  • https://github.com/diffplug/spotless/issues/2245#issuecomment-2321891763 -> https://github.com/google/google-java-format/issues/1154#issuecomment-2321722479

Furthermore, Spotless would most likely need pervasive changes because when a file is being formatted, it only has access to the file contents and metadata (represented by a File object), IIRC. Furthermore, the Gradle plugin uses incremental file changes, so we may not have access to all the source files needed for compilation. And this would all have to be dealt with without losing performance for our average users.

Anyway, I don't want to stop anyone from trying to implement this, but I thought it was worth mentioning all the known issues to help set expectations and to explain why this hasn't been done before. 😅

jbduncan avatar Aug 10 '25 20:08 jbduncan

Thank you so much for the detailed and thoughtful explanation!

Now I understand why this is a much more complex task than I initially thought 🙂. I agree this feature would not be a suitable starting point for me at this point.

I'll look for a simpler, self-contained feature that fits better with the existing FormatterStep model. Thanks again for your guidance!

HamedTaghani avatar Aug 12 '25 02:08 HamedTaghani