Repository navigation
Add a replaceStaticImports step for Java - #3185
Open
patpatpat123 wants to merge 1 commit into
Open
patpatpat123 wants to merge 1 commit into
patpatpat123 wants to merge 1 commit into
Conversation
Replaces static imports with regular imports and qualified references, so that assertTrue(x) becomes Assertions.assertTrue(x). Available as replaceStaticImports() in Gradle and <replaceStaticImports/> in Maven, with excludes for the static imports to keep. A static import is only replaced when every use of it can be resolved with certainty, using the project classpath and the JLS scoping rules; otherwise it is left as it is. The JavaParser setup that expandWildcardImports uses moves into a small shared helper.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi! This adds
replaceStaticImports, the step #1379 asked for back in 2022. It replaces static imports with regular imports and qualified references:In Gradle it's
replaceStaticImports(), orreplaceStaticImports('org.assertj.core.api.Assertions.*')to keep some static imports. In Maven it's<replaceStaticImports/>, with an optional<excludes>list.Closes #1379.
Why it's doable now
The blocker in the issue was that Spotless never gave a step the project's classpath, which you need to know what a bare
max(...)orSECONDSrefers to.expandWildcardImportsadded that plumbing to both plugins, so this step reuses it.Keeping it safe
A step like this must never produce code that doesn't compile, or that compiles but means something else. So each static import is either replaced completely or left exactly as it is.
The hard part is deciding what a bare name refers to. JavaParser's own resolution isn't reliable enough for that: it picks between imports in source order, misses some pattern variables, keeps
forloop variables in scope after the loop, doesn't see member types inherited from superclasses, and silently skips supertypes it can't resolve. So the step applies the JLS scoping rules itself and mostly asks JavaParser about types:Mockito.*andArgumentMatchers.any, for example)caselabels of a switch on that enum stay unqualified, because qualified labels need Java 21. In a Java 21 switch on another type they're ordinary namesT.foo(x)could make one ofT's instance overloads the best match, that import staysjava.langtypes never get an import, becauseremoveUnusedImports()would delete itspotless:offregions, Javadoc references, names spelled with unicode escapes, andmodule-info.javakeep their importsStatic imports of nested classes become regular imports (
import java.util.Map.Entry;). New imports are appended after the existing ones, so likeshortenFullyQualifiedTypesit's meant to run beforeimportOrder().It's a big PR, mostly because of these checks. The class Javadoc of
ReplaceStaticImportsFormatterFuncwalks through the approach.Error Prone's take on static imports
This came up in the issue thread, so I looked into it. Error Prone has no rule against static imports in general.
BadImport(on by default, as a warning) only qualifies a fixed list of generic names such asof,copyOf,valueOf,builder,INSTANCE, or nestedBuilder/Type/Key, because the class name gives the reader context there. ItsBadEnclosingTypesoption can be pointed at whole classes, but that runs inside a full compile and leaves the static import line behind for a separate cleanup pass. Google Java Style is fine with single static imports too.So this step is an opt-in preference that goes further than both. Where they overlap, they agree: nested classes become regular imports, and enum labels stay unqualified.
Testing
StepHarnesschecks idempotency. There's also anequality()test.java.lang.Math.Notes for review
expandWildcardImports. Only the defaulttoggleOffOnmarkers (and@formatter:off/@formatter:on) are recognised. Classes that aren't on the classpath, such as generated sources, are assumed not to exist.SymbolResolutionhelper.expandWildcardImportsuses it with unchanged behaviour.FileSignature, the paths transient, plus the JDK version.expandWildcardImportshas the same leak, and I'm happy to apply the same fix there in a follow-up.expandWildcardImports, the Gradle plugin resolves the compile classpath when the DSL method runs. Making both of them lazy seemed like a separate change. Maven now resolves the classpath once per execution, however many steps ask for it.Thanks for maintaining Spotless, and I'm happy to adjust anything!