diff --git a/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java b/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java index b716ef1d..f5e4136b 100644 --- a/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/RefasterTemplateProcessor.java @@ -528,7 +528,9 @@ private String joinPreconditions(Collection preconditions, String op, in } private static String lambdaCastType(Class type, JCTree.JCMethodDecl method) { - if (type == JCTree.JCMethodInvocation.class && method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement) { + if (type == JCTree.JCMethodInvocation.class + && method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement + && !(method.getReturnType().type instanceof Type.JCVoidType)) { return ""; } int paramCount = method.params.size(); diff --git a/src/test/resources/recipes/MultipleDereferences.java b/src/test/resources/recipes/MultipleDereferences.java index 8c72fb05..f3100f92 100644 --- a/src/test/resources/recipes/MultipleDereferences.java +++ b/src/test/resources/recipes/MultipleDereferences.java @@ -3,8 +3,24 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + public class MultipleDereferences { + public static class VoidType { + @BeforeTemplate + void before(Path p) throws IOException { + Files.delete(p); + } + + @AfterTemplate + void after(Path p)throws IOException { + Files.delete(p); + } + } + public static class StringIsEmpty { @BeforeTemplate boolean before(String s) { diff --git a/src/test/resources/recipes/MultipleDereferencesRecipes.java b/src/test/resources/recipes/MultipleDereferencesRecipes.java index e9f1dd57..35e59fbe 100644 --- a/src/test/resources/recipes/MultipleDereferencesRecipes.java +++ b/src/test/resources/recipes/MultipleDereferencesRecipes.java @@ -16,6 +16,9 @@ import java.util.Arrays; import java.util.List; +import java.nio.file.Files; +import java.io.IOException; +import java.nio.file.Path; public final class MultipleDereferencesRecipes extends Recipe { @@ -33,10 +36,57 @@ public String getDescription() { @Override public List getRecipeList() { return Arrays.asList( + new VoidTypeRecipe(), new StringIsEmptyRecipe(), new EqualsItselfRecipe() ); } + public static class VoidTypeRecipe extends Recipe { + + @Override + public String getDisplayName() { + return "Refaster template `MultipleDereferences.VoidType`"; + } + + @Override + public String getDescription() { + return "Recipe created for the following Refaster template:\n```java\npublic static class VoidType {\n \n @BeforeTemplate()\n void before(Path p) throws IOException {\n Files.delete(p);\n }\n \n @AfterTemplate()\n void after(Path p) throws IOException {\n Files.delete(p);\n }\n}\n```\n."; + } + + @Override + public TreeVisitor getVisitor() { + JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { + + Supplier before = memoize(() -> JavaTemplate.compile(this, "before", (JavaTemplate.P1) (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build()); + + Supplier after = memoize(() -> JavaTemplate.compile(this, "after", (JavaTemplate.P1) (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build()); + + @Override + public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { + JavaTemplate.Matcher matcher; + if ((matcher = matcher(before, getCursor())).find()) { + return embed( + apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + getCursor(), + ctx + ); + } + return super.visitMethodInvocation(elem, ctx); + } + + }; + return Preconditions.check( + Preconditions.and( + new UsesType<>("java.io.IOException", true), + new UsesType<>("java.nio.file.Files", true), + new UsesType<>("java.nio.file.Path", true), + new UsesMethod<>("java.nio.file.Files delete(..)") + ), + javaVisitor + ); + } + } + public static class StringIsEmptyRecipe extends Recipe { @Override @@ -73,7 +123,8 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { }; return Preconditions.check( new UsesMethod<>("java.lang.String isEmpty(..)"), - javaVisitor); + javaVisitor + ); } }