You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Replace static imports with non-static imports #1379
The more complicated case would be with wildcard imports, in order for that to work we would need access to the compilation classpath which is beyond the scope of most formatters.
Any suggestions on how to handle the wildcard static imports?
you can replace a StaticImportStep that doesn't have the classpath with a new StaticImportStep which does have the classpath
so that should be everything you need. I'm not sure how to make it work with maven, but it's fine to have features which only work for one plugin or another.
It's definitely possible. The trouble is that currently Spotless provides only source code to the formatters. It does not provide compiled jars or the classpath.
Normally people can add a FormatterStep to Spotless and making it work in the build plugins is very little work. In this case, the classpath will have to piped from the build plugin to the FormatterStep, which we have never done before. I know how to do that in the Gradle plugin (shown above), and it can certainly be done in Maven, I just don't know how.
Once someone has done it once, we'll be able to standardize it and make it easier to make other classpath-dependent steps in the future.
I have the classpath access working with Gradle, I just need to implement the FormatterStep logic. Grad school classes started again so I'm a little crunched for time.
Like Ned, I don't have any experience with the Maven API but I have a lot of experience with the Gradle API. If there's someone that knows how to get the classpath in Maven, I'd certainly add that to the PR.
Many projects get harder to work on as they get bigger. Spotless is easier to work on than ever, and one of the reasons why is that we don't require contributors to "fill the matrix". ... Once someone has filled in one square of the formatter/build system matrix, it's easy for interested parties to fill in any empty squares, since you'll now have a working example for every piece needed.
A 2026 status update, since a few things have changed since this was last discussed:
The classpath blocker is solved. The problem described above was that Spotless never handed a formatter step the project's classpath, which you need to know what a bare max(...) or SECONDS refers to. expandWildcardImports added exactly that plumbing to both the Gradle and the Maven plugin in late 2025.
Error Prone doesn't cover this. It has no rule against static imports in general. Its BadImport check (on by default, as a warning) only qualifies a fixed list of generic names such as of, copyOf, valueOf, builder or INSTANCE, because the class name gives the reader context there. It can be pointed at whole classes with -XepOpt:BadImport:BadEnclosingTypes, but that runs as part of compilation, not as a formatter. Google Java Style is fine with single static imports too, so this stays a per-project preference, which seems a good fit for Spotless.
There's a PR now:Add a replaceStaticImports step for Java #3185 adds replaceStaticImports() for Gradle and <replaceStaticImports/> for Maven, with a way to keep some static imports (AssertJ's assertThat, say). It only replaces a static import when it can show the result still compiles and means the same thing; otherwise it leaves the import alone.
Activity
[-]How to replace static imports with non-static imports?[/-][+]Reeplace static imports with non-static imports[/+]nedtwigg commentedon Oct 19, 2022
This is not possible right now, but we'd be happy to merge a PR which added support for this.
[-]Reeplace static imports with non-static imports[/-][+]Replace static imports with non-static imports[/+]Chasson1992 commentedon Dec 30, 2022
@szpigielm I was going to implement this since I would be intersted in using this as well.
I was going to add to the Java extension a function called
replaceStaticImportsto enable this formatting.The basic case is straight forward.
Instead of
Replace with
The more complicated case would be with wildcard imports, in order for that to work we would need access to the compilation classpath which is beyond the scope of most formatters.
Any suggestions on how to handle the wildcard static imports?
nedtwigg commentedon Dec 30, 2022
In this place
spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java
Lines 275 to 294 in 8d1467f
You have:
StaticImportStepthat doesn't have the classpath with a newStaticImportStepwhich does have the classpathso that should be everything you need. I'm not sure how to make it work with maven, but it's fine to have features which only work for one plugin or another.
agreedSkiing commentedon Jan 27, 2023
Would this be possible to do in the maven plugin if the dependecy for org.eclipse.jdt.ls.core is included since it has a helper method that does this? https://github.com/eclipse/eclipse.jdt.ls/blob/master/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/cleanup/StaticAccessUsesClassNameCleanUp.java
It atleast works in VSCode when having the qualifyStaticMembers identifier set in the redhat.java VSCode plugin
to
agreedSkiing commentedon Jan 27, 2023
Or as comment from jdbuncan suggested #1532 (comment) OpenRewrite https://docs.openrewrite.org/reference/recipes/java/nostaticimport
nedtwigg commentedon Jan 27, 2023
It's definitely possible. The trouble is that currently Spotless provides only source code to the formatters. It does not provide compiled jars or the classpath.
Normally people can add a FormatterStep to Spotless and making it work in the build plugins is very little work. In this case, the classpath will have to piped from the build plugin to the FormatterStep, which we have never done before. I know how to do that in the Gradle plugin (shown above), and it can certainly be done in Maven, I just don't know how.
Once someone has done it once, we'll be able to standardize it and make it easier to make other classpath-dependent steps in the future.
Chasson1992 commentedon Jan 27, 2023
I have the classpath access working with Gradle, I just need to implement the FormatterStep logic. Grad school classes started again so I'm a little crunched for time.
Like Ned, I don't have any experience with the Maven API but I have a lot of experience with the Gradle API. If there's someone that knows how to get the classpath in Maven, I'd certainly add that to the PR.
nedtwigg commentedon Jan 27, 2023
Amazing @Chasson1992, thanks for getting something going! Don't worry about the Maven stuff at all, that should be a separate PR. From our root
README.mdWhy are there empty squares?patpatpat123 commentedon Nov 16, 2026
A 2026 status update, since a few things have changed since this was last discussed:
max(...)orSECONDSrefers to.expandWildcardImportsadded exactly that plumbing to both the Gradle and the Maven plugin in late 2025.BadImportcheck (on by default, as a warning) only qualifies a fixed list of generic names such asof,copyOf,valueOf,builderorINSTANCE, because the class name gives the reader context there. It can be pointed at whole classes with-XepOpt:BadImport:BadEnclosingTypes, but that runs as part of compilation, not as a formatter. Google Java Style is fine with single static imports too, so this stays a per-project preference, which seems a good fit for Spotless.replaceStaticImports()for Gradle and<replaceStaticImports/>for Maven, with a way to keep some static imports (AssertJ'sassertThat, say). It only replaces a static import when it can show the result still compiles and means the same thing; otherwise it leaves the import alone.Feedback on the PR is very welcome!