diff --git a/src/main/java/org/openrewrite/java/template/internal/JavacResolution.java b/src/main/java/org/openrewrite/java/template/internal/JavacResolution.java index b09451a5..f47e3b49 100644 --- a/src/main/java/org/openrewrite/java/template/internal/JavacResolution.java +++ b/src/main/java/org/openrewrite/java/template/internal/JavacResolution.java @@ -30,6 +30,7 @@ import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.Log; +import org.openrewrite.internal.lang.Nullable; import javax.tools.JavaFileObject; import java.lang.reflect.Field; @@ -50,7 +51,7 @@ public JavacResolution(Context context) { this.log = Log.instance(context); } - public Map resolveAll(Context context, JCCompilationUnit cu, List trees) { + public @Nullable Map resolveAll(Context context, JCCompilationUnit cu, List trees) { AtomicReference> resolved = new AtomicReference<>(); new TreeScanner() { @@ -203,6 +204,7 @@ public void visitMethodDef(JCMethodDecl tree) { copyAt = tree; } + @Override public void visitVarDef(JCVariableDecl tree) { if (copyAt != null) { return; 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 cbb66a12..5a9a46ac 100644 --- a/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java +++ b/src/main/java/org/openrewrite/java/template/internal/TemplateCode.java @@ -64,6 +64,13 @@ public static String process(T tree, List a.attribute.type.tsym.getQualifiedName().toString().equals(PRIMITIVE_ANNOTATION))) { + String type = param.get().sym.type.toString(); + if (param.get().getModifiers().getAnnotations().stream() + .anyMatch(a -> a.attribute.type.tsym.getQualifiedName().toString().equals(PRIMITIVE_ANNOTATION))) { type = getUnboxedPrimitive(type); } print(":any(" + type + ")"); 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 8dd74f8f..29a3c1db 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -29,9 +29,9 @@ import com.sun.tools.javac.tree.TreeScanner; import com.sun.tools.javac.util.Context; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.java.template.internal.FQNPretty; import org.openrewrite.java.template.internal.ImportDetector; import org.openrewrite.java.template.internal.JavacResolution; +import org.openrewrite.java.template.internal.TemplateCode; import org.openrewrite.java.template.internal.UsedMethodDetector; import javax.annotation.processing.RoundEnvironment; @@ -213,22 +213,15 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { for (Map.Entry entry : beforeTemplates.entrySet()) { recipe.append(" final JavaTemplate ") .append(entry.getKey()) - .append(" = Semantics.") - .append(statementType(entry.getValue())) - .append("(this, \"") - .append(entry.getKey()).append("\", ") - .append(toLambda(entry.getValue())) - .append(").build();\n"); + .append(" = ") + .append(toJavaTemplateBuilder(entry.getValue(), descriptor.resolvedParameters)) + .append("\n .build();\n"); } recipe.append(" final JavaTemplate ") .append(after) - .append(" = Semantics.") - .append(statementType(descriptor.afterTemplate)) - .append("(this, \"") - .append(after) - .append("\", ") - .append(toLambda(descriptor.afterTemplate)) - .append(").build();\n"); + .append(" = ") + .append(toJavaTemplateBuilder(descriptor.afterTemplate, descriptor.resolvedParameters)) + .append("\n .build();\n"); recipe.append("\n"); List lstTypes = LST_TYPE_MAP.get(getType(descriptor.beforeTemplates.get(0))); @@ -331,11 +324,11 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { out.write("import org.openrewrite.Recipe;\n"); out.write("import org.openrewrite.TreeVisitor;\n"); out.write("import org.openrewrite.internal.lang.NonNullApi;\n"); + out.write("import org.openrewrite.java.JavaParser;\n"); out.write("import org.openrewrite.java.JavaTemplate;\n"); out.write("import org.openrewrite.java.JavaVisitor;\n"); out.write("import org.openrewrite.java.search.*;\n"); out.write("import org.openrewrite.java.template.Primitive;\n"); - out.write("import org.openrewrite.java.template.Semantics;\n"); out.write("import org.openrewrite.java.template.function.*;\n"); out.write("import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor;\n"); out.write("import org.openrewrite.java.tree.*;\n"); @@ -400,6 +393,22 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { } } + private String toJavaTemplateBuilder(JCTree.JCMethodDecl methodDecl, + Map resolvedParameters) { + JCTree tree = methodDecl.getBody().getStatements().get(0); + if (tree instanceof JCTree.JCReturn) { + tree = ((JCTree.JCReturn) tree).getExpression(); + } + + List mappedParameters = methodDecl.getParameters().stream() + .map(resolvedParameters::get) + .map(JCTree.JCVariableDecl.class::cast) + .collect(Collectors.toList()); + + String javaTemplateBuilder = TemplateCode.process(tree, mappedParameters, true); + return TemplateCode.indent(javaTemplateBuilder, 16); + } + private boolean simplifyBooleans(JCTree.JCMethodDecl template) { if (template.getReturnType().type.getTag() == TypeTag.BOOLEAN) { return true; @@ -655,55 +664,6 @@ private JCTree.JCExpression getReturnExpression(JCTree.JCMethodDecl method) { return null; } - private String statementType(JCTree.JCMethodDecl method) { - // for now excluding assignment expressions and prefix and postfix -- and ++ - Set> expressionStatementTypes = Stream.of( - JCTree.JCMethodInvocation.class, - JCTree.JCNewClass.class).collect(Collectors.toSet()); - - Class type = getType(method); - if (expressionStatementTypes.contains(type)) { - if (type == JCTree.JCMethodInvocation.class - && method.getBody().getStatements().last() instanceof JCTree.JCExpressionStatement - && !(method.getReturnType().type instanceof Type.JCVoidType)) { - return "expression"; - } - if (method.restype.type instanceof Type.JCVoidType || !JCTree.JCExpression.class.isAssignableFrom(type)) { - return "statement"; - } - } - return "expression"; - } - - private String toLambda(JCTree.JCMethodDecl method) { - StringBuilder builder = new StringBuilder(); - - StringJoiner joiner = new StringJoiner(", ", "(", ")"); - for (JCTree.JCVariableDecl parameter : method.getParameters()) { - String paramType = parameter.getType().type.toString(); - if (!getBoxedPrimitive(paramType).equals(paramType)) { - paramType = "@Primitive " + getBoxedPrimitive(paramType); - } else if (paramType.startsWith("java.lang.")) { - paramType = paramType.substring("java.lang.".length()); - } - joiner.add(paramType + " " + parameter.getName()); - } - builder.append(joiner); - builder.append(" -> "); - - JCTree.JCStatement statement = method.getBody().getStatements().get(0); - if (statement instanceof JCTree.JCReturn) { - builder.append(FQNPretty.toString(((JCTree.JCReturn) statement).getExpression())); - } else if (statement instanceof JCTree.JCThrow) { - String string = FQNPretty.toString(statement); - builder.append("{ ").append(string).append(" }"); - } else { - String string = FQNPretty.toString(statement); - builder.append(string); - } - return builder.toString(); - } - @Nullable private TemplateDescriptor getTemplateDescriptor(JCTree.JCClassDecl tree, Context context, JCCompilationUnit cu) { TemplateDescriptor result = new TemplateDescriptor(tree); @@ -727,6 +687,7 @@ class TemplateDescriptor { final JCTree.JCClassDecl classDecl; final List beforeTemplates = new ArrayList<>(); JCTree.JCMethodDecl afterTemplate; + Map resolvedParameters = new IdentityHashMap<>(); public TemplateDescriptor(JCTree.JCClassDecl classDecl) { this.classDecl = classDecl; @@ -829,14 +790,30 @@ public void afterTemplate(JCTree.JCMethodDecl method) { } private boolean resolve(Context context, JCCompilationUnit cu) { + JavacResolution res = new JavacResolution(context); try { - JavacResolution res = new JavacResolution(context); - beforeTemplates.replaceAll(key -> { - Map resolved = res.resolveAll(context, cu, singletonList(key)); - return (JCTree.JCMethodDecl) resolved.get(key); - }); - Map resolved = res.resolveAll(context, cu, singletonList(afterTemplate)); - afterTemplate = (JCTree.JCMethodDecl) resolved.get(afterTemplate); + // Resolve parameters + for (JCTree.JCMethodDecl beforeTemplate : beforeTemplates) { + if (!beforeTemplate.getParameters().isEmpty()) { + for (Map.Entry e : res.resolveAll(context, cu, beforeTemplate.getParameters()).entrySet()) { + if (e.getKey() instanceof JCTree.JCVariableDecl && e.getValue() instanceof JCTree.JCVariableDecl) { + resolvedParameters.put((JCTree.JCVariableDecl) e.getValue(), (JCTree.JCVariableDecl) e.getKey()); + } + } + } + } + if (!afterTemplate.getParameters().isEmpty()) { + for (Map.Entry e : res.resolveAll(context, cu, afterTemplate.getParameters()).entrySet()) { + if (e.getKey() instanceof JCTree.JCVariableDecl && e.getValue() instanceof JCTree.JCVariableDecl) { + resolvedParameters.put((JCTree.JCVariableDecl) e.getValue(), (JCTree.JCVariableDecl) e.getKey()); + } + } + } + + // Resolve templates + Map resolvedBeforeTemplates = res.resolveAll(context, cu, beforeTemplates); + beforeTemplates.replaceAll(key -> (JCTree.JCMethodDecl) resolvedBeforeTemplates.get(key)); + afterTemplate = (JCTree.JCMethodDecl) res.resolveAll(context, cu, singletonList(afterTemplate)).get(afterTemplate); } catch (Throwable t) { processingEnv.getMessager().printMessage(Kind.WARNING, "Had trouble type attributing the template."); return false; 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 10dea5f1..aadfab53 100644 --- a/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/TemplateProcessor.java @@ -163,7 +163,7 @@ public void visitApply(JCTree.JCMethodInvocation tree) { out.write(" * @return the JavaTemplate builder.\n"); out.write(" */\n"); out.write(" public static JavaTemplate.Builder getTemplate() {\n"); - out.write(" return " + indent(templateCode, 12) + ";\n"); + out.write(" return " + TemplateCode.indent(templateCode, 12) + ";\n"); out.write(" }\n"); out.write("}\n"); out.flush(); @@ -176,13 +176,6 @@ public void visitApply(JCTree.JCMethodInvocation tree) { super.visitApply(tree); } - - private String indent(String code, int width) { - char[] indent = new char[width]; - Arrays.fill(indent, ' '); - String replacement = "$1" + new String(indent); - return code.replaceAll("(?m)(\\R)", replacement); - } }.scan(cu); } diff --git a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java index 842e6b75..9cfe0f5d 100644 --- a/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java +++ b/src/test/java/org/openrewrite/java/template/RefasterTemplateProcessorTest.java @@ -110,14 +110,19 @@ private static Compilation compileResource(String resourceName) { static Compilation compileResource(String resourceName, TypeAwareProcessor processor) { // As per https://github.com/google/compile-testing/blob/v0.21.0/src/main/java/com/google/testing/compile/package-info.java#L53-L55 - return compileResource(JavaFileObjects.forResource(resourceName), processor); + return compile(JavaFileObjects.forResource(resourceName), processor); + } + + @SuppressWarnings("unused") // use when text blocks are available + static Compilation compileSource(String fqn, @Language("java") String source) { + return compile(JavaFileObjects.forSourceString(fqn, source), new RefasterTemplateProcessor()); } static Compilation compileSource(String fqn, @Language("java") String source, TypeAwareProcessor processor) { - return compileResource(JavaFileObjects.forSourceString(fqn, source), processor); + return compile(JavaFileObjects.forSourceString(fqn, source), processor); } - static Compilation compileResource(JavaFileObject javaFileObject, TypeAwareProcessor processor) { + static Compilation compile(JavaFileObject javaFileObject, TypeAwareProcessor processor) { return javac() .withProcessors(processor) .withClasspath(Arrays.asList( diff --git a/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java b/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java index 0d1ec116..100e6cb7 100644 --- a/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java +++ b/src/test/java/org/openrewrite/java/template/TemplateProcessorTest.java @@ -23,7 +23,7 @@ import org.openrewrite.java.template.processor.TemplateProcessor; import static com.google.testing.compile.CompilationSubject.assertThat; -import static org.openrewrite.java.template.RefasterTemplateProcessorTest.compileResource; +import static org.openrewrite.java.template.RefasterTemplateProcessorTest.*; class TemplateProcessorTest { @ParameterizedTest diff --git a/src/test/resources/refaster/ArraysRecipe.java b/src/test/resources/refaster/ArraysRecipe.java index 0f690e16..f4283a73 100644 --- a/src/test/resources/refaster/ArraysRecipe.java +++ b/src/test/resources/refaster/ArraysRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -33,12 +34,18 @@ import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*; + +/** + * OpenRewrite recipe created for Refaster template {@code Arrays}. + */ @SuppressWarnings("all") @NonNullApi public class ArraysRecipe extends Recipe { - public ArraysRecipe() { - } + /** + * Instantiates a new instance. + */ + public ArraysRecipe() {} @Override public String getDisplayName() { @@ -53,8 +60,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String[] strings) -> String.join(", ", strings)).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String[] strings) -> String.join(":", strings)).build(); + final JavaTemplate before = JavaTemplate + .builder("String.join(\", \", #{strings:any(java.lang.String[])})") + .build(); + final JavaTemplate after = JavaTemplate + .builder("String.join(\":\", #{strings:any(java.lang.String[])})") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/CharacterEscapeAnnotationRecipe.java b/src/test/resources/refaster/CharacterEscapeAnnotationRecipe.java index fe1dcabc..541aa369 100644 --- a/src/test/resources/refaster/CharacterEscapeAnnotationRecipe.java +++ b/src/test/resources/refaster/CharacterEscapeAnnotationRecipe.java @@ -20,11 +20,11 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -35,7 +35,7 @@ /** - * OpenRewrite recipe created for Refaster template {@code MultilineAnnotation}. + * OpenRewrite recipe created for Refaster template {@code CharacterEscapeAnnotation}. */ @SuppressWarnings("all") @NonNullApi @@ -44,7 +44,7 @@ public class CharacterEscapeAnnotationRecipe extends Recipe { /** * Instantiates a new instance. */ - public MultilineAnnotationRecipe() {} + public CharacterEscapeAnnotationRecipe() {} @Override public String getDisplayName() { @@ -64,8 +64,12 @@ public Set getTags() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", () -> "The answer to life, the universe, and everything").build(); - final JavaTemplate after = Semantics.expression(this, "after", () -> "42").build(); + final JavaTemplate before = JavaTemplate + .builder("\"The answer to life, the universe, and everything\"") + .build(); + final JavaTemplate after = JavaTemplate + .builder("\"42\"") + .build(); @Override public J visitExpression(Expression elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/EscapesRecipes.java b/src/test/resources/refaster/EscapesRecipes.java index 82b89aaa..f66fc224 100644 --- a/src/test/resources/refaster/EscapesRecipes.java +++ b/src/test/resources/refaster/EscapesRecipes.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -36,8 +37,14 @@ import com.sun.tools.javac.util.Convert; import com.sun.tools.javac.util.Constants; +/** + * OpenRewrite recipes created for Refaster template {@code foo.Escapes}. + */ @SuppressWarnings("all") public class EscapesRecipes extends Recipe { + /** + * Instantiates a new instance. + */ public EscapesRecipes() {} @Override @@ -58,9 +65,16 @@ public List getRecipeList() { ); } + /** + * OpenRewrite recipe created for Refaster template {@code Escapes.ConstantsFormat}. + */ @SuppressWarnings("all") @NonNullApi public static class ConstantsFormatRecipe extends Recipe { + + /** + * Instantiates a new instance. + */ public ConstantsFormatRecipe() {} @Override @@ -76,8 +90,14 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String value) -> String.format("\"%s\"", com.sun.tools.javac.util.Convert.quote(value))).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String value) -> com.sun.tools.javac.util.Constants.format(value)).build(); + final JavaTemplate before = JavaTemplate + .builder("String.format(\"\\\"%s\\\"\", com.sun.tools.javac.util.Convert.quote(#{value:any(java.lang.String)}))") + .javaParser(JavaParser.fromJavaVersion().classpath("tools")) + .build(); + final JavaTemplate after = JavaTemplate + .builder("com.sun.tools.javac.util.Constants.format(#{value:any(java.lang.String)})") + .javaParser(JavaParser.fromJavaVersion().classpath("tools")) + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -106,9 +126,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code Escapes.Split}. + */ @SuppressWarnings("all") @NonNullApi public static class SplitRecipe extends Recipe { + + /** + * Instantiates a new instance. + */ public SplitRecipe() {} @Override @@ -124,8 +151,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s.split("[^\\S]+")).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s.split("\\s+")).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.split(\"[^\\\\S]+\")") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)}.split(\"\\\\s+\")") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -149,4 +180,4 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } -} \ No newline at end of file +} diff --git a/src/test/resources/refaster/GenericsRecipes.java b/src/test/resources/refaster/GenericsRecipes.java index cd002e37..bd5153fb 100644 --- a/src/test/resources/refaster/GenericsRecipes.java +++ b/src/test/resources/refaster/GenericsRecipes.java @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -87,8 +88,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (java.util.List l) -> l.iterator().next()).build(); - final JavaTemplate after = Semantics.expression(this, "after", (java.util.List l) -> l.get(0)).build(); + final JavaTemplate before = JavaTemplate + .builder("#{l:any(java.util.List)}.iterator().next()") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{l:any(java.util.List)}.get(0)") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/MatchingRecipes.java b/src/test/resources/refaster/MatchingRecipes.java index 057ace2b..7064ed13 100644 --- a/src/test/resources/refaster/MatchingRecipes.java +++ b/src/test/resources/refaster/MatchingRecipes.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -33,9 +34,15 @@ import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*; + +/** + * OpenRewrite recipes created for Refaster template {@code foo.Matching}. + */ @SuppressWarnings("all") public class MatchingRecipes extends Recipe { - + /** + * Instantiates a new instance. + */ public MatchingRecipes() {} @Override @@ -60,10 +67,16 @@ public List getRecipeList() { ); } + /** + * OpenRewrite recipe created for Refaster template {@code Matching.StringIsEmpty}. + */ @SuppressWarnings("all") @NonNullApi public static class StringIsEmptyRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public StringIsEmptyRecipe() {} @Override @@ -84,9 +97,15 @@ public Set getTags() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build(); - final JavaTemplate before2 = Semantics.expression(this, "before2", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build(); - final JavaTemplate after = Semantics.expression(this, "after", (@Primitive Integer i, String s) -> (s != null && s.length() == 0)).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.substring(#{i:any(int)}).isEmpty()") + .build(); + final JavaTemplate before2 = JavaTemplate + .builder("#{s:any(java.lang.String)}.substring(#{i:any(int)}).isEmpty()") + .build(); + final JavaTemplate after = JavaTemplate + .builder("(#{s:any(java.lang.String)} != null && #{s}.length() == 0)") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/MethodThrowsRecipe.java b/src/test/resources/refaster/MethodThrowsRecipe.java index d13e5279..c9c1171a 100644 --- a/src/test/resources/refaster/MethodThrowsRecipe.java +++ b/src/test/resources/refaster/MethodThrowsRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -37,10 +38,16 @@ import java.nio.file.Path; import java.nio.charset.StandardCharsets; +/** + * OpenRewrite recipe created for Refaster template {@code MethodThrows}. + */ @SuppressWarnings("all") @NonNullApi public class MethodThrowsRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public MethodThrowsRecipe() {} @Override @@ -56,8 +63,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.statement(this, "before", (java.nio.file.Path path) -> java.nio.file.Files.readAllLines(path, java.nio.charset.StandardCharsets.UTF_8)).build(); - final JavaTemplate after = Semantics.statement(this, "after", (java.nio.file.Path path) -> java.nio.file.Files.readAllLines(path)).build(); + final JavaTemplate before = JavaTemplate + .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)})") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index 205afe10..3eb865f1 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -36,9 +37,14 @@ import java.nio.file.Files; import java.nio.file.Path; +/** + * OpenRewrite recipes created for Refaster template {@code foo.MultipleDereferences}. + */ @SuppressWarnings("all") public class MultipleDereferencesRecipes extends Recipe { - + /** + * Instantiates a new instance. + */ public MultipleDereferencesRecipes() {} @Override @@ -60,10 +66,16 @@ public List getRecipeList() { ); } + /** + * OpenRewrite recipe created for Refaster template {@code MultipleDereferences.VoidType}. + */ @SuppressWarnings("all") @NonNullApi public static class VoidTypeRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public VoidTypeRecipe() {} @Override @@ -79,8 +91,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.statement(this, "before", (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build(); - final JavaTemplate after = Semantics.statement(this, "after", (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build(); + final JavaTemplate before = JavaTemplate + .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)})") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -108,10 +124,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code MultipleDereferences.StringIsEmpty}. + */ @SuppressWarnings("all") @NonNullApi public static class StringIsEmptyRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public StringIsEmptyRecipe() {} @Override @@ -127,8 +149,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s.isEmpty()).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s != null && s.length() == 0).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.isEmpty()") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)} != null && #{s}.length() == 0") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -152,10 +178,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code MultipleDereferences.EqualsItself}. + */ @SuppressWarnings("all") @NonNullApi public static class EqualsItselfRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public EqualsItselfRecipe() {} @Override @@ -171,8 +203,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (Object o) -> o == o).build(); - final JavaTemplate after = Semantics.expression(this, "after", (Object o) -> true).build(); + final JavaTemplate before = JavaTemplate + .builder("#{o:any(java.lang.Object)} == #{o}") + .build(); + final JavaTemplate after = JavaTemplate + .builder("true") + .build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/NestedPreconditionsRecipe.java b/src/test/resources/refaster/NestedPreconditionsRecipe.java index 85353143..8f9835f2 100644 --- a/src/test/resources/refaster/NestedPreconditionsRecipe.java +++ b/src/test/resources/refaster/NestedPreconditionsRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -38,10 +39,16 @@ import java.util.HashMap; import java.util.Hashtable; +/** + * OpenRewrite recipe created for Refaster template {@code NestedPreconditions}. + */ @SuppressWarnings("all") @NonNullApi public class NestedPreconditionsRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public NestedPreconditionsRecipe() {} @Override @@ -57,9 +64,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate hashMap = Semantics.expression(this, "hashMap", (@Primitive Integer size) -> new java.util.HashMap(size)).build(); - final JavaTemplate linkedHashMap = Semantics.expression(this, "linkedHashMap", (@Primitive Integer size) -> new java.util.LinkedHashMap(size)).build(); - final JavaTemplate hashtable = Semantics.expression(this, "hashtable", (@Primitive Integer size) -> new java.util.Hashtable(size)).build(); + final JavaTemplate hashMap = JavaTemplate + .builder("new java.util.HashMap(#{size:any(int)})") + .build(); + final JavaTemplate linkedHashMap = JavaTemplate + .builder("new java.util.LinkedHashMap(#{size:any(int)})") + .build(); + final JavaTemplate hashtable = JavaTemplate + .builder("new java.util.Hashtable(#{size:any(int)})") + .build(); @Override public J visitNewClass(J.NewClass elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/ParameterReuseRecipe.java b/src/test/resources/refaster/ParameterReuseRecipe.java index c1c7b888..da56b237 100644 --- a/src/test/resources/refaster/ParameterReuseRecipe.java +++ b/src/test/resources/refaster/ParameterReuseRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -33,10 +34,17 @@ import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*; + +/** + * OpenRewrite recipe created for Refaster template {@code ParameterReuse}. + */ @SuppressWarnings("all") @NonNullApi public class ParameterReuseRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public ParameterReuseRecipe() {} @Override @@ -52,8 +60,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s == s).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s.equals(s)).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)} == #{s}") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)}.equals(#{s})") + .build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/ShouldAddImportsRecipes.java b/src/test/resources/refaster/ShouldAddImportsRecipes.java index 8d573dd4..904843f3 100644 --- a/src/test/resources/refaster/ShouldAddImportsRecipes.java +++ b/src/test/resources/refaster/ShouldAddImportsRecipes.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -39,8 +40,14 @@ import static java.nio.file.Files.exists; import static java.util.Objects.hash; +/** + * OpenRewrite recipes created for Refaster template {@code foo.ShouldAddImports}. + */ @SuppressWarnings("all") public class ShouldAddImportsRecipes extends Recipe { + /** + * Instantiates a new instance. + */ public ShouldAddImportsRecipes() {} @Override @@ -63,10 +70,16 @@ public List getRecipeList() { ); } + /** + * OpenRewrite recipe created for Refaster template {@code ShouldAddImports.StringValueOf}. + */ @SuppressWarnings("all") @NonNullApi public static class StringValueOfRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public StringValueOfRecipe() {} @Override @@ -82,8 +95,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> String.valueOf(s)).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> java.util.Objects.toString(s)).build(); + final JavaTemplate before = JavaTemplate + .builder("String.valueOf(#{s:any(java.lang.String)})") + .build(); + final JavaTemplate after = JavaTemplate + .builder("java.util.Objects.toString(#{s:any(java.lang.String)})") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -107,10 +124,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code ShouldAddImports.ObjectsEquals}. + */ @SuppressWarnings("all") @NonNullApi public static class ObjectsEqualsRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public ObjectsEqualsRecipe() {} @Override @@ -126,9 +149,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate equals = Semantics.expression(this, "equals", (@Primitive Integer a, @Primitive Integer b) -> java.util.Objects.equals(a, b)).build(); - final JavaTemplate compareZero = Semantics.expression(this, "compareZero", (@Primitive Integer a, @Primitive Integer b) -> Integer.compare(a, b) == 0).build(); - final JavaTemplate isis = Semantics.expression(this, "isis", (@Primitive Integer a, @Primitive Integer b) -> a == b).build(); + final JavaTemplate equals = JavaTemplate + .builder("java.util.Objects.equals(#{a:any(int)}, #{b:any(int)})") + .build(); + final JavaTemplate compareZero = JavaTemplate + .builder("Integer.compare(#{a:any(int)}, #{b:any(int)}) == 0") + .build(); + final JavaTemplate isis = JavaTemplate + .builder("#{a:any(int)} == #{b:any(int)}") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -167,10 +196,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code ShouldAddImports.StaticImportObjectsHash}. + */ @SuppressWarnings("all") @NonNullApi public static class StaticImportObjectsHashRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public StaticImportObjectsHashRecipe() {} @Override @@ -186,8 +221,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> hash(s)).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s.hashCode()).build(); + final JavaTemplate before = JavaTemplate + .builder("java.util.Objects.hash(#{s:any(java.lang.String)})") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)}.hashCode()") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { @@ -212,10 +251,16 @@ public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { } } + /** + * OpenRewrite recipe created for Refaster template {@code ShouldAddImports.FileExists}. + */ @SuppressWarnings("all") @NonNullApi public static class FileExistsRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public FileExistsRecipe() {} @Override @@ -231,8 +276,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (java.nio.file.Path path) -> path.toFile().exists()).build(); - final JavaTemplate after = Semantics.expression(this, "after", (java.nio.file.Path path) -> exists(path)).build(); + final JavaTemplate before = JavaTemplate + .builder("#{path:any(java.nio.file.Path)}.toFile().exists()") + .build(); + final JavaTemplate after = JavaTemplate + .builder("java.nio.file.Files.exists(#{path:any(java.nio.file.Path)})") + .build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java index 9a00ec25..bf9a4ab4 100644 --- a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java +++ b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -87,8 +88,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s.length() > 0).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> !s.isEmpty()).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.length() > 0") + .build(); + final JavaTemplate after = JavaTemplate + .builder("!#{s:any(java.lang.String)}.isEmpty()") + .build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { @@ -137,8 +142,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s.length() == 0).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s.isEmpty()).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.length() == 0") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{s:any(java.lang.String)}.isEmpty()") + .build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/SimplifyBooleansRecipe.java b/src/test/resources/refaster/SimplifyBooleansRecipe.java index f72ade77..46227cfb 100644 --- a/src/test/resources/refaster/SimplifyBooleansRecipe.java +++ b/src/test/resources/refaster/SimplifyBooleansRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -33,10 +34,17 @@ import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.*; + +/** + * OpenRewrite recipe created for Refaster template {@code SimplifyBooleans}. + */ @SuppressWarnings("all") @NonNullApi public class SimplifyBooleansRecipe extends Recipe { + /** + * Instantiates a new instance. + */ public SimplifyBooleansRecipe() {} @Override @@ -52,8 +60,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s, String s1, String s2) -> s.replaceAll(s1, s2)).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s, String s1, String s2) -> s != null ? s.replaceAll(s1, s2) : s).build(); + 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) { diff --git a/src/test/resources/refaster/SimplifyTernaryRecipes.java b/src/test/resources/refaster/SimplifyTernaryRecipes.java index ec1bed6e..674aa4f1 100644 --- a/src/test/resources/refaster/SimplifyTernaryRecipes.java +++ b/src/test/resources/refaster/SimplifyTernaryRecipes.java @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -86,8 +87,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (@Primitive Boolean expr) -> expr ? true : false).build(); - final JavaTemplate after = Semantics.expression(this, "after", (@Primitive Boolean expr) -> expr).build(); + final JavaTemplate before = JavaTemplate + .builder("#{expr:any(boolean)} ? true : false") + .build(); + final JavaTemplate after = JavaTemplate + .builder("#{expr:any(boolean)}") + .build(); @Override public J visitTernary(J.Ternary elem, ExecutionContext ctx) { @@ -133,8 +138,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (@Primitive Boolean expr) -> expr ? false : true).build(); - final JavaTemplate after = Semantics.expression(this, "after", (@Primitive Boolean expr) -> !(expr)).build(); + final JavaTemplate before = JavaTemplate + .builder("#{expr:any(boolean)} ? false : true") + .build(); + final JavaTemplate after = JavaTemplate + .builder("!(#{expr:any(boolean)})") + .build(); @Override public J visitTernary(J.Ternary elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/UnnamedPackageRecipe.java b/src/test/resources/refaster/UnnamedPackageRecipe.java index c2d15255..7ba0b392 100644 --- a/src/test/resources/refaster/UnnamedPackageRecipe.java +++ b/src/test/resources/refaster/UnnamedPackageRecipe.java @@ -18,11 +18,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -57,8 +58,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", () -> "This class is located in the default package").build(); - final JavaTemplate after = Semantics.expression(this, "after", () -> "And that doesn\'t cause any problems").build(); + final JavaTemplate before = JavaTemplate + .builder("\"This class is located in the default package\"") + .build(); + final JavaTemplate after = JavaTemplate + .builder("\"And that doesn\\'t cause any problems\"") + .build(); @Override public J visitExpression(Expression elem, ExecutionContext ctx) { diff --git a/src/test/resources/refaster/UseStringIsEmpty.java b/src/test/resources/refaster/UseStringIsEmpty.java index e71be4a0..3b5ceb7a 100644 --- a/src/test/resources/refaster/UseStringIsEmpty.java +++ b/src/test/resources/refaster/UseStringIsEmpty.java @@ -36,6 +36,6 @@ boolean before(String s) { @AfterTemplate boolean after(String s) { - return !s.isEmpty(); + return !(s.isEmpty()); } } diff --git a/src/test/resources/refaster/UseStringIsEmptyRecipe.java b/src/test/resources/refaster/UseStringIsEmptyRecipe.java index 66ebdf75..64ebc830 100644 --- a/src/test/resources/refaster/UseStringIsEmptyRecipe.java +++ b/src/test/resources/refaster/UseStringIsEmptyRecipe.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright 2024 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +20,12 @@ import org.openrewrite.Recipe; import org.openrewrite.TreeVisitor; import org.openrewrite.internal.lang.NonNullApi; +import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.search.*; import org.openrewrite.java.template.Primitive; -import org.openrewrite.java.template.Semantics; + import org.openrewrite.java.template.function.*; import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; @@ -59,8 +60,12 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final JavaTemplate before = Semantics.expression(this, "before", (String s) -> s.length() > 0).build(); - final JavaTemplate after = Semantics.expression(this, "after", (String s) -> !s.isEmpty()).build(); + final JavaTemplate before = JavaTemplate + .builder("#{s:any(java.lang.String)}.length() > 0") + .build(); + final JavaTemplate after = JavaTemplate + .builder("!(#{s:any(java.lang.String)}.isEmpty())") + .build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { @@ -70,7 +75,7 @@ public J visitBinary(J.Binary elem, ExecutionContext ctx) { after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx, - SHORTEN_NAMES, SIMPLIFY_BOOLEANS + REMOVE_PARENS, SHORTEN_NAMES, SIMPLIFY_BOOLEANS ); } return super.visitBinary(elem, ctx);