Skip to content

Commit 922f9f1

Browse files
iddeepakPankraz76GooolerCopilot
authored
Support removing wildcard imports via removeWildcardImports step (#2517)
* Remove wildcard imports * Update plugin-gradle/README.md Co-authored-by: Consequences of Lacking Discipline <[email protected]> * Update testlib/src/main/resources/java/removewildcardimports/JavaCodeWildcardsUnformatted.test Co-authored-by: Consequences of Lacking Discipline <[email protected]> * Apply suggestions from code review * gradle test * gradle test * Update plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Co-authored-by: Copilot <[email protected]> * spotless apply * spotless apply * spotless apply * Apply suggestions from code review --------- Co-authored-by: Consequences of Lacking Discipline <[email protected]> Co-authored-by: Zongle Wang <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 2cc8c04 commit 922f9f1

File tree

13 files changed

+161
-2
lines changed

13 files changed

+161
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We 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
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

plugin-gradle/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We 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

plugin-gradle/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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).

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -41,6 +41,7 @@
4141
import com.diffplug.spotless.java.ImportOrderStep;
4242
import com.diffplug.spotless.java.PalantirJavaFormatStep;
4343
import com.diffplug.spotless.java.RemoveUnusedImportsStep;
44+
import com.diffplug.spotless.java.RemoveWildcardImportsStep;
4445

4546
public 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());

plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JavaDefaultTargetTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff 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}.

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We 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

plugin-maven/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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).

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
 (0)