Skip to content

Commit

Permalink
Derive OpenRewrite recipes from a subset of Refaster rules (#925)
Browse files Browse the repository at this point in the history
Using OpenRewrite's `rewrite-templating` annotation processor, Refaster rules
are now converted into matching recipes and bundled as part of the 
`error-prone-contrib` artifact. Note that not all rules are supported yet.
  • Loading branch information
timtebeek authored Jan 5, 2024
1 parent 0ec8ebe commit a2f44f8
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
27 changes: 26 additions & 1 deletion error-prone-contrib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.auto</groupId>
Expand Down Expand Up @@ -181,6 +181,31 @@
<artifactId>mongodb-driver-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java-11</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-templating</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.reactivestreams</groupId>
<artifactId>reactive-streams</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package tech.picnic.errorprone.refasterrules;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.openrewrite.java.Assertions.java;

import com.google.common.io.Resources;
import java.io.IOException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

// XXX: This class currently validates the OpenRewrite recipe generation by applying a single
// recipe. Generalize this setup to cover all generated recipes (for _all_ Refaster rule
// collections), ideally by reusing the `RefasterRulesTest` test resources. (This may introduce
// additional hurdles, as OpenRewrite removes obsolete imports, while Refaster doesn't.)
final class StringRulesRecipesTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new StringRulesRecipes());
}

@Test
void stringValueOf() {
// XXX: Use text blocks once supported.
rewriteRun(
java(
"import java.util.Objects;\n"
+ '\n'
+ "class Test {\n"
+ " String test(Object object) {\n"
+ " return Objects.toString(object);\n"
+ " }\n"
+ '}',
"class Test {\n"
+ " String test(Object object) {\n"
+ " return String.valueOf(object);\n"
+ " }\n"
+ '}'));
}

@Disabled("Not all rules are currently supported")
@Test
void allRules() throws IOException {
rewriteRun(
spec ->
spec.parser(JavaParser.fromJavaVersion().classpath("guava", "refaster-test-support")),
java(
loadResource("StringRulesTestInput.java"), loadResource("StringRulesTestOutput.java")));
}

private String loadResource(String resource) throws IOException {
return Resources.toString(Resources.getResource(getClass(), resource), UTF_8);
}
}
45 changes: 44 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,18 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-templating</artifactId>
<version>1.3.14</version>
</dependency>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-recipe-bom</artifactId>
<version>2.5.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-bom</artifactId>
Expand Down Expand Up @@ -891,6 +903,10 @@
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</path>
<path>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-templating</artifactId>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
Expand Down Expand Up @@ -1167,12 +1183,15 @@
non-distributed (i.e. Picnic-internal) deployable
artifacts (i.e. web services). -->
<includedLicense>Apache-2.0</includedLicense>
<includedLicense>BSD-2-Clause</includedLicense>
<includedLicense>BSD-3-Clause</includedLicense>
<includedLicense>CC0-1.0</includedLicense>
<includedLicense>CDDL-1.1</includedLicense>
<includedLicense>EPL-1.0</includedLicense>
<includedLicense>EPL-2.0</includedLicense>
<includedLicense>GPL-2.0-with-classpath-exception</includedLicense>
<includedLicense>ICU</includedLicense>
<includedLicense>LGPL-2.1+</includedLicense>
<includedLicense>LGPL-3.0+</includedLicense>
<includedLicense>MIT</includedLicense>
<includedLicense>MIT-0</includedLicense>
Expand All @@ -1195,10 +1214,20 @@
| The Apache Software License, Version 2.0
</licenseMerge>
<licenseMerge>
<!-- -->
<!-- XXX: Get projects referencing just "BSD"
to explicitly state the clause count. -->
BSD-2-Clause
| The BSD License
</licenseMerge>
<licenseMerge>
<!-- XXX: Get projects referencing just "BSD"
to explicitly state the clause count. -->
BSD-3-Clause
| 3-Clause BSD License
| BSD 3-clause
| BSD 3-Clause "New" or "Revised" License (BSD-3-Clause)
<!-- XXX: Typo; file ticket. -->
| BSD licence
| BSD License 3
| Eclipse Distribution License (New BSD License)
| New BSD License
Expand All @@ -1207,6 +1236,7 @@
<!-- -->
CC0-1.0
| CC0
| Public Domain, per Creative Commons CC0
</licenseMerge>
<licenseMerge>
<!-- -->
Expand All @@ -1225,6 +1255,12 @@
EPL-2.0
| Eclipse Public License - v 2.0
| Eclipse Public License v2.0
| EPL 2.0
</licenseMerge>
<licenseMerge>
<!-- -->
ICU
| Unicode/ICU License
</licenseMerge>
<licenseMerge>
<!-- -->
Expand All @@ -1236,6 +1272,12 @@
</licenseMerge>
<licenseMerge>
<!-- -->
LGPL-2.1+
| LGPL-2.1-or-later
</licenseMerge>
<licenseMerge>
<!-- XXX: Get projects referencing just "LGPL"
to explicitly state the license version. -->
LGPL-3.0+
| GNU Lesser Public License
</licenseMerge>
Expand All @@ -1245,6 +1287,7 @@
| MIT license
| MIT License
| The MIT License
| The MIT License (MIT)
</licenseMerge>
</licenseMerges>
<!-- Nearly no projects ship a "missing third party
Expand Down
15 changes: 15 additions & 0 deletions refaster-runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-templating</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
15 changes: 15 additions & 0 deletions refaster-test-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-templating</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit a2f44f8

Please sign in to comment.