Skip to content

Commit bc88c83

Browse files
author
Vincent Potucek
committed
PoC: Add rewrite support for errorprone.refasterrules
1 parent db6a60e commit bc88c83

File tree

109 files changed

+317
-246
lines changed

Some content is hidden

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

109 files changed

+317
-246
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ concurrency:
1313
cancel-in-progress: true
1414
jobs:
1515
sanityCheck:
16-
name: spotlessCheck assemble testClasses
16+
name: QA (spotlessCheck, assemble testClasses, rewriteDryRun)
1717
runs-on: ubuntu-latest
1818
env:
1919
buildcacheuser: ${{ secrets.BUILDCACHE_USER }}
@@ -33,6 +33,8 @@ jobs:
3333
run: ./gradlew spotlessCheck
3434
- name: assemble testClasses
3535
run: ./gradlew assemble testClasses
36+
- name: rewriteDryRun
37+
run: ./gradlew rewriteDryRun --build-cache --info
3638
build:
3739
needs: sanityCheck
3840
strategy:
@@ -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+
* PoC: Add `rewrite` support for `errorprone.refasterrules` ([#2576](https://github.com/diffplug/spotless/pull/2576))
1920

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

build.gradle

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
apply plugin: 'dev.equo.ide'
2-
equoIde {
3-
branding().title('Spotless').icon(file('_images/spotless_logo.png'))
4-
welcome().openUrl('https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md')
5-
gradleBuildship().autoImport('.')
6-
}
72

8-
repositories {
9-
mavenCentral()
10-
}
11-
12-
apply from: rootProject.file('gradle/java-publish.gradle')
133
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+
147
allprojects {
158
apply from: rootProject.file('gradle/spotless.gradle')
9+
apply from: rootProject.file('gradle/rewrite.gradle')
10+
}
11+
12+
equoIde {
13+
branding().title('Spotless').icon(file('_images/spotless_logo.png'))
14+
welcome().openUrl('https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md')
15+
gradleBuildship().autoImport('.')
1616
}
17-
apply from: rootProject.file('gradle/spotless-freshmark.gradle')
1817

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-
}
18+
dependencies {
19+
rewrite("org.openrewrite.recipe:rewrite-migrate-java:3.14.1")
20+
rewrite("org.openrewrite.recipe:rewrite-rewrite:0.10.1")
21+
rewrite("org.openrewrite.recipe:rewrite-static-analysis:2.14.0")
22+
rewrite("org.openrewrite.recipe:rewrite-third-party:0.24.1")
2923
}

gradle/rewrite.gradle

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.java.recipes.JavaRecipeBestPractices",
9+
"org.openrewrite.java.recipes.RecipeTestingBestPractices",
10+
"org.openrewrite.staticanalysis.EmptyBlock",
11+
"org.openrewrite.staticanalysis.EqualsAvoidsNull",
12+
"org.openrewrite.staticanalysis.JavaApiBestPractices",
13+
"org.openrewrite.staticanalysis.LowercasePackage",
14+
"org.openrewrite.staticanalysis.MissingOverrideAnnotation",
15+
"org.openrewrite.staticanalysis.ModifierOrder",
16+
"org.openrewrite.staticanalysis.NoFinalizer",
17+
"org.openrewrite.staticanalysis.RemoveCallsToSystemGc",
18+
"org.openrewrite.staticanalysis.RemoveUnneededAssertion",
19+
"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables",
20+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields",
21+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods",
22+
"org.openrewrite.staticanalysis.StringLiteralEquality",
23+
"org.openrewrite.staticanalysis.UnnecessaryParentheses",
24+
"org.openrewrite.text.EndOfLineAtEndOfFile",
25+
"tech.picnic.errorprone.refasterrules.BugCheckerRulesRecipes",
26+
"tech.picnic.errorprone.refasterrules.CollectionRulesRecipes",
27+
"tech.picnic.errorprone.refasterrules.FileRulesRecipes",
28+
"tech.picnic.errorprone.refasterrules.NullRulesRecipes",
29+
"tech.picnic.errorprone.refasterrules.StreamRulesRecipes",
30+
"tech.picnic.errorprone.refasterrules.StringRulesRecipes",
31+
"tech.picnic.errorprone.refasterrules.SuggestedFixRulesRecipes",
32+
//"org.openrewrite.java.migrate.UpgradeToJava17",
33+
//"org.openrewrite.staticanalysis.CodeCleanup",
34+
//"org.openrewrite.staticanalysis.UnnecessaryThrows",
35+
)
36+
// bugs
37+
exclusions.add("**AJacksonFormatterFunc.java")
38+
exclusions.add("**NpmPathResolver.java")
39+
exclusions.add("**NpmTestsWithoutNpmInstallationTest_gradle_node_plugin_example_*.gradle")
40+
exclusions.add("**gradle/changelog.gradle")
41+
exclusions.add("**gradle/java-publish.gradle")
42+
exclusions.add("**lib-extra/build.gradle")
43+
exclusions.add("**lib/build.gradle")
44+
exclusions.add("**package-info.java")
45+
exclusions.add("**plugin-maven/build.gradle")
46+
exclusions.add("**settings.gradle")
47+
exportDatatables = true
48+
failOnDryRunResults = true
49+
}

