Skip to content

Commit 009c5da

Browse files
authored
Rename removeWildcardImports to forbidWildcardImports, etc. (#2633)
2 parents 792d0f2 + d8d496a commit 009c5da

File tree

14 files changed

+119
-44
lines changed

14 files changed

+119
-44
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Changes
1414
* **BREAKING** Bump the required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
15+
* **BREAKING** Renamed `RemoveWildcardImportsStep` to `ForbidWildcardImportsStep`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
1516
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))
1617
* Adds support for worktrees (fixes [#1765](https://github.com/diffplug/spotless/issues/1765))
1718
* Bump default `google-java-format` version to latest `1.24.0` -> `1.28.0`. ([#2345](https://github.com/diffplug/spotless/pull/2345))

lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java renamed to lib/src/main/java/com/diffplug/spotless/java/ForbidWildcardImportsStep.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
import com.diffplug.spotless.FormatterStep;
1919
import com.diffplug.spotless.generic.ReplaceRegexStep;
2020

21-
/** Removes any wildcard import statements. */
22-
public final class RemoveWildcardImportsStep {
21+
/** Forbids any wildcard import statements. */
22+
public final class ForbidWildcardImportsStep {
2323

2424
/**
2525
* Matches lines like 'import foo.*;' or 'import static foo.*;'.
2626
*/
2727
private static final String REGEX = "(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?";
28-
private static final String NAME = "removeWildcardImports";
28+
private static final String NAME = "forbidWildcardImports";
2929
private static final String ERROR = "Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this";
3030

31-
private RemoveWildcardImportsStep() {}
31+
private ForbidWildcardImportsStep() {}
3232

3333
public static FormatterStep create() {
3434
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);

plugin-gradle/CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changed
77
* **BREAKING** Bump the required Gradle to `7.3` and required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
8+
* **BREAKING** Renamed `removeWildcardImports` to `forbidWildcardImports`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
89
* **BREAKING** `spotlessInstallGitPrePushHook` task is now installed only on the root project. ([#2570](https://github.com/diffplug/spotless/pull/2570))
910
* **BREAKING** `LintSuppression` now enforces unix-style paths in its `setPath` method. ([#2629](https://github.com/diffplug/spotless/pull/2629))
1011
* Running `spotlessCheck` with violations unilaterally produces the error message `Run './gradlew spotlessApply' to fix these violations`. ([#2592](https://github.com/diffplug/spotless/issues/2592))
@@ -21,6 +22,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2122
* Respect system gitconfig when performing git operations ([#2404](https://github.com/diffplug/spotless/issues/2404))
2223
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
2324
* `spotlessInstallGitPrePushHook` is now compatible with configuration cache. ([#2570](https://github.com/diffplug/spotless/pull/2570))
25+
### Added
26+
* There is now a `forbidRegex(String name, String regex, String lintDetail)` which throws a lint anytime the regex matches. ([#2633](https://github.com/diffplug/spotless/pull/2633))
2427

2528
## [7.2.1] - 2025-07-21
2629
### Fixed

plugin-gradle/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ spotless {
206206
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
207207
208208
removeUnusedImports()
209-
removeWildcardImports()
209+
forbidWildcardImports()
210210
211211
// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
212212
cleanthat() // has its own section below
@@ -247,12 +247,12 @@ spotless {
247247
removeUnusedImports('cleanthat-javaparser-unnecessaryimport')
248248
```
249249
250-
### removeWildcardImports
250+
### forbidWildcardImports
251251
252252
```
253253
spotless {
254254
java {
255-
removeWildcardImports()
255+
forbidWildcardImports()
256256
}
257257
}
258258
```

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,11 @@ public void replaceRegex(String name, String regex, String replacement) {
490490
addStep(ReplaceRegexStep.create(name, regex, replacement));
491491
}
492492

493+
/** A regex which generates a lint. */
494+
public void forbidRegex(String name, String regex, String lintDetail) {
495+
addStep(ReplaceRegexStep.lint(name, regex, lintDetail));
496+
}
497+
493498
/** Removes trailing whitespace. */
494499
public void trimTrailingWhitespace() {
495500
addStep(TrimTrailingWhitespaceStep.create());

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
import com.diffplug.spotless.extra.java.EclipseJdtFormatterStep;
3737
import com.diffplug.spotless.generic.LicenseHeaderStep;
3838
import com.diffplug.spotless.java.CleanthatJavaStep;
39+
import com.diffplug.spotless.java.ForbidWildcardImportsStep;
3940
import com.diffplug.spotless.java.FormatAnnotationsStep;
4041
import com.diffplug.spotless.java.GoogleJavaFormatStep;
4142
import com.diffplug.spotless.java.ImportOrderStep;
4243
import com.diffplug.spotless.java.PalantirJavaFormatStep;
4344
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
44-
import com.diffplug.spotless.java.RemoveWildcardImportsStep;
4545

4646
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense, JvmLang {
4747
static final String NAME = "java";
@@ -152,8 +152,14 @@ public void removeUnusedImports(String formatter) {
152152
addStep(RemoveUnusedImportsStep.create(formatter, provisioner()));
153153
}
154154

155+
@Deprecated
155156
public void removeWildcardImports() {
156-
addStep(RemoveWildcardImportsStep.create());
157+
System.err.println("`removeWildcardImports()` replaced by `forbidWildcardImports()`");
158+
forbidWildcardImports();
159+
}
160+
161+
public void forbidWildcardImports() {
162+
addStep(ForbidWildcardImportsStep.create());
157163
}
158164

159165
/** Uses the <a href="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/google/google-java-format">google-java-format</a> jar to format source code. */

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Changes
77
* **BREAKING** Bump the required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
8+
* **BREAKING** Renamed `removeWildcardImports` to `forbidWildcardImports`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
89
* **BREAKING** `spotless:install-git-pre-push-hook` task is now always installed in the root `.git/hooks` directory by resolving the top-level project base directory. ([#2570](https://github.com/diffplug/spotless/pull/2570))
910
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))
1011
* Adds support for worktrees (fixes [#1765](https://github.com/diffplug/spotless/issues/1765))
@@ -19,6 +20,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1920
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
2021
### Added
2122
* `<lintSupressions>` API ([#2309](https://github.com/diffplug/spotless/issues/2309))
23+
* There is now a `forbidRegex(String name, String searchRegex, String lintDetail)` which throws a lint anytime the regex matches. ([#2633](https://github.com/diffplug/spotless/pull/2633))
2224

2325
## [2.46.1] - 2025-07-21
2426
### Fixed

plugin-maven/README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
230230
</importOrder>
231231

232232
<removeUnusedImports /> <!-- self-explanatory -->
233-
<removeWildcardImports /> <!-- drop any import ending with '*' -->
233+
<forbidWildcardImports /> <!-- yell if any import ends with '*' -->
234234

235235
<formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
236236

@@ -249,10 +249,10 @@ any other maven phase (i.e. compile) then it can be configured as below;
249249
</removeUnusedImports>
250250
```
251251

252-
### removeWildcardImports
252+
### forbidWildcardImports
253253

254254
```xml
255-
<removeWildcardImports/>
255+
<forbidWildcardImports/>
256256
```
257257

258258
### google-java-format
@@ -1786,6 +1786,12 @@ See [here](../INTELLIJ_IDEA_SCREENSHOTS.md) for an explanation on how to extract
17861786
<searchRegex>(Hello) W[a-z]{3}d</searchRegex>
17871787
<replacement>$1 Mars</replacement>
17881788
</replaceRegex>
1789+
1790+
<forbidRegex>
1791+
<name>Forbid FooBar</name>
1792+
<searchRegex>import foo\.bar\.(.*)</searchRegex>
1793+
<lintDetail>foobar is officially abandoned, use fizzbuzz instead</lintDetail>
1794+
</forbidRegex>
17891795
```
17901796

17911797
<a name="license-header-options"></a>
@@ -1950,7 +1956,7 @@ Sometimes Spotless will encounter lint errors that can't be auto-fixed. For exam
19501956

19511957
```
19521958
[ERROR] Unable to format file src/main/java/com/example/App.java
1953-
[ERROR] Step 'removeWildcardImports' found problem in 'App.java':
1959+
[ERROR] Step 'forbidWildcardImports' found problem in 'App.java':
19541960
[ERROR] Do not use wildcard imports
19551961
```
19561962

@@ -1963,12 +1969,12 @@ To suppress these lints, you can use the `<lintSuppressions>` configuration:
19631969
<version>${spotless.version}</version>
19641970
<configuration>
19651971
<java>
1966-
<removeWildcardImports/>
1972+
<forbidWildcardImports/>
19671973
</java>
19681974
<lintSuppressions>
19691975
<lintSuppression>
19701976
<path>src/main/java/com/example/App.java</path>
1971-
<step>removeWildcardImports</step>
1977+
<step>forbidWildcardImports</step>
19721978
<shortCode>*</shortCode>
19731979
</lintSuppression>
19741980
</lintSuppressions>
@@ -1988,13 +1994,13 @@ You can suppress multiple patterns:
19881994
<!-- Suppress all wildcard import errors in legacy code -->
19891995
<lintSuppression>
19901996
<path>src/main/java/com/example/legacy/*</path>
1991-
<step>removeWildcardImports</step>
1997+
<step>forbidWildcardImports</step>
19921998
<shortCode>*</shortCode>
19931999
</lintSuppression>
19942000
<!-- Suppress all errors from a specific step -->
19952001
<lintSuppression>
19962002
<path>*</path>
1997-
<step>removeWildcardImports</step>
2003+
<step>forbidWildcardImports</step>
19982004
<shortCode>*</shortCode>
19992005
</lintSuppression>
20002006
</lintSuppressions>

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.diffplug.spotless.LineEnding;
3939
import com.diffplug.spotless.maven.generic.EclipseWtp;
4040
import com.diffplug.spotless.maven.generic.EndWithNewline;
41+
import com.diffplug.spotless.maven.generic.ForbidRegex;
4142
import com.diffplug.spotless.maven.generic.Idea;
4243
import com.diffplug.spotless.maven.generic.Indent;
4344
import com.diffplug.spotless.maven.generic.Jsr223;
@@ -140,6 +141,10 @@ public final void addReplaceRegex(ReplaceRegex replaceRegex) {
140141
addStepFactory(replaceRegex);
141142
}
142143

144+
public final void addForbidRegex(ForbidRegex forbidRegex) {
145+
addStepFactory(forbidRegex);
146+
}
147+
143148
public final void addEclipseWtp(EclipseWtp eclipseWtp) {
144149
addStepFactory(eclipseWtp);
145150
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2016-2025 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.generic;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.generic.ReplaceRegexStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class ForbidRegex implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String name;
29+
30+
@Parameter
31+
private String searchRegex;
32+
33+
@Parameter
34+
private String lintDetail;
35+
36+
@Override
37+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38+
if (name == null || searchRegex == null) {
39+
throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'.");
40+
}
41+
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
42+
// an empty string as a property value as maven will always trim the value and if it is
43+
// empty, maven will consider the property as not provided.
44+
return ReplaceRegexStep.lint(name, searchRegex, lintDetail);
45+
}
46+
}

0 commit comments

Comments
 (0)