Skip to content

Commit 1ed7249

Browse files
authored
Use palantir-java-format as default formatter in RemoveUnusedImportsStep (#2541)
2 parents 03e89b8 + 2ebe138 commit 1ed7249

File tree

5 files changed

+83
-43
lines changed

5 files changed

+83
-43
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Added
1414
* Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020), [#2535](https://github.com/diffplug/spotless/pull/2535))
1515
* Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517))
16+
* Use `palantir-java-format` as default formatter in `RemoveUnusedImportsStep`. ([#2541](https://github.com/diffplug/spotless/pull/2541))
1617

1718
### Fixed
1819
* Make sure npm-based formatters use the correct `node_modules` directory when running in parallel. ([#2542](https://github.com/diffplug/spotless/pull/2542))

lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java

Lines changed: 6 additions & 7 deletions
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.
@@ -22,12 +22,12 @@
2222
import com.diffplug.spotless.FormatterStep;
2323
import com.diffplug.spotless.Provisioner;
2424

25-
/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
25+
/** Uses palantir-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
2626
public class RemoveUnusedImportsStep implements Serializable {
2727
private static final long serialVersionUID = 1L;
2828
static final String NAME = "removeUnusedImports";
2929

30-
static final String GJF = "google-java-format";
30+
static final String DEFAULT_FORMATTER = "palantir-java-format";
3131
static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport";
3232

3333
// https://github.com/solven-eu/cleanthat/blob/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryImport.java
@@ -37,18 +37,17 @@ public class RemoveUnusedImportsStep implements Serializable {
3737
private RemoveUnusedImportsStep() {}
3838

3939
public static String defaultFormatter() {
40-
return GJF;
40+
return DEFAULT_FORMATTER;
4141
}
4242

4343
public static FormatterStep create(Provisioner provisioner) {
44-
// The default importRemover is GJF
45-
return create(GJF, provisioner);
44+
return create(DEFAULT_FORMATTER, provisioner);
4645
}
4746

4847
public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) {
4948
Objects.requireNonNull(provisioner, "provisioner");
5049
switch (unusedImportRemover) {
51-
case GJF:
50+
case DEFAULT_FORMATTER:
5251
return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner);
5352
case CLEANTHAT:
5453
return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2016-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 static com.diffplug.spotless.java.RemoveUnusedImportsStep.DEFAULT_FORMATTER;
19+
import static com.diffplug.spotless.java.RemoveUnusedImportsStep.NAME;
20+
import static com.diffplug.spotless.java.RemoveUnusedImportsStep.defaultFormatter;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
24+
import java.util.Collections;
25+
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
29+
30+
class RemoveUnusedImportsStepTest extends MavenIntegrationHarness {
31+
32+
@Test
33+
void testRemoveUnusedImports() throws Exception {
34+
writePomWithJavaSteps("<removeUnusedImports/>");
35+
36+
String path = "src/main/java/test.java";
37+
setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test");
38+
mavenRunner().withArguments("spotless:apply").runNoError();
39+
assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test");
40+
}
41+
42+
@Test
43+
void testDefaults() {
44+
assertEquals("palantir-java-format", defaultFormatter());
45+
assertEquals("palantir-java-format", DEFAULT_FORMATTER);
46+
assertEquals("removeUnusedImports", NAME);
47+
}
48+
49+
@Test
50+
void testCreateWithDefaultFormatter() {
51+
assertEquals(NAME, RemoveUnusedImportsStep.create((groupArtifact, version) -> Collections.emptySet()).getName());
52+
}
53+
54+
@Test
55+
void testCreateWithPalantirFormatter() {
56+
assertEquals(NAME, RemoveUnusedImportsStep.create("palantir-java-format", (groupArtifact, version) -> Collections.emptySet()).getName());
57+
}
58+
59+
@Test
60+
void testCreateWithCleanthatFormatter() {
61+
assertEquals(NAME, RemoveUnusedImportsStep.create("cleanthat-javaparser-unnecessaryimport", (groupArtifact, version) -> Collections.emptySet()).getName());
62+
}
63+
64+
@Test
65+
void testCreateWithInvalidFormatter() {
66+
assertThrows(IllegalArgumentException.class, () -> RemoveUnusedImportsStep.create("invalid-formatter", (groupArtifact, version) -> Collections.emptySet()));
67+
}
68+
69+
@Test
70+
void testCreateWithNullProvisioner() {
71+
assertThrows(NullPointerException.class, () -> RemoveUnusedImportsStep.create("palantir-java-format", null));
72+
}
73+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java

Lines changed: 0 additions & 33 deletions
This file was deleted.

testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java

Lines changed: 3 additions & 3 deletions
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.
@@ -25,7 +25,7 @@
2525
class RemoveUnusedImportsStep_withGoogleJavaFormatTest {
2626
@Test
2727
void behavior() throws Exception {
28-
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
28+
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
2929
StepHarness.forStep(step)
3030
.testResource("java/removeunusedimports/JavaCodeUnformatted.test", "java/removeunusedimports/JavaCodeFormatted.test")
3131
.testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test")
@@ -48,7 +48,7 @@ protected void setupTest(API api) {
4848

4949
@Override
5050
protected FormatterStep create() {
51-
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
51+
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
5252
}
5353
}.testEquals();
5454
}

0 commit comments

Comments
 (0)