gradle/spotless.gradle

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
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
/*
617
* No good way to get around using this import:
718
* https://github.com/gradle/gradle/issues/3191
819
*/
920
String regex = "import org\\.gradle\\.api\\.internal\\.(?!plugins\\.DslObject)(?!project\\.ProjectInternal)"
10-
if ((text.contains('import org.gradle.internal.') || text.find(regex)) &&
11-
!text.contains('def noInternalDepsClosure')) {
21+
if ((text.contains('import org.gradle.internal.') || text.find(regex))
22+
&& !text.contains('def noInternalDepsClosure')) {
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/jdt/java/com/diffplug/spotless/extra/glue/jdt/DefaultJavaElementComparator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ private boolean isSortPreserved(BodyDeclaration bodyDeclaration) {
368368
}
369369

370370
private int preserveRelativeOrder(BodyDeclaration bodyDeclaration1, BodyDeclaration bodyDeclaration2) {
371-
int value1 = ((Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER));
372-
int value2 = ((Integer) bodyDeclaration2.getProperty(CompilationUnitSorter.RELATIVE_ORDER));
371+
int value1 = (Integer) bodyDeclaration1.getProperty(CompilationUnitSorter.RELATIVE_ORDER);
372+
int value2 = (Integer) bodyDeclaration2.getProperty(CompilationUnitSorter.RELATIVE_ORDER);
373373
return value1 - value2;
374374
}
375375

lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/JdtFlags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ && isInterfaceOrAnnotationMember(bodyDeclaration))
4848
}
4949

5050
private static boolean isPackageVisible(BodyDeclaration bodyDeclaration) {
51-
return (!isPrivate(bodyDeclaration) && !isProtected(bodyDeclaration) && !isPublic(bodyDeclaration));
51+
return !isPrivate(bodyDeclaration) && !isProtected(bodyDeclaration) && !isPublic(bodyDeclaration);
5252
}
5353

