Skip to content

Commit 3ec6538

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

38 files changed

+107
-64
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/src/main/java/com/diffplug/spotless/java/FormatAnnotationsStep.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ private static final class State implements Serializable {
432432

433433
// group 1 is the basename of the annotation.
434434
private static final String annoNoArgRegex = "@(?:[A-Za-z_][A-Za-z0-9_.]*\\.)?([A-Za-z_][A-Za-z0-9_]*)";
435-
private static final Pattern annoNoArgPattern = Pattern.compile(annoNoArgRegex);
436435
// 3 non-empty cases: () (".*") (.*)
437436
private static final String annoArgRegex = "(?:\\(\\)|\\(\"[^\"]*\"\\)|\\([^\")][^)]*\\))?";
438437
// group 1 is the basename of the annotation.

plugin-maven/src/main/resources/META-INF/m2e/lifecycle-mapping-metadata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@
2929
</action>
3030
</pluginExecution>
3131
</pluginExecutions>
32-
</lifecycleMappingMetadata>
32+
</lifecycleMappingMetadata>

plugin-maven/src/test/resources/.mvn/wrapper/maven-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# maven-plugin-plugin 3.5 (see pom-build.xml.mustache).
33
# The Maven version used for build and test is not relevant for compatibility.
44
# Instead the versions of the provided dependencies (see pom-build.xml.mustache) must match.
5-
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5-bin.zip
5+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.2.5/apache-maven-3.2.5-bin.zip

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ plugins {
2323
id 'com.gradle.develocity' version '3.19.2'
2424
// https://github.com/equodev/equo-ide/blob/main/plugin-gradle/CHANGELOG.md
2525
id 'dev.equo.ide' version '1.7.8' apply false
26+
id 'org.openrewrite.rewrite' version '7.12.1' apply false
2627
}
2728

2829
dependencyResolutionManagement {

testlib/src/main/resources/biome/config/css-enabled.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
"linter": {
1717
"enabled": false
1818
}
19-
}
19+
}

0 commit comments

Comments
 (0)