From cb202b11ff3ac041fc188457e5420d87c119bec0 Mon Sep 17 00:00:00 2001 From: Vincent Potucek Date: Mon, 7 Jul 2025 17:06:51 +0200 Subject: [PATCH 1/2] Revert "Use palantir-java-format as default formatter in RemoveUnusedImportsStep" This reverts commit 2ebe138c --- CHANGES.md | 3 - .../java/RemoveUnusedImportsStep.java | 13 ++-- .../java/RemoveUnusedImportsStepTest.java | 73 ------------------- .../java/RemoveUnusedImportsStepTest.java | 33 +++++++++ ...dImportsStep_withGoogleJavaFormatTest.java | 6 +- 5 files changed, 43 insertions(+), 85 deletions(-) delete mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java diff --git a/CHANGES.md b/CHANGES.md index 2f9e031795..735eca7576 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,7 +13,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * Support for `idea` ([#2020](https://github.com/diffplug/spotless/pull/2020), [#2535](https://github.com/diffplug/spotless/pull/2535)) * Add support for removing wildcard imports via `removeWildcardImports` step. ([#2517](https://github.com/diffplug/spotless/pull/2517)) -* Use `palantir-java-format` as default formatter in `RemoveUnusedImportsStep`. ([#2541](https://github.com/diffplug/spotless/pull/2541)) * scalafmt: enforce version consistency between the version configured in Spotless and the version declared in Scalafmt config file ([#2460](https://github.com/diffplug/spotless/issues/2460)) ### Fixed * `SortPom` disable expandEmptyElements, to avoid empty body warnings. ([#2520](https://github.com/diffplug/spotless/pull/2520)) @@ -418,8 +417,6 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [2.26.0] - 2022-06-05 ### Added * Support for `editorConfigOverride` in `ktlint`. ([#1218](https://github.com/diffplug/spotless/pull/1218) fixes [#1193](https://github.com/diffplug/spotless/issues/1193)) -### Fixed -* `google-java-format` and `RemoveUnusedImportsStep` works on JDK16+ without jvm args workaround. ([#1224](https://github.com/diffplug/spotless/pull/1224) fixes [#834](https://github.com/diffplug/spotless/issues/834)) ## [2.25.3] - 2022-05-10 ### Fixed diff --git a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java index fcdb52503b..753678a86b 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,12 +22,12 @@ import com.diffplug.spotless.FormatterStep; import com.diffplug.spotless.Provisioner; -/** Uses palantir-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */ +/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */ public class RemoveUnusedImportsStep implements Serializable { private static final long serialVersionUID = 1L; static final String NAME = "removeUnusedImports"; - static final String DEFAULT_FORMATTER = "palantir-java-format"; + static final String GJF = "google-java-format"; static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport"; // https://github.com/solven-eu/cleanthat/blob/master/java/src/main/java/eu/solven/cleanthat/engine/java/refactorer/mutators/UnnecessaryImport.java @@ -37,17 +37,18 @@ public class RemoveUnusedImportsStep implements Serializable { private RemoveUnusedImportsStep() {} public static String defaultFormatter() { - return DEFAULT_FORMATTER; + return GJF; } public static FormatterStep create(Provisioner provisioner) { - return create(DEFAULT_FORMATTER, provisioner); + // The default importRemover is GJF + return create(GJF, provisioner); } public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) { Objects.requireNonNull(provisioner, "provisioner"); switch (unusedImportRemover) { - case DEFAULT_FORMATTER: + case GJF: return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner); case CLEANTHAT: return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner); diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java deleted file mode 100644 index 85713423cc..0000000000 --- a/plugin-maven/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStepTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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. - * 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 static com.diffplug.spotless.java.RemoveUnusedImportsStep.DEFAULT_FORMATTER; -import static com.diffplug.spotless.java.RemoveUnusedImportsStep.NAME; -import static com.diffplug.spotless.java.RemoveUnusedImportsStep.defaultFormatter; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.Collections; - -import org.junit.jupiter.api.Test; - -import com.diffplug.spotless.maven.MavenIntegrationHarness; - -class RemoveUnusedImportsStepTest extends MavenIntegrationHarness { - - @Test - void testRemoveUnusedImports() throws Exception { - writePomWithJavaSteps(""); - - String path = "src/main/java/test.java"; - setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); - mavenRunner().withArguments("spotless:apply").runNoError(); - assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); - } - - @Test - void testDefaults() { - assertEquals("palantir-java-format", defaultFormatter()); - assertEquals("palantir-java-format", DEFAULT_FORMATTER); - assertEquals("removeUnusedImports", NAME); - } - - @Test - void testCreateWithDefaultFormatter() { - assertEquals(NAME, RemoveUnusedImportsStep.create((groupArtifact, version) -> Collections.emptySet()).getName()); - } - - @Test - void testCreateWithPalantirFormatter() { - assertEquals(NAME, RemoveUnusedImportsStep.create("palantir-java-format", (groupArtifact, version) -> Collections.emptySet()).getName()); - } - - @Test - void testCreateWithCleanthatFormatter() { - assertEquals(NAME, RemoveUnusedImportsStep.create("cleanthat-javaparser-unnecessaryimport", (groupArtifact, version) -> Collections.emptySet()).getName()); - } - - @Test - void testCreateWithInvalidFormatter() { - assertThrows(IllegalArgumentException.class, () -> RemoveUnusedImportsStep.create("invalid-formatter", (groupArtifact, version) -> Collections.emptySet())); - } - - @Test - void testCreateWithNullProvisioner() { - assertThrows(NullPointerException.class, () -> RemoveUnusedImportsStep.create("palantir-java-format", null)); - } -} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java new file mode 100644 index 0000000000..3689fa8f98 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveUnusedImportsStepTest.java @@ -0,0 +1,33 @@ +/* + * 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. + * 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 RemoveUnusedImportsStepTest extends MavenIntegrationHarness { + + @Test + void testRemoveUnusedInports() throws Exception { + writePomWithJavaSteps(""); + + String path = "src/main/java/test.java"; + setFile(path).toResource("java/removeunusedimports/JavaCodeWithPackageUnformatted.test"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile(path).sameAsResource("java/removeunusedimports/JavaCodeWithPackageFormatted.test"); + } +} diff --git a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java index 766816a659..44699a8215 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2025 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,7 +25,7 @@ class RemoveUnusedImportsStep_withGoogleJavaFormatTest { @Test void behavior() throws Exception { - FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral()); + FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral()); StepHarness.forStep(step) .testResource("java/removeunusedimports/JavaCodeUnformatted.test", "java/removeunusedimports/JavaCodeFormatted.test") .testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test") @@ -48,7 +48,7 @@ protected void setupTest(API api) { @Override protected FormatterStep create() { - return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral()); + return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral()); } }.testEquals(); } From 62b12ca8fb2b17f04bf3028c97e73f99f682b5a9 Mon Sep 17 00:00:00 2001 From: ntwigg Date: Mon, 7 Jul 2025 14:03:03 -0700 Subject: [PATCH 2/2] spotlessApply --- .../com/diffplug/spotless/java/RemoveUnusedImportsStep.java | 2 +- .../java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java index 753678a86b..71b6dc4281 100644 --- a/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java +++ b/lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java @@ -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. diff --git a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java index 44699a8215..84ad3f1f61 100644 --- a/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/java/RemoveUnusedImportsStep_withGoogleJavaFormatTest.java @@ -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.