diff --git a/CHANGES.md b/CHANGES.md
index 920bef6631..ba1e66f1f7 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
## [Unreleased]
+### Added
+* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
## [3.1.2] - 2025-05-27
### Fixed
diff --git a/lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java b/lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java
new file mode 100644
index 0000000000..ca513b8280
--- /dev/null
+++ b/lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 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.java;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.generic.ReplaceRegexStep;
+
+/** Removes any wildcard import statements. */
+public final class RemoveWildcardImportsStep {
+ private RemoveWildcardImportsStep() {}
+
+ public static FormatterStep create() {
+ // Matches lines like 'import foo.*;' or 'import static foo.*;'.
+ return ReplaceRegexStep.create(
+ "removeWildcardImports",
+ "(?m)^import\\s+(?:static\\s+)?[^;\\n]*\\*;\\R?",
+ "");
+ }
+}
diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md
index e35da34bfc..c7fc99944b 100644
--- a/plugin-gradle/CHANGES.md
+++ b/plugin-gradle/CHANGES.md
@@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
## [Unreleased]
+### Added
+* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
## [7.0.4] - 2025-05-27
### Fixed
diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md
index e3bf3df8fd..7a2c6b42b7 100644
--- a/plugin-gradle/README.md
+++ b/plugin-gradle/README.md
@@ -188,6 +188,7 @@ spotless {
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
removeUnusedImports()
+ removeWildcardImports()
// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
cleanthat() // has its own section below
@@ -227,6 +228,16 @@ spotless {
removeUnusedImports('cleanthat-javaparser-unnecessaryimport')
```
+### removeWildcardImports
+
+```
+spotless {
+ java {
+ removeWildcardImports()
+ }
+}
+```
+
### google-java-format
[homepage](https://github.com/google/google-java-format). [changelog](https://github.com/google/google-java-format/releases).
diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java
index b7d3c84304..aeed7b64b5 100644
--- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java
+++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2024 DiffPlug
+ * 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.
@@ -41,6 +41,7 @@
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";
@@ -151,6 +152,10 @@ public void removeUnusedImports(String formatter) {
addStep(RemoveUnusedImportsStep.create(formatter, provisioner()));
}
+ public void removeWildcardImports() {
+ addStep(RemoveWildcardImportsStep.create());
+ }
+
/** Uses the google-java-format jar to format source code. */
public GoogleJavaFormatConfig googleJavaFormat() {
return googleJavaFormat(GoogleJavaFormatStep.defaultVersion());
diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java
index a244de372e..401e8ee330 100644
--- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java
+++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java
@@ -81,6 +81,26 @@ void removeUnusedImportsWithCleanthat() throws IOException {
assertFile("src/main/java/test.java").sameAsResource("java/removeunusedimports/Jdk17TextBlockFormatted.test");
}
+ @Test
+ void removeWildCardImports() throws IOException {
+ setFile("build.gradle").toLines(
+ "plugins {",
+ " id 'com.diffplug.spotless'",
+ "}",
+ "repositories { mavenCentral() }",
+ "",
+ "spotless {",
+ " java {",
+ " target file('test.java')",
+ " removeWildcardImports()",
+ " }",
+ "}");
+
+ setFile("test.java").toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
+ gradleRunner().withArguments("spotlessApply").build();
+ assertFile("test.java").sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
+ }
+
/**
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
* {@code targetExcludeContentPattern} both being not {@code null}.
diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md
index d09ac6144f..53c6f4b093 100644
--- a/plugin-maven/CHANGES.md
+++ b/plugin-maven/CHANGES.md
@@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
## [Unreleased]
+### Added
+* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
## [2.44.5] - 2025-05-27
### Changed
diff --git a/plugin-maven/README.md b/plugin-maven/README.md
index cafb11ea43..b27ddc4eab 100644
--- a/plugin-maven/README.md
+++ b/plugin-maven/README.md
@@ -210,6 +210,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
+
@@ -228,6 +229,12 @@ any other maven phase (i.e. compile) then it can be configured as below;
```
+### removeWildcardImports
+
+```xml
+
+```
+
### google-java-format
[homepage](https://github.com/google/google-java-format). [changelog](https://github.com/google/google-java-format/releases). [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/GoogleJavaFormat.java).
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
index b0692446b7..9fdb794eb1 100644
--- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/Java.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016-2023 DiffPlug
+ * 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.
@@ -76,6 +76,10 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
addStepFactory(removeUnusedImports);
}
+ public void addRemoveWildcardImports(RemoveWildcardImports removeWildcardImports) {
+ addStepFactory(removeWildcardImports);
+ }
+
public void addFormatAnnotations(FormatAnnotations formatAnnotations) {
addStepFactory(formatAnnotations);
}
diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/RemoveWildcardImports.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/RemoveWildcardImports.java
new file mode 100644
index 0000000000..8620088a08
--- /dev/null
+++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/java/RemoveWildcardImports.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 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.java;
+
+import com.diffplug.spotless.FormatterStep;
+import com.diffplug.spotless.java.RemoveWildcardImportsStep;
+import com.diffplug.spotless.maven.FormatterStepConfig;
+import com.diffplug.spotless.maven.FormatterStepFactory;
+
+public class RemoveWildcardImports implements FormatterStepFactory {
+ @Override
+ public FormatterStep newFormatterStep(FormatterStepConfig config) {
+ return RemoveWildcardImportsStep.create();
+ }
+}
diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java
new file mode 100644
index 0000000000..4d4ade94d0
--- /dev/null
+++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 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.java;
+
+import org.junit.jupiter.api.Test;
+
+import com.diffplug.spotless.maven.MavenIntegrationHarness;
+
+class RemoveWildcardImportsStepTest extends MavenIntegrationHarness {
+
+ @Test
+ void testRemoveWildcardImports() throws Exception {
+ writePomWithJavaSteps("");
+
+ String path = "src/main/java/test.java";
+ setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test");
+ mavenRunner().withArguments("spotless:apply").runNoError();
+ assertFile(path).sameAsResource("java/removewildcardimports/JavaCodeWildcardsFormatted.test");
+ }
+}
diff --git a/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsFormatted.test b/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsFormatted.test
new file mode 100644
index 0000000000..85ee903fc5
--- /dev/null
+++ b/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsFormatted.test
@@ -0,0 +1,4 @@
+import java.util.List;
+import mylib.Helper;
+
+public class Test {}
\ No newline at end of file
diff --git a/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsUnformatted.test b/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsUnformatted.test
new file mode 100644
index 0000000000..74646094ae
--- /dev/null
+++ b/testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsUnformatted.test
@@ -0,0 +1,9 @@
+import java.util.*;
+import static java.util.Collections.*;
+import java.util.List;
+import mylib.Helper;
+import io.quarkus.maven.dependency.*;
+import static io.quarkus.vertx.web.Route.HttpMethod.*;
+import static org.springframework.web.reactive.function.BodyInserters.*;
+
+public class Test {}
\ No newline at end of file