File tree Expand file tree Collapse file tree 13 files changed +161
-2
lines changed
lib/src/main/java/com/diffplug/spotless/java
main/java/com/diffplug/gradle/spotless
test/java/com/diffplug/gradle/spotless
main/java/com/diffplug/spotless/maven/java
test/java/com/diffplug/spotless/maven/java
testlib/src/main/resources/java/removewildcardimports Expand file tree Collapse file tree 13 files changed +161
-2
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 1.27.0 ` ).
1111
1212## [ Unreleased]
13+ ### Added
14+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
1315
1416## [ 3.1.2] - 2025-05-27
1517### Fixed
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 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 .java ;
17+
18+ import com .diffplug .spotless .FormatterStep ;
19+ import com .diffplug .spotless .generic .ReplaceRegexStep ;
20+
21+ /** Removes any wildcard import statements. */
22+ public final class RemoveWildcardImportsStep {
23+ private RemoveWildcardImportsStep () {}
24+
25+ public static FormatterStep create () {
26+ // Matches lines like 'import foo.*;' or 'import static foo.*;'.
27+ return ReplaceRegexStep .create (
28+ "removeWildcardImports" ,
29+ "(?m)^import\\ s+(?:static\\ s+)?[^;\\ n]*\\ *;\\ R?" ,
30+ "" );
31+ }
32+ }
Original file line number Diff line number Diff line change 33We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 3.27.0 ` ).
44
55## [ Unreleased]
6+ ### Added
7+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
68
79## [ 7.0.4] - 2025-05-27
810### Fixed
Original file line number Diff line number Diff line change @@ -188,6 +188,7 @@ spotless {
188188 importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
189189
190190 removeUnusedImports()
191+ removeWildcardImports()
191192
192193 // Cleanthat will refactor your code, but it may break your style: apply it before your formatter
193194 cleanthat() // has its own section below
@@ -227,6 +228,16 @@ spotless {
227228 removeUnusedImports('cleanthat-javaparser-unnecessaryimport')
228229```
229230
231+ ### removeWildcardImports
232+
233+ ```
234+ spotless {
235+ java {
236+ removeWildcardImports()
237+ }
238+ }
239+ ```
240+
230241### google-java-format
231242
232243[homepage](https://github.com/google/google-java-format). [changelog](https://github.com/google/google-java-format/releases).
Original file line number Diff line number Diff line change 11/*
2- * Copyright 2016-2024 DiffPlug
2+ * Copyright 2016-2025 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
4141import com .diffplug .spotless .java .ImportOrderStep ;
4242import com .diffplug .spotless .java .PalantirJavaFormatStep ;
4343import com .diffplug .spotless .java .RemoveUnusedImportsStep ;
44+ import com .diffplug .spotless .java .RemoveWildcardImportsStep ;
4445
4546public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense , JvmLang {
4647 static final String NAME = "java" ;
@@ -151,6 +152,10 @@ public void removeUnusedImports(String formatter) {
151152 addStep (RemoveUnusedImportsStep .create (formatter , provisioner ()));
152153 }
153154
155+ public void removeWildcardImports () {
156+ addStep (RemoveWildcardImportsStep .create ());
157+ }
158+
154159 /** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
155160 public GoogleJavaFormatConfig googleJavaFormat () {
156161 return googleJavaFormat (GoogleJavaFormatStep .defaultVersion ());
Original file line number Diff line number Diff line change @@ -81,6 +81,26 @@ void removeUnusedImportsWithCleanthat() throws IOException {
8181 assertFile ("src/main/java/test.java" ).sameAsResource ("java/removeunusedimports/Jdk17TextBlockFormatted.test" );
8282 }
8383
84+ @ Test
85+ void removeWildCardImports () throws IOException {
86+ setFile ("build.gradle" ).toLines (
87+ "plugins {" ,
88+ " id 'com.diffplug.spotless'" ,
89+ "}" ,
90+ "repositories { mavenCentral() }" ,
91+ "" ,
92+ "spotless {" ,
93+ " java {" ,
94+ " target file('test.java')" ,
95+ " removeWildcardImports()" ,
96+ " }" ,
97+ "}" );
98+
99+ setFile ("test.java" ).toResource ("java/removewildcardimports/JavaCodeWildcardsUnformatted.test" );
100+ gradleRunner ().withArguments ("spotlessApply" ).build ();
101+ assertFile ("test.java" ).sameAsResource ("java/removewildcardimports/JavaCodeWildcardsFormatted.test" );
102+ }
103+
84104 /**
85105 * Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
86106 * {@code targetExcludeContentPattern} both being not {@code null}.
Original file line number Diff line number Diff line change 33We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 1.27.0 ` ).
44
55## [ Unreleased]
6+ ### Added
7+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
68
79## [ 2.44.5] - 2025-05-27
810### Changed
Original file line number Diff line number Diff line change @@ -210,6 +210,7 @@ any other maven phase (i.e. compile) then it can be configured as below;
210210 </importOrder >
211211
212212 <removeUnusedImports /> <!-- self-explanatory -->
213+ <removeWildcardImports /> <!-- drop any import ending with '*' -->
213214
214215 <formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
215216
@@ -228,6 +229,12 @@ any other maven phase (i.e. compile) then it can be configured as below;
228229</removeUnusedImports >
229230```
230231
232+ ### removeWildcardImports
233+
234+ ``` xml
235+ <removeWildcardImports />
236+ ```
237+
231238### google-java-format
232239
233240[ 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 ) .
Original file line number Diff line number Diff line change 11/*
2- * Copyright 2016-2023 DiffPlug
2+ * Copyright 2016-2025 DiffPlug
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -76,6 +76,10 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
7676 addStepFactory (removeUnusedImports );
7777 }
7878
79+ public void addRemoveWildcardImports (RemoveWildcardImports removeWildcardImports ) {
80+ addStepFactory (removeWildcardImports );
81+ }
82+
7983 public void addFormatAnnotations (FormatAnnotations formatAnnotations ) {
8084 addStepFactory (formatAnnotations );
8185 }
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright 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 .java ;
17+
18+ import com .diffplug .spotless .FormatterStep ;
19+ import com .diffplug .spotless .java .RemoveWildcardImportsStep ;
20+ import com .diffplug .spotless .maven .FormatterStepConfig ;
21+ import com .diffplug .spotless .maven .FormatterStepFactory ;
22+
23+ public class RemoveWildcardImports implements FormatterStepFactory {
24+ @ Override
25+ public FormatterStep newFormatterStep (FormatterStepConfig config ) {
26+ return RemoveWildcardImportsStep .create ();
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments