Skip to content

Commit 154a33d

Browse files
author
Vincent Potucek
committed
[Experimental] Add rewrite support for RemoveUnusedPrivateMethods & RemoveUnusedImports
1 parent ff973d3 commit 154a33d

File tree

88 files changed

+192
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+192
-199
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
uses: gradle/actions/setup-gradle@v4
3232
- name: spotlessCheck
3333
run: ./gradlew spotlessCheck
34+
- name: rewriteDryRun
35+
run: ./gradlew rewriteDryRun
3436
- name: assemble testClasses
3537
run: ./gradlew assemble testClasses
3638
build:
@@ -66,10 +68,10 @@ jobs:
6668
uses: gradle/actions/setup-gradle@v4
6769
- name: build (maven-only)
6870
if: matrix.kind == 'maven'
69-
run: ./gradlew :plugin-maven:build -x spotlessCheck
71+
run: ./gradlew :plugin-maven:build -x spotlessCheck -x rewriteDryRun
7072
- name: build (everything-but-maven)
7173
if: matrix.kind == 'gradle'
72-
run: ./gradlew build -x spotlessCheck -PSPOTLESS_EXCLUDE_MAVEN=true
74+
run: ./gradlew build -x spotlessCheck -x rewriteDryRun -PSPOTLESS_EXCLUDE_MAVEN=true
7375
- name: test npm
7476
if: matrix.kind == 'npm'
7577
run: ./gradlew testNpm

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1616
* Adds support for worktrees (fixes [#1765](https://github.com/diffplug/spotless/issues/1765))
1717
* Bump default `google-java-format` version to latest `1.24.0` -> `1.28.0`. ([#2345](https://github.com/diffplug/spotless/pull/2345))
1818
* Bump default `ktlint` version to latest `1.5.0` -> `1.7.1`. ([#2555](https://github.com/diffplug/spotless/pull/2555))
19+
* [Experimental] Add `rewrite` support for `RemoveUnusedPrivateMethods` & `RemoveUnusedImports` ([#2576](https://github.com/diffplug/spotless/pull/2576))
1920

2021
## [3.3.1] - 2025-07-21
2122
### Fixed

build.gradle

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
apply plugin: 'dev.equo.ide'
2+
3+
apply from: rootProject.file('gradle/changelog.gradle')
4+
apply from: rootProject.file('gradle/java-publish.gradle')
5+
apply from: rootProject.file('gradle/spotless-freshmark.gradle')
6+
7+
allprojects {
8+
apply from: rootProject.file('gradle/spotless.gradle')
9+
apply from: rootProject.file('gradle/rewrite.gradle')
10+
}
11+
212
equoIde {
313
branding().title('Spotless').icon(file('_images/spotless_logo.png'))
414
welcome().openUrl('https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md')
@@ -9,21 +19,6 @@ repositories {
919
mavenCentral()
1020
}
1121

12-
apply from: rootProject.file('gradle/java-publish.gradle')
13-
apply from: rootProject.file('gradle/changelog.gradle')
14-
allprojects {
15-
apply from: rootProject.file('gradle/spotless.gradle')
16-
}
17-
apply from: rootProject.file('gradle/spotless-freshmark.gradle')
18-
19-
spotless {
20-
groovyGradle {
21-
target '*.gradle', 'gradle/*.gradle'
22-
}
23-
format 'dotfiles', {
24-
target '.gitignore', '.gitattributes', '.editorconfig'
25-
leadingTabsToSpaces(2)
26-
trimTrailingWhitespace()
27-
endWithNewline()
28-
}
22+
dependencies {
23+
rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.12.0", "org.openrewrite.recipe:rewrite-third-party:0.24.1")
2924
}

gradle/rewrite.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'org.openrewrite.rewrite'
2+
3+
rewrite {
4+
activeRecipe(
5+
"org.openrewrite.gradle.GradleBestPractices",
6+
"org.openrewrite.java.RemoveUnusedImports",
7+
"org.openrewrite.java.format.RemoveTrailingWhitespace",
8+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods",
9+
"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables",
10+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields",
11+
"org.openrewrite.text.EndOfLineAtEndOfFile",
12+
"tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes"
13+
)
14+
exclusions.add("**package-info.java") // bug
15+
exclusions.add("**gradle/java-publish.gradle") // bug
16+
failOnDryRunResults = true
17+
}
18+
19+
// off switch for release: ' -x check' or ' -x rewriteDryRun'
20+
//tasks {
21+
// check.dependsOn(rewriteDryRun)
22+
//}
23+
24+
// > Task :rewriteDryRun FAILED
25+
//
26+
//2 problems were found storing the configuration cache.
27+
//- Task `:rewriteDryRun` of type `org.openrewrite.gradle.RewriteDryRunTask`: cannot serialize object of type 'org.gradle.api.internal.project.DefaultProject', a subtype of 'org.gradle.api.Project', as these are not supported with the configuration cache.
28+
// See https://docs.gradle.org/8.14.3/userguide/configuration_cache.html#config_cache:requirements:disallowed_types
29+
//- Task `:rewriteDryRun` of type `org.openrewrite.gradle.RewriteDryRunTask`: invocation of 'Task.project' at execution time is unsupported.
30+
// See https://docs.gradle.org/8.14.3/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution
31+
32+
// FAILURE: Build failed with an exception.
33+
//
34+
//* What went wrong:
35+
//Execution failed for task ':rewriteDryRun'.
36+
//> java.lang.RuntimeException: Applying recipes would make changes. See logs for more details.

gradle/spotless.gradle

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
apply plugin: 'com.diffplug.spotless'
2+
23
spotless {
4+
groovyGradle {
5+
target '*.gradle', 'gradle/*.gradle'
6+
greclipse().configFile rootProject.files('gradle/spotless.eclipseformat.xml', 'gradle/spotless.groovyformat.prefs')
7+
}
8+
format 'dotfiles', {
9+
target '.gitignore', '.gitattributes', '.editorconfig'
10+
leadingTabsToSpaces(2)
11+
trimTrailingWhitespace()
12+
endWithNewline()
13+
}
314
def noInternalDepsClosure = {
415
String text = it
516
/*
@@ -12,22 +23,16 @@ spotless {
1223
throw new AssertionError("Accidental internal import")
1324
}
1425
}
15-
if (project != rootProject) {
16-
// the rootProject doesn't have any java
17-
java {
18-
ratchetFrom 'origin/main'
19-
bumpThisNumberIfACustomStepChanges(1)
20-
licenseHeaderFile rootProject.file('gradle/spotless.license')
21-
importOrderFile rootProject.file('gradle/spotless.importorder')
22-
eclipse().configFile rootProject.file('gradle/spotless.eclipseformat.xml')
23-
trimTrailingWhitespace()
24-
removeUnusedImports()
25-
formatAnnotations()
26-
custom 'noInternalDeps', noInternalDepsClosure
27-
}
28-
}
29-
groovyGradle {
30-
target '*.gradle'
31-
greclipse().configFile rootProject.files('gradle/spotless.eclipseformat.xml', 'gradle/spotless.groovyformat.prefs')
26+
java {
27+
target '*.gitignore'
28+
ratchetFrom 'origin/main'
29+
bumpThisNumberIfACustomStepChanges(1)
30+
licenseHeaderFile rootProject.file('gradle/spotless.license')
31+
importOrderFile rootProject.file('gradle/spotless.importorder')
32+
eclipse().configFile rootProject.file('gradle/spotless.eclipseformat.xml')
33+
trimTrailingWhitespace()
34+
removeUnusedImports()
35+
formatAnnotations()
36+
custom 'noInternalDeps', noInternalDepsClosure
3237
}
3338
}

lib-extra/src/test/java/com/diffplug/spotless/extra/eclipse/EclipseResourceHarness.java

Lines changed: 1 addition & 3 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.
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.extra.eclipse;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
2018
import java.io.File;
2119
import java.util.Arrays;
2220

lib-extra/src/test/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStepTest.java

Lines changed: 1 addition & 3 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.
@@ -24,8 +24,6 @@
2424
import com.diffplug.spotless.extra.eclipse.EquoResourceHarness;
2525

2626
public class GrEclipseFormatterStepTest extends EquoResourceHarness {
27-
private final static String INPUT = "class F{ def m(){} }";
28-
private final static String EXPECTED = "class F{\n\tdef m(){}\n}";
2927

3028
public GrEclipseFormatterStepTest() {
3129
super(GrEclipseFormatterStep.createBuilder(TestProvisioner.mavenCentral()));

lib/src/compatKtLint0Dot49Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot49Dot0Adapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 DiffPlug
2+
* Copyright 2023-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.
@@ -27,9 +27,6 @@
2727
import java.util.stream.Collectors;
2828
import java.util.stream.Stream;
2929

30-
import org.slf4j.Logger;
31-
import org.slf4j.LoggerFactory;
32-
3330
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3;
3431
import com.pinterest.ktlint.rule.engine.api.Code;
3532
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults;
@@ -55,8 +52,6 @@
5552

5653
public class KtLintCompat0Dot49Dot0Adapter implements KtLintCompatAdapter {
5754

58-
private static final Logger logger = LoggerFactory.getLogger(KtLintCompat0Dot49Dot0Adapter.class);
59-
6055
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
6156

6257
static {

lib/src/compatKtLint0Dot50Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat0Dot50Dot0Adapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 DiffPlug
2+
* Copyright 2023-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,9 +25,6 @@
2525
import java.util.stream.Collectors;
2626
import java.util.stream.Stream;
2727

28-
import org.slf4j.Logger;
29-
import org.slf4j.LoggerFactory;
30-
3128
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3;
3229
import com.pinterest.ktlint.rule.engine.api.Code;
3330
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults;
@@ -55,8 +52,6 @@
5552

5653
public class KtLintCompat0Dot50Dot0Adapter implements KtLintCompatAdapter {
5754

58-
private static final Logger logger = LoggerFactory.getLogger(KtLintCompat0Dot50Dot0Adapter.class);
59-
6055
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
6156

6257
static {

lib/src/compatKtLint1Dot0Dot0/java/com/diffplug/spotless/glue/ktlint/compat/KtLintCompat1Dot0Dot0Adapter.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 DiffPlug
2+
* Copyright 2023-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.
@@ -24,9 +24,6 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626

27-
import org.slf4j.Logger;
28-
import org.slf4j.LoggerFactory;
29-
3027
import com.pinterest.ktlint.cli.ruleset.core.api.RuleSetProviderV3;
3128
import com.pinterest.ktlint.rule.engine.api.Code;
3229
import com.pinterest.ktlint.rule.engine.api.EditorConfigDefaults;
@@ -54,8 +51,6 @@
5451

5552
public class KtLintCompat1Dot0Dot0Adapter implements KtLintCompatAdapter {
5653

57-
private static final Logger logger = LoggerFactory.getLogger(KtLintCompat1Dot0Dot0Adapter.class);
58-
5954
private static final List<EditorConfigProperty<?>> DEFAULT_EDITOR_CONFIG_PROPERTIES;
6055

6156
static {

0 commit comments

Comments
 (0)