From 2b0326568c2d526fab44ef209baf36901f64f603 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 23 Dec 2024 13:03:50 +0100 Subject: [PATCH] Show a fifth rule without any JavaDoc --- src/test/resources/refaster/PicnicRules.java | 13 +++++ .../refaster/PicnicRulesRecipes.java | 58 ++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/test/resources/refaster/PicnicRules.java b/src/test/resources/refaster/PicnicRules.java index 69c142c..079a98b 100644 --- a/src/test/resources/refaster/PicnicRules.java +++ b/src/test/resources/refaster/PicnicRules.java @@ -90,4 +90,17 @@ String after(String s, String s1, String s2) { return s != null ? s.replaceAll(s1, s2) : s; } } + + // No JavaDoc + public static class FifthRule { + @BeforeTemplate + String before(String s, String s1, String s2) { + return s.replaceAll(s1, s2); + } + + @AfterTemplate + String after(String s, String s1, String s2) { + return s != null ? s.replaceAll(s1, s2) : s; + } + } } diff --git a/src/test/resources/refaster/PicnicRulesRecipes.java b/src/test/resources/refaster/PicnicRulesRecipes.java index 0ce98ae..6c884dc 100644 --- a/src/test/resources/refaster/PicnicRulesRecipes.java +++ b/src/test/resources/refaster/PicnicRulesRecipes.java @@ -61,7 +61,8 @@ public List getRecipeList() { new FirstRuleRecipe(), new SecondRuleRecipe(), new ThirdRuleRecipe(), - new FourthRuleRecipe() + new FourthRuleRecipe(), + new FifthRuleRecipe() ); } @@ -285,4 +286,59 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code PicnicRules.FifthRule}. + */ + @SuppressWarnings("all") + @NullMarked + @Generated("org.openrewrite.java.template.processor.RefasterTemplateProcessor") + public static class FifthRuleRecipe extends Recipe { + + /** + * Instantiates a new instance. + */ + public FifthRuleRecipe() {} + + @Override + public String getDisplayName() { + return "Refaster template `PicnicRules.FifthRule`"; + } + + @Override + public String getDescription() { + return "Recipe created for the following Refaster template:\n```java\npublic static class FifthRule {\n \n @BeforeTemplate()\n String before(String s, String s1, String s2) {\n return s.replaceAll(s1, s2);\n }\n \n @AfterTemplate()\n String after(String s, String s1, String s2) {\n return s != null ? s.replaceAll(s1, s2) : s;\n }\n}\n```\n."; + } + + @Override + public TreeVisitor getVisitor() { + JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.replaceAll(#{s1:any(java.lang.String)}, #{s2:any(java.lang.String)})") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)} != null ? #{s}.replaceAll(#{s1:any(java.lang.String)}, #{s2:any(java.lang.String)}) : #{s}") + .build(); + + @Override + public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { + JavaTemplate.Matcher matcher; + if ((matcher = before.matcher(getCursor())).find()) { + return embed( + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1), matcher.parameter(2)), + getCursor(), + ctx, + SHORTEN_NAMES, SIMPLIFY_BOOLEANS + ); + } + return super.visitMethodInvocation(elem, ctx); + } + + }; + return Preconditions.check( + new UsesMethod<>("java.lang.String replaceAll(..)", true), + javaVisitor + ); + } + } + }