Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changes
* **BREAKING** Bump the required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
* **BREAKING** Renamed `RemoveWildcardImportsStep` to `ForbidWildcardImportsStep`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))
* Adds support for worktrees (fixes [#1765](https://github.com/diffplug/spotless/issues/1765))
* Bump default `google-java-format` version to latest `1.24.0` -> `1.28.0`. ([#2345](https://github.com/diffplug/spotless/pull/2345))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.generic.ReplaceRegexStep;

/** Removes any wildcard import statements. */
public final class RemoveWildcardImportsStep {
/** Forbids any wildcard import statements. */
public final class ForbidWildcardImportsStep {

/**
* Matches lines like 'import foo.*;' or 'import static foo.*;'.
*/
private static final String REGEX = "(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?";
private static final String NAME = "removeWildcardImports";
private static final String NAME = "forbidWildcardImports";
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";

private RemoveWildcardImportsStep() {}
private ForbidWildcardImportsStep() {}

public static FormatterStep create() {
return ReplaceRegexStep.lint(NAME, REGEX, ERROR);
Expand Down
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changed
* **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))
* **BREAKING** Renamed `removeWildcardImports` to `forbidWildcardImports`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
* **BREAKING** `spotlessInstallGitPrePushHook` task is now installed only on the root project. ([#2570](https://github.com/diffplug/spotless/pull/2570))
* **BREAKING** `LintSuppression` now enforces unix-style paths in its `setPath` method. ([#2629](https://github.com/diffplug/spotless/pull/2629))
* Running `spotlessCheck` with violations unilaterally produces the error message `Run './gradlew spotlessApply' to fix these violations`. ([#2592](https://github.com/diffplug/spotless/issues/2592))
Expand All @@ -21,6 +22,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Respect system gitconfig when performing git operations ([#2404](https://github.com/diffplug/spotless/issues/2404))
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
* `spotlessInstallGitPrePushHook` is now compatible with configuration cache. ([#2570](https://github.com/diffplug/spotless/pull/2570))
### Added
* 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))

## [7.2.1] - 2025-07-21
### Fixed
Expand Down
6 changes: 3 additions & 3 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ spotless {
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse

removeUnusedImports()
removeWildcardImports()
forbidWildcardImports()

// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
cleanthat() // has its own section below
Expand Down Expand Up @@ -247,12 +247,12 @@ spotless {
removeUnusedImports('cleanthat-javaparser-unnecessaryimport')
```

### removeWildcardImports
### forbidWildcardImports

```
spotless {
java {
removeWildcardImports()
forbidWildcardImports()
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ public void replaceRegex(String name, String regex, String replacement) {
addStep(ReplaceRegexStep.create(name, regex, replacement));
}

/** A regex which generates a lint. */
public void forbidRegex(String name, String regex, String lintDetail) {
addStep(ReplaceRegexStep.lint(name, regex, lintDetail));
}

/** Removes trailing whitespace. */
public void trimTrailingWhitespace() {
addStep(TrimTrailingWhitespaceStep.create());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
import com.diffplug.spotless.extra.java.EclipseJdtFormatterStep;
import com.diffplug.spotless.generic.LicenseHeaderStep;
import com.diffplug.spotless.java.CleanthatJavaStep;
import com.diffplug.spotless.java.ForbidWildcardImportsStep;
import com.diffplug.spotless.java.FormatAnnotationsStep;
import com.diffplug.spotless.java.GoogleJavaFormatStep;
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.java.PalantirJavaFormatStep;
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
import com.diffplug.spotless.java.RemoveWildcardImportsStep;

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

@Deprecated
public void removeWildcardImports() {
addStep(RemoveWildcardImportsStep.create());
System.err.println("`removeWildcardImports()` replaced by `forbidWildcardImports()`");
forbidWildcardImports();
}

public void forbidWildcardImports() {
addStep(ForbidWildcardImportsStep.create());
}

/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changes
* **BREAKING** Bump the required Java to `17`. ([#2375](https://github.com/diffplug/spotless/issues/2375), [#2540](https://github.com/diffplug/spotless/pull/2540))
* **BREAKING** Renamed `removeWildcardImports` to `forbidWildcardImports`. ([#2633](https://github.com/diffplug/spotless/pull/2633))
* **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))
* Bump JGit from `6.10.1` to `7.3.0` ([#2257](https://github.com/diffplug/spotless/pull/2257))
* Adds support for worktrees (fixes [#1765](https://github.com/diffplug/spotless/issues/1765))
Expand All @@ -19,6 +20,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
### Added
* `<lintSupressions>` API ([#2309](https://github.com/diffplug/spotless/issues/2309))
* 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))

## [2.46.1] - 2025-07-21
### Fixed
Expand Down
22 changes: 14 additions & 8 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
</importOrder>

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

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

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

### removeWildcardImports
### forbidWildcardImports

```xml
<removeWildcardImports/>
<forbidWildcardImports/>
```

### google-java-format
Expand Down Expand Up @@ -1786,6 +1786,12 @@ See [here](../INTELLIJ_IDEA_SCREENSHOTS.md) for an explanation on how to extract
<searchRegex>(Hello) W[a-z]{3}d</searchRegex>
<replacement>$1 Mars</replacement>
</replaceRegex>

<forbidRegex>
<name>Forbid FooBar</name>
<searchRegex>import foo\.bar\.(.*)</searchRegex>
<lintDetail>foobar is officially abandoned, use fizzbuzz instead</lintDetail>
</forbidRegex>
```

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

```
[ERROR] Unable to format file src/main/java/com/example/App.java
[ERROR] Step 'removeWildcardImports' found problem in 'App.java':
[ERROR] Step 'forbidWildcardImports' found problem in 'App.java':
[ERROR] Do not use wildcard imports
```

Expand All @@ -1963,12 +1969,12 @@ To suppress these lints, you can use the `<lintSuppressions>` configuration:
<version>${spotless.version}</version>
<configuration>
<java>
<removeWildcardImports/>
<forbidWildcardImports/>
</java>
<lintSuppressions>
<lintSuppression>
<path>src/main/java/com/example/App.java</path>
<step>removeWildcardImports</step>
<step>forbidWildcardImports</step>
<shortCode>*</shortCode>
</lintSuppression>
</lintSuppressions>
Expand All @@ -1988,13 +1994,13 @@ You can suppress multiple patterns:
<!-- Suppress all wildcard import errors in legacy code -->
<lintSuppression>
<path>src/main/java/com/example/legacy/*</path>
<step>removeWildcardImports</step>
<step>forbidWildcardImports</step>
<shortCode>*</shortCode>
</lintSuppression>
<!-- Suppress all errors from a specific step -->
<lintSuppression>
<path>*</path>
<step>removeWildcardImports</step>
<step>forbidWildcardImports</step>
<shortCode>*</shortCode>
</lintSuppression>
</lintSuppressions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.diffplug.spotless.LineEnding;
import com.diffplug.spotless.maven.generic.EclipseWtp;
import com.diffplug.spotless.maven.generic.EndWithNewline;
import com.diffplug.spotless.maven.generic.ForbidRegex;
import com.diffplug.spotless.maven.generic.Idea;
import com.diffplug.spotless.maven.generic.Indent;
import com.diffplug.spotless.maven.generic.Jsr223;
Expand Down Expand Up @@ -140,6 +141,10 @@ public final void addReplaceRegex(ReplaceRegex replaceRegex) {
addStepFactory(replaceRegex);
}

public final void addForbidRegex(ForbidRegex forbidRegex) {
addStepFactory(forbidRegex);
}

public final void addEclipseWtp(EclipseWtp eclipseWtp) {
addStepFactory(eclipseWtp);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2016-2025 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.generic;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.generic.ReplaceRegexStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class ForbidRegex implements FormatterStepFactory {

@Parameter
private String name;

@Parameter
private String searchRegex;

@Parameter
private String lintDetail;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
if (name == null || searchRegex == null) {
throw new IllegalArgumentException("Must specify 'name' and 'searchRegex'.");
}
// Use empty string if replacement is not provided. In pom.xml there is no way to specify
// an empty string as a property value as maven will always trim the value and if it is
// empty, maven will consider the property as not provided.
return ReplaceRegexStep.lint(name, searchRegex, lintDetail);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
package com.diffplug.spotless.maven.java;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.java.RemoveWildcardImportsStep;
import com.diffplug.spotless.java.ForbidWildcardImportsStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class RemoveWildcardImports implements FormatterStepFactory {
public class ForbidWildcardImports implements FormatterStepFactory {
@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
return RemoveWildcardImportsStep.create();
return ForbidWildcardImportsStep.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
addStepFactory(removeUnusedImports);
}

public void addRemoveWildcardImports(RemoveWildcardImports removeWildcardImports) {
addStepFactory(removeWildcardImports);
public void addForbidWildcardImports(ForbidWildcardImports forbidWildcardImports) {
addStepFactory(forbidWildcardImports);
}

public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@

import com.diffplug.spotless.maven.MavenIntegrationHarness;

class RemoveWildcardImportsStepTest extends MavenIntegrationHarness {
class ForbidWildcardImportsStepTest extends MavenIntegrationHarness {

@Test
void testRemoveWildcardImports() throws Exception {
writePomWithJavaSteps("<removeWildcardImports/>");
void testForbidWildcardImports() throws Exception {
writePomWithJavaSteps("<forbidWildcardImports/>");

String path = "src/main/java/test.java";
setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
expectSelfieErrorMsg(mavenRunner().withArguments("spotless:apply").runHasError()).toBe("""
var selfie = expectSelfieErrorMsg(mavenRunner().withArguments("spotless:apply").runHasError());
assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
selfie.toBe("""
Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:VERSION:apply (default-cli) on project spotless-maven-plugin-tests: There were 5 lint error(s), they must be fixed or suppressed.
src/main/java/test.java:L1 removeWildcardImports(import java.util.*;) 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
src/main/java/test.java:L2 removeWildcardImports(import static java.util.Collections.*;) 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
src/main/java/test.java:L5 removeWildcardImports(import io.quarkus.maven.dependency.*;) 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
src/main/java/test.java:L6 removeWildcardImports(import static io.quarkus.vertx.web.Route.HttpMethod.*;) 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
src/main/java/test.java:L7 removeWildcardImports(import static org.springframework.web.reactive.function.BodyInserters.*;) 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
src/main/java/test.java:L1 forbidWildcardImports(import java.util.*;) 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
src/main/java/test.java:L2 forbidWildcardImports(import static java.util.Collections.*;) 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
src/main/java/test.java:L5 forbidWildcardImports(import io.quarkus.maven.dependency.*;) 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
src/main/java/test.java:L6 forbidWildcardImports(import static io.quarkus.vertx.web.Route.HttpMethod.*;) 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
src/main/java/test.java:L7 forbidWildcardImports(import static org.springframework.web.reactive.function.BodyInserters.*;) 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
Resolve these lints or suppress with `<lintSuppressions>`
""");
assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
}
}
Loading
Loading