Skip to content

Use palantir-java-format as default formatter in RemoveUnusedImportsStep #2541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* 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))

## [3.1.2] - 2025-05-27
### Fixed
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 All @@ -22,12 +22,12 @@
import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.Provisioner;

/** Uses google-java-format or cleanthat.UnnecessaryImport, but only to remove unused imports. */
/** Uses palantir-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 GJF = "google-java-format";
static final String DEFAULT_FORMATTER = "palantir-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
Expand All @@ -37,18 +37,17 @@ public class RemoveUnusedImportsStep implements Serializable {
private RemoveUnusedImportsStep() {}

public static String defaultFormatter() {
return GJF;
return DEFAULT_FORMATTER;
}

public static FormatterStep create(Provisioner provisioner) {
// The default importRemover is GJF
return create(GJF, provisioner);
return create(DEFAULT_FORMATTER, provisioner);
}

public static FormatterStep create(String unusedImportRemover, Provisioner provisioner) {
Objects.requireNonNull(provisioner, "provisioner");
switch (unusedImportRemover) {
case GJF:
case DEFAULT_FORMATTER:
return GoogleJavaFormatStep.createRemoveUnusedImportsOnly(provisioner);
case CLEANTHAT:
return CleanthatJavaStep.createWithStepName(NAME, CleanthatJavaStep.defaultGroupArtifact(), CleanthatJavaStep.defaultVersion(), "99.9", List.of(CLEANTHAT_MUTATOR), List.of(), false, provisioner);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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("<removeUnusedImports/>");

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

convention is same package, to be able to access package private.

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));
}
}

This file was deleted.

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 All @@ -25,7 +25,7 @@
class RemoveUnusedImportsStep_withGoogleJavaFormatTest {
@Test
void behavior() throws Exception {
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
FormatterStep step = RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
StepHarness.forStep(step)
.testResource("java/removeunusedimports/JavaCodeUnformatted.test", "java/removeunusedimports/JavaCodeFormatted.test")
.testResource("java/removeunusedimports/JavaCodeWithLicenseUnformatted.test", "java/removeunusedimports/JavaCodeWithLicenseFormatted.test")
Expand All @@ -48,7 +48,7 @@ protected void setupTest(API api) {

@Override
protected FormatterStep create() {
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.GJF, TestProvisioner.mavenCentral());
return RemoveUnusedImportsStep.create(RemoveUnusedImportsStep.DEFAULT_FORMATTER, TestProvisioner.mavenCentral());
}
}.testEquals();
}
Expand Down
Loading