diff --git a/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java b/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java index f0a84668..d2d6cd62 100644 --- a/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java +++ b/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java @@ -31,11 +31,15 @@ public class TemplateCode { - public static String process(T tree, List parameters, boolean fullyQualified) { + public static String process(T tree, List parameters, boolean asStatement, boolean fullyQualified) { StringWriter writer = new StringWriter(); TemplateCodePrinter printer = new TemplateCodePrinter(writer, parameters, fullyQualified); try { - printer.printExpr(tree); + if (asStatement) { + printer.printStat(tree); + } else { + printer.printExpr(tree); + } StringBuilder builder = new StringBuilder("JavaTemplate\n"); builder .append(" .builder(\"") diff --git a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java index eea35827..67bdd82a 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -768,7 +768,7 @@ private String toJavaTemplateBuilder() { tree = ((JCTree.JCReturn) tree).getExpression(); } - String javaTemplateBuilder = TemplateCode.process(tree, method.getParameters(), true); + String javaTemplateBuilder = TemplateCode.process(tree, method.getParameters(), method.restype.type instanceof Type.JCVoidType, true); return TemplateCode.indent(javaTemplateBuilder, 16); } @@ -808,7 +808,7 @@ public void visitApply(JCTree.JCMethodInvocation jcMethodInvocation) { } }.translate(copied); - String javaTemplateBuilder = TemplateCode.process(translated, method.getParameters(), true); + String javaTemplateBuilder = TemplateCode.process(translated, method.getParameters(), method.restype.type instanceof Type.JCVoidType, true); return TemplateCode.indent(javaTemplateBuilder, 16); } diff --git a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java index aadfab53..62555bfb 100644 --- a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java @@ -138,7 +138,7 @@ public void visitApply(JCTree.JCMethodInvocation tree) { } } - String templateCode = TemplateCode.process(resolved.get(template.getBody()), parameters, false); + String templateCode = TemplateCode.process(resolved.get(template.getBody()), parameters, "statement".equals(name), false); Symbol.PackageSymbol pkg = classDecl.sym.packge(); JavaFileObject builderFile = processingEnv.getFiler().createSourceFile(templateFqn); diff --git a/src/test/resources/refaster/MethodThrowsRecipe.java b/src/test/resources/refaster/MethodThrowsRecipe.java index ede66c67..1334ab5a 100644 --- a/src/test/resources/refaster/MethodThrowsRecipe.java +++ b/src/test/resources/refaster/MethodThrowsRecipe.java @@ -59,10 +59,10 @@ public String getDescription() { public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { final JavaTemplate before = JavaTemplate - .builder("java.nio.file.Files.readAllLines(#{path:any(java.nio.file.Path)}, java.nio.charset.StandardCharsets.UTF_8)") + .builder("java.nio.file.Files.readAllLines(#{path:any(java.nio.file.Path)}, java.nio.charset.StandardCharsets.UTF_8);") .build(); final JavaTemplate after = JavaTemplate - .builder("java.nio.file.Files.readAllLines(#{path:any(java.nio.file.Path)})") + .builder("java.nio.file.Files.readAllLines(#{path:any(java.nio.file.Path)});") .build(); @Override diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index 5e71f8d3..47a9a21f 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -88,10 +88,10 @@ public String getDescription() { public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { final JavaTemplate before = JavaTemplate - .builder("java.nio.file.Files.delete(#{p:any(java.nio.file.Path)})") + .builder("java.nio.file.Files.delete(#{p:any(java.nio.file.Path)});") .build(); final JavaTemplate after = JavaTemplate - .builder("java.nio.file.Files.delete(#{p:any(java.nio.file.Path)})") + .builder("java.nio.file.Files.delete(#{p:any(java.nio.file.Path)});") .build(); @Override