-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ne-contrib/src/test/java/tech/picnic/errorprone/refasterrules/StringRulesRecipesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters