Skip to content
Merged
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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?",
"");
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ spotless {
importOrderFile('eclipse-import-order.txt') // import order file as exported from eclipse
removeUnusedImports()
removeWildcardImports()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removeObsoletes()
removeObsoleteInit()
removeObsoleteInitializations()

// 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 @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -151,6 +152,10 @@ public void removeUnusedImports(String formatter) {
addStep(RemoveUnusedImportsStep.create(formatter, provisioner()));
}

public void removeWildcardImports() {
addStep(RemoveWildcardImportsStep.create());
}

/** Uses the <a href="https://github.com/google/google-java-format">google-java-format</a> jar to format source code. */
public GoogleJavaFormatConfig googleJavaFormat() {
return googleJavaFormat(GoogleJavaFormatStep.defaultVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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 '*' -->

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

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

### removeWildcardImports

```xml
<removeWildcardImports/>
```

### 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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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("<removeWildcardImports/>");

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");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import java.util.List;
import mylib.Helper;

public class Test {}
Original file line number Diff line number Diff line change
@@ -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 {}
Loading