From a2f44f82f2c815450c4fc3ee8b35452c03d053f1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 5 Jan 2024 21:59:22 +0100 Subject: [PATCH] Derive OpenRewrite recipes from a subset of Refaster rules (#925) 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. --- error-prone-contrib/pom.xml | 27 ++++++++- .../refasterrules/StringRulesRecipesTest.java | 56 +++++++++++++++++++ pom.xml | 45 ++++++++++++++- refaster-runner/pom.xml | 15 +++++ refaster-test-support/pom.xml | 15 +++++ 5 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/StringRulesRecipesTest.java diff --git a/error-prone-contrib/pom.xml b/error-prone-contrib/pom.xml index f268bbeab3..e0fd16e62d 100644 --- a/error-prone-contrib/pom.xml +++ b/error-prone-contrib/pom.xml @@ -60,7 +60,7 @@ com.fasterxml.jackson.core jackson-annotations - test + provided com.google.auto @@ -181,6 +181,31 @@ mongodb-driver-core test + + org.openrewrite + rewrite-core + provided + + + org.openrewrite + rewrite-java + provided + + + org.openrewrite + rewrite-java-11 + test + + + org.openrewrite + rewrite-templating + provided + + + org.openrewrite + rewrite-test + test + org.reactivestreams reactive-streams diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/StringRulesRecipesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/StringRulesRecipesTest.java new file mode 100644 index 0000000000..5121a065f7 --- /dev/null +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/StringRulesRecipesTest.java @@ -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); + } +} diff --git a/pom.xml b/pom.xml index 80afbcc0ec..5cc94ebb11 100644 --- a/pom.xml +++ b/pom.xml @@ -451,6 +451,18 @@ + + org.openrewrite + rewrite-templating + 1.3.14 + + + org.openrewrite.recipe + rewrite-recipe-bom + 2.5.4 + pom + import + org.slf4j slf4j-bom @@ -891,6 +903,10 @@ com.google.auto.service auto-service + + org.openrewrite + rewrite-templating + --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED @@ -1167,12 +1183,15 @@ non-distributed (i.e. Picnic-internal) deployable artifacts (i.e. web services). --> Apache-2.0 + BSD-2-Clause BSD-3-Clause CC0-1.0 CDDL-1.1 EPL-1.0 EPL-2.0 GPL-2.0-with-classpath-exception + ICU + LGPL-2.1+ LGPL-3.0+ MIT MIT-0 @@ -1195,10 +1214,20 @@ | The Apache Software License, Version 2.0 - + + BSD-2-Clause + | The BSD License + + + BSD-3-Clause | 3-Clause BSD License | BSD 3-clause + | BSD 3-Clause "New" or "Revised" License (BSD-3-Clause) + + | BSD licence | BSD License 3 | Eclipse Distribution License (New BSD License) | New BSD License @@ -1207,6 +1236,7 @@ CC0-1.0 | CC0 + | Public Domain, per Creative Commons CC0 @@ -1225,6 +1255,12 @@ EPL-2.0 | Eclipse Public License - v 2.0 | Eclipse Public License v2.0 + | EPL 2.0 + + + + ICU + | Unicode/ICU License @@ -1236,6 +1272,12 @@ + LGPL-2.1+ + | LGPL-2.1-or-later + + + LGPL-3.0+ | GNU Lesser Public License @@ -1245,6 +1287,7 @@ | MIT license | MIT License | The MIT License + | The MIT License (MIT)