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.
10
10
We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 1.27.0 ` ).
11
11
12
12
## [ Unreleased]
13
+ ### Added
14
+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
13
15
14
16
## [ 3.1.2] - 2025-05-27
15
17
### 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 3
3
We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 3.27.0 ` ).
4
4
5
5
## [ Unreleased]
6
+ ### Added
7
+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
6
8
7
9
## [ 7.0.4] - 2025-05-27
8
10
### Fixed
Original file line number Diff line number Diff line change @@ -188,6 +188,7 @@ spotless {
188
188
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
189
189
190
190
removeUnusedImports()
191
+ removeWildcardImports()
191
192
192
193
// Cleanthat will refactor your code, but it may break your style: apply it before your formatter
193
194
cleanthat() // has its own section below
@@ -227,6 +228,16 @@ spotless {
227
228
removeUnusedImports('cleanthat-javaparser-unnecessaryimport')
228
229
```
229
230
231
+ ### removeWildcardImports
232
+
233
+ ```
234
+ spotless {
235
+ java {
236
+ removeWildcardImports()
237
+ }
238
+ }
239
+ ```
240
+
230
241
### google-java-format
231
242
232
243
[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 1
1
/*
2
- * Copyright 2016-2024 DiffPlug
2
+ * Copyright 2016-2025 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
41
41
import com .diffplug .spotless .java .ImportOrderStep ;
42
42
import com .diffplug .spotless .java .PalantirJavaFormatStep ;
43
43
import com .diffplug .spotless .java .RemoveUnusedImportsStep ;
44
+ import com .diffplug .spotless .java .RemoveWildcardImportsStep ;
44
45
45
46
public class JavaExtension extends FormatExtension implements HasBuiltinDelimiterForLicense , JvmLang {
46
47
static final String NAME = "java" ;
@@ -151,6 +152,10 @@ public void removeUnusedImports(String formatter) {
151
152
addStep (RemoveUnusedImportsStep .create (formatter , provisioner ()));
152
153
}
153
154
155
+ public void removeWildcardImports () {
156
+ addStep (RemoveWildcardImportsStep .create ());
157
+ }
158
+
154
159
/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
155
160
public GoogleJavaFormatConfig googleJavaFormat () {
156
161
return googleJavaFormat (GoogleJavaFormatStep .defaultVersion ());
Original file line number Diff line number Diff line change @@ -81,6 +81,26 @@ void removeUnusedImportsWithCleanthat() throws IOException {
81
81
assertFile ("src/main/java/test.java" ).sameAsResource ("java/removeunusedimports/Jdk17TextBlockFormatted.test" );
82
82
}
83
83
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
+
84
104
/**
85
105
* Triggers the special case in {@link FormatExtension#setupTask(SpotlessTask)} with {@code toggleFence} and
86
106
* {@code targetExcludeContentPattern} both being not {@code null}.
Original file line number Diff line number Diff line change 3
3
We adhere to the [ keepachangelog] ( https://keepachangelog.com/en/1.0.0/ ) format (starting after version ` 1.27.0 ` ).
4
4
5
5
## [ Unreleased]
6
+ ### Added
7
+ * Add support for removing wildcard imports via ` removeWildcardImports ` step. ([ #2517 ] ( https://github.com/diffplug/spotless/pull/2517 ) )
6
8
7
9
## [ 2.44.5] - 2025-05-27
8
10
### 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;
210
210
</importOrder >
211
211
212
212
<removeUnusedImports /> <!-- self-explanatory -->
213
+ <removeWildcardImports /> <!-- drop any import ending with '*' -->
213
214
214
215
<formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
215
216
@@ -228,6 +229,12 @@ any other maven phase (i.e. compile) then it can be configured as below;
228
229
</removeUnusedImports >
229
230
```
230
231
232
+ ### removeWildcardImports
233
+
234
+ ``` xml
235
+ <removeWildcardImports />
236
+ ```
237
+
231
238
### google-java-format
232
239
233
240
[ 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 1
1
/*
2
- * Copyright 2016-2023 DiffPlug
2
+ * Copyright 2016-2025 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -76,6 +76,10 @@ public void addRemoveUnusedImports(RemoveUnusedImports removeUnusedImports) {
76
76
addStepFactory (removeUnusedImports );
77
77
}
78
78
79
+ public void addRemoveWildcardImports (RemoveWildcardImports removeWildcardImports ) {
80
+ addStepFactory (removeWildcardImports );
81
+ }
82
+
79
83
public void addFormatAnnotations (FormatAnnotations formatAnnotations ) {
80
84
addStepFactory (formatAnnotations );
81
85
}
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