Skip to content

Commit

Permalink
Show a fifth rule without any JavaDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 23, 2024
1 parent 7c8e33c commit 2b03265
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/test/resources/refaster/PicnicRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
58 changes: 57 additions & 1 deletion src/test/resources/refaster/PicnicRulesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public List<Recipe> getRecipeList() {
new FirstRuleRecipe(),
new SecondRuleRecipe(),
new ThirdRuleRecipe(),
new FourthRuleRecipe()
new FourthRuleRecipe(),
new FifthRuleRecipe()
);
}

Expand Down Expand Up @@ -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<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> 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
);
}
}

}

0 comments on commit 2b03265

Please sign in to comment.