From 58db7dbdff3da96995fcff7d12a9896a0484b2f5 Mon Sep 17 00:00:00 2001 From: Michael Keppler Date: Sat, 27 Jan 2024 17:37:32 +0100 Subject: [PATCH] Don't skip embedOptions for parameterless recipes Deduplicate the logic for creating the recipe code to avoid an inconcistency in how the embedOptions are used. Fixes #66. --- .../processor/RefasterTemplateProcessor.java | 18 +++++++++--------- .../refaster/MultipleDereferencesRecipes.java | 7 ++++++- .../refaster/UnnamedPackageRecipe.java | 7 ++++++- 3 files changed, 21 insertions(+), 11 deletions(-) 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 84b8dd59..50dcaae8 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -274,16 +274,16 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { embedOptions.add("SIMPLIFY_BOOLEANS"); } - if (parameters.isEmpty()) { - recipe.append(" return embed(").append(after).append(".apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx);\n"); - } else { - recipe.append(" return embed(\n"); - recipe.append(" ").append(after).append(".apply(getCursor(), elem.getCoordinates().replace(), ").append(parameters).append("),\n"); - recipe.append(" getCursor(),\n"); - recipe.append(" ctx,\n"); - recipe.append(" ").append(String.join(", ", embedOptions)).append("\n"); - recipe.append(" );\n"); + recipe.append(" return embed(\n"); + recipe.append(" ").append(after).append(".apply(getCursor(), elem.getCoordinates().replace()"); + if (!parameters.isEmpty()) { + recipe.append(", ").append(parameters); } + recipe.append("),\n"); + recipe.append(" getCursor(),\n"); + recipe.append(" ctx,\n"); + recipe.append(" ").append(String.join(", ", embedOptions)).append("\n"); + recipe.append(" );\n"); recipe.append(" }\n"); } recipe.append(" return super.visit").append(methodSuffix).append("(elem, ctx);\n"); diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index a9a9cbee..205afe10 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -178,7 +178,12 @@ public TreeVisitor getVisitor() { public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; if ((matcher = before.matcher(getCursor())).find()) { - return embed(after.apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx); + return embed( + after.apply(getCursor(), elem.getCoordinates().replace()), + getCursor(), + ctx, + SHORTEN_NAMES, SIMPLIFY_BOOLEANS + ); } return super.visitBinary(elem, ctx); } diff --git a/src/test/resources/refaster/UnnamedPackageRecipe.java b/src/test/resources/refaster/UnnamedPackageRecipe.java index 63ccaf3b..c2d15255 100644 --- a/src/test/resources/refaster/UnnamedPackageRecipe.java +++ b/src/test/resources/refaster/UnnamedPackageRecipe.java @@ -64,7 +64,12 @@ public TreeVisitor getVisitor() { public J visitExpression(Expression elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; if ((matcher = before.matcher(getCursor())).find()) { - return embed(after.apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx); + return embed( + after.apply(getCursor(), elem.getCoordinates().replace()), + getCursor(), + ctx, + SHORTEN_NAMES + ); } return super.visitExpression(elem, ctx); }