Skip to content

Commit

Permalink
Generate statement templates as appropriate (#80)
Browse files Browse the repository at this point in the history
When a Refaster template method has a `void` return type the body should be matched against a statement, rather than an expression.
  • Loading branch information
knutwannheden authored Mar 13, 2024
1 parent c905275 commit 81dc64e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@

public class TemplateCode {

public static <T extends JCTree> String process(T tree, List<JCTree.JCVariableDecl> parameters, boolean fullyQualified) {
public static <T extends JCTree> String process(T tree, List<JCTree.JCVariableDecl> 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(\"")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/MethodThrowsRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> 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
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/refaster/MultipleDereferencesRecipes.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
JavaVisitor<ExecutionContext> 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
Expand Down

0 comments on commit 81dc64e

Please sign in to comment.