5454
private static boolean isPrivate(BodyDeclaration bodyDeclaration) {

lib-extra/src/main/java/com/diffplug/spotless/extra/EquoBasedStepBuilder.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@
2020
import java.io.File;
2121
import java.io.IOException;
2222
import java.io.Serializable;
23-
import java.util.ArrayList;
24-
import java.util.Collection;
25-
import java.util.List;
26-
import java.util.Map;
27-
import java.util.Optional;
28-
import java.util.Properties;
23+
import java.util.*;
2924

3025
import javax.annotation.Nullable;
3126

@@ -127,7 +122,7 @@ public FormatterStep build() {
127122
var roundtrippableState = new EquoStep(formatterVersion, settingProperties, settingXml, FileSignature.promise(settingsFiles), JarState.promise(() -> {
128123
P2QueryResult query;
129124
try {
130-
if (null != cacheDirectory) {
125+
if (cacheDirectory != null) {
131126
CacheLocations.override_p2data = cacheDirectory.toPath().resolve("dev/equo/p2-data").toFile();
132127
}
133128
query = createModelWithMirrors().query(P2ClientCache.PREFER_OFFLINE, P2QueryCache.ALLOW);
@@ -190,8 +185,8 @@ static class EquoStep implements Serializable {
190185
ImmutableMap<String, String> stepProperties) {
191186

192187
this.semanticVersion = semanticVersion;
193-
this.settingProperties = Optional.ofNullable(settingProperties).orElse(new ArrayList<>());
194-
this.settingXml = Optional.ofNullable(settingXml).orElse(new ArrayList<>());
188+
this.settingProperties = Objects.requireNonNullElse(settingProperties, new ArrayList<>());
189+
this.settingXml = Objects.requireNonNullElse(settingXml, new ArrayList<>());
195190
this.settingsPromise = settingsPromise;
196191
this.jarPromise = jarPromise;
197192
this.stepProperties = stepProperties;
@@ -218,8 +213,8 @@ public static class State implements Serializable {
218213
public State(String semanticVersion, JarState jarState, List<String> settingProperties, List<String> settingXml, FileSignature settingsFiles, ImmutableMap<String, String> stepProperties) {
219214
this.semanticVersion = semanticVersion;
220215
this.jarState = jarState;
221-
this.settingProperties = Optional.ofNullable(settingProperties).orElse(new ArrayList<>());
222-
this.settingXml = Optional.ofNullable(settingXml).orElse(new ArrayList<>());
216+
this.settingProperties = Objects.requireNonNullElse(settingProperties, new ArrayList<>());
217+
this.settingXml = Objects.requireNonNullElse(settingXml, new ArrayList<>());
223218
this.settingsFiles = settingsFiles;
224219
this.stepProperties = stepProperties;
225220
}

lib-extra/src/main/java/com/diffplug/spotless/extra/GitAttributesLineEndings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,15 @@ static class CachedEndings implements Serializable {
146146
private static final long serialVersionUID = -2534772773057900619L;
147147

148148
/** this is transient, to simulate PathSensitive.RELATIVE */
149-
transient final String rootDir;
149+
final transient String rootDir;
150150
/** the line ending used for most files */
151151
final String defaultEnding;
152152
/** any exceptions to that default, in terms of relative path from rootDir */
153153
final ConcurrentRadixTree<String> hasNonDefaultEnding = new ConcurrentRadixTree<>(new DefaultCharSequenceNodeFactory());
154154

155155
CachedEndings(File projectDir, Runtime runtime, Iterable<File> toFormat) {
156156
String rootPath = FileSignature.pathNativeToUnix(projectDir.getAbsolutePath());
157-
rootDir = rootPath.equals("/") ? rootPath : rootPath + "/";
157+
rootDir = "/".equals(rootPath) ? rootPath : rootPath + "/";
158158
defaultEnding = runtime.defaultEnding;
159159
for (File file : toFormat) {
160160
String ending = runtime.getEndingFor(file);

lib-extra/src/main/java/com/diffplug/spotless/extra/GitRatchet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ private static boolean worktreeIsCleanCheckout(TreeWalk treeWalk) {
132132
return treeWalk.idEqual(TREE, WORKDIR);
133133
}
134134

135-
private final static int TREE = 0;
136-
private final static int INDEX = 1;
137-
private final static int WORKDIR = 2;
135+
private static final int TREE = 0;
136+
private static final int INDEX = 1;
137+
private static final int WORKDIR = 2;
138138

139139
Map<File, Repository> gitRoots = new HashMap<>();
140140
Table<Repository, String, ObjectId> rootTreeShaCache = HashBasedTable.create();

0 commit comments

Comments
 (0)