diff --git a/src/main/java/org/openrewrite/java/template/internal/AbstractRefasterJavaVisitor.java b/src/main/java/org/openrewrite/java/template/internal/AbstractRefasterJavaVisitor.java index 39f51618..1e4ae0c6 100644 --- a/src/main/java/org/openrewrite/java/template/internal/AbstractRefasterJavaVisitor.java +++ b/src/main/java/org/openrewrite/java/template/internal/AbstractRefasterJavaVisitor.java @@ -18,42 +18,18 @@ import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.ShortenFullyQualifiedTypeReferences; import org.openrewrite.java.cleanup.SimplifyBooleanExpressionVisitor; import org.openrewrite.java.cleanup.UnnecessaryParenthesesVisitor; import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaCoordinates; import java.util.Arrays; import java.util.EnumSet; -import java.util.Objects; -import java.util.concurrent.atomic.AtomicReference; -import java.util.function.Supplier; @SuppressWarnings("unused") public abstract class AbstractRefasterJavaVisitor extends JavaVisitor { - protected final Supplier memoize(Supplier delegate) { - AtomicReference value = new AtomicReference<>(); - return () -> { - T val = value.get(); - if (val == null) { - val = value.updateAndGet(cur -> cur == null ? Objects.requireNonNull(delegate.get()) : cur); - } - return val; - }; - } - - protected final JavaTemplate.Matcher matcher(Supplier template, Cursor cursor) { - return template.get().matcher(cursor); - } - - protected final J apply(Supplier template, Cursor cursor, JavaCoordinates coordinates, Object... parameters) { - return template.get().apply(cursor, coordinates, parameters); - } - @Deprecated // to be removed as soon as annotation processor generates required options protected J embed(J j, Cursor cursor, ExecutionContext ctx) { 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 b8abf4b6..e96ba685 100644 --- a/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java +++ b/src/main/java/org/openrewrite/java/template/processor/RefasterTemplateProcessor.java @@ -194,24 +194,24 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { recipe.append(" public TreeVisitor getVisitor() {\n"); recipe.append(" JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() {\n"); for (Map.Entry entry : beforeTemplates.entrySet()) { - recipe.append(" final Supplier ") + recipe.append(" final JavaTemplate ") .append(entry.getKey()) - .append(" = memoize(() -> Semantics.") + .append(" = Semantics.") .append(statementType(entry.getValue())) .append("(this, \"") .append(entry.getKey()).append("\", ") .append(toLambda(entry.getValue())) - .append(").build());\n"); + .append(").build();\n"); } - recipe.append(" final Supplier ") + recipe.append(" final JavaTemplate ") .append(after) - .append(" = memoize(() -> Semantics.") + .append(" = Semantics.") .append(statementType(descriptor.afterTemplate)) .append("(this, \"") .append(after) .append("\", ") .append(toLambda(descriptor.afterTemplate)) - .append(").build());\n"); + .append(").build();\n"); recipe.append("\n"); List lstTypes = LST_TYPE_MAP.get(getType(descriptor.beforeTemplates.get(0))); @@ -229,7 +229,7 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { recipe.append(" JavaTemplate.Matcher matcher;\n"); for (Map.Entry entry : beforeTemplates.entrySet()) { - recipe.append(" if (" + "(matcher = matcher(").append(entry.getKey()).append(", getCursor())).find()").append(") {\n"); + recipe.append(" if (" + "(matcher = ").append(entry.getKey()).append(".matcher(getCursor())).find()").append(") {\n"); com.sun.tools.javac.util.List jcVariableDecls = entry.getValue().getParameters(); for (int i = 0; i < jcVariableDecls.size(); i++) { JCTree.JCVariableDecl param = jcVariableDecls.get(i); @@ -254,10 +254,10 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { maybeRemoveImports(staticImports, entry, descriptor, recipe); if (parameters.isEmpty()) { - recipe.append(" return embed(apply(").append(after).append(", getCursor(), elem.getCoordinates().replace()), getCursor(), ctx);\n"); + recipe.append(" return embed(").append(after).append(".apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx);\n"); } else { recipe.append(" return embed(\n"); - recipe.append(" apply(").append(after).append(", getCursor(), elem.getCoordinates().replace(), ").append(parameters).append("),\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(" );\n"); @@ -307,7 +307,6 @@ public void visitClassDef(JCTree.JCClassDecl classDecl) { out.write("import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor;\n"); out.write("import org.openrewrite.java.tree.*;\n"); out.write("\n"); - out.write("import java.util.function.Supplier;\n"); out.write("import java.util.*;\n"); out.write("\n"); diff --git a/src/test/resources/refaster/MatchingRecipes.java b/src/test/resources/refaster/MatchingRecipes.java index 429a07b1..56399b10 100644 --- a/src/test/resources/refaster/MatchingRecipes.java +++ b/src/test/resources/refaster/MatchingRecipes.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,12 +14,10 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; public final class MatchingRecipes extends Recipe { - @Override public String getDisplayName() { return "Static analysis"; @@ -60,7 +43,6 @@ public List getRecipeList() { @NonNullApi public static class StringIsEmptyRecipe extends Recipe { - @Override public String getDisplayName() { return "Use String length comparison"; @@ -79,29 +61,29 @@ public Set getTags() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build()); - final Supplier before2 = memoize(() -> Semantics.expression(this, "before2", (@Primitive Integer i, String s) -> s.substring(i).isEmpty()).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> s != null && s.length() == 0).build()); + 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", (String s) -> s != null && s.length() == 0).build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { if (new org.openrewrite.java.template.MethodInvocationMatcher().matches((Expression) matcher.parameter(1))) { return super.visitMethodInvocation(elem, ctx); } return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); } - if ((matcher = matcher(before2, getCursor())).find()) { + if ((matcher = before2.matcher(getCursor())).find()) { if (!new org.openrewrite.java.template.MethodInvocationMatcher().matches((Expression) matcher.parameter(1))) { return super.visitMethodInvocation(elem, ctx); } return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); diff --git a/src/test/resources/refaster/MultipleDereferencesRecipes.java b/src/test/resources/refaster/MultipleDereferencesRecipes.java index 6e94390f..d0acc835 100644 --- a/src/test/resources/refaster/MultipleDereferencesRecipes.java +++ b/src/test/resources/refaster/MultipleDereferencesRecipes.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,7 +14,6 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; import java.nio.file.Files; @@ -37,7 +21,6 @@ import java.nio.file.Path; public final class MultipleDereferencesRecipes extends Recipe { - @Override public String getDisplayName() { return "`MultipleDereferences` Refaster recipes"; @@ -73,15 +56,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.statement(this, "before", (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build()); - final Supplier after = memoize(() -> Semantics.statement(this, "after", (java.nio.file.Path p) -> java.nio.file.Files.delete(p)).build()); + 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(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); @@ -118,15 +101,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> s.isEmpty()).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> s != null && s.length() == 0).build()); + 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(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); @@ -158,14 +141,14 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (Object o) -> o == o).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (Object o) -> true).build()); + final JavaTemplate before = Semantics.expression(this, "before", (Object o) -> o == o).build(); + final JavaTemplate after = Semantics.expression(this, "after", (Object o) -> true).build(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { - return embed(apply(after, getCursor(), elem.getCoordinates().replace()), getCursor(), ctx); + if ((matcher = before.matcher(getCursor())).find()) { + return embed(after.apply(getCursor(), elem.getCoordinates().replace()), getCursor(), ctx); } return super.visitBinary(elem, ctx); } diff --git a/src/test/resources/refaster/NestedPreconditionsRecipe.java b/src/test/resources/refaster/NestedPreconditionsRecipe.java index ec96ca88..0d51f2e9 100644 --- a/src/test/resources/refaster/NestedPreconditionsRecipe.java +++ b/src/test/resources/refaster/NestedPreconditionsRecipe.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,7 +14,6 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; import java.util.LinkedHashMap; @@ -53,25 +37,25 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier hashMap = memoize(() -> Semantics.expression(this, "hashMap", (@Primitive Integer size) -> new java.util.HashMap(size)).build()); - final Supplier linkedHashMap = memoize(() -> Semantics.expression(this, "linkedHashMap", (@Primitive Integer size) -> new java.util.LinkedHashMap(size)).build()); - final Supplier hashtable = memoize(() -> Semantics.expression(this, "hashtable", (@Primitive Integer size) -> new java.util.Hashtable(size)).build()); + 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(); @Override public J visitExpression(Expression elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(hashMap, getCursor())).find()) { + if ((matcher = hashMap.matcher(getCursor())).find()) { maybeRemoveImport("java.util.HashMap"); return embed( - apply(hashtable, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + hashtable.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); } - if ((matcher = matcher(linkedHashMap, getCursor())).find()) { + if ((matcher = linkedHashMap.matcher(getCursor())).find()) { maybeRemoveImport("java.util.LinkedHashMap"); return embed( - apply(hashtable, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + hashtable.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); diff --git a/src/test/resources/refaster/ParameterReuseRecipe.java b/src/test/resources/refaster/ParameterReuseRecipe.java index d77855ce..589a88a9 100644 --- a/src/test/resources/refaster/ParameterReuseRecipe.java +++ b/src/test/resources/refaster/ParameterReuseRecipe.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,7 +14,6 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; @@ -49,15 +33,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> s == s).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> s.equals(s)).build()); + 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(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); diff --git a/src/test/resources/refaster/ShouldAddImportsRecipes.java b/src/test/resources/refaster/ShouldAddImportsRecipes.java index b1973d96..81993d91 100644 --- a/src/test/resources/refaster/ShouldAddImportsRecipes.java +++ b/src/test/resources/refaster/ShouldAddImportsRecipes.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,7 +14,6 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; import java.util.Objects; @@ -37,7 +21,6 @@ import static java.util.Objects.hash; public final class ShouldAddImportsRecipes extends Recipe { - @Override public String getDisplayName() { return "`ShouldAddImports` Refaster recipes"; @@ -73,15 +56,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> String.valueOf(s)).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> java.util.Objects.toString(s)).build()); + 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(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); @@ -113,24 +96,24 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier equals = memoize(() -> Semantics.expression(this, "equals", (@Primitive Integer a, @Primitive Integer b) -> java.util.Objects.equals(a, b)).build()); - final Supplier compareZero = memoize(() -> Semantics.expression(this, "compareZero", (@Primitive Integer a, @Primitive Integer b) -> Integer.compare(a, b) == 0).build()); - final Supplier isis = memoize(() -> Semantics.expression(this, "isis", (@Primitive Integer a, @Primitive Integer b) -> a == b).build()); + 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(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(equals, getCursor())).find()) { + if ((matcher = equals.matcher(getCursor())).find()) { maybeRemoveImport("java.util.Objects"); return embed( - apply(isis, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)), + isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)), getCursor(), ctx ); } - if ((matcher = matcher(compareZero, getCursor())).find()) { + if ((matcher = compareZero.matcher(getCursor())).find()) { return embed( - apply(isis, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)), + isis.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0), matcher.parameter(1)), getCursor(), ctx ); @@ -168,16 +151,16 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> hash(s)).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> s.hashCode()).build()); + final JavaTemplate before = Semantics.expression(this, "before", (String s) -> hash(s)).build(); + final JavaTemplate after = Semantics.expression(this, "after", (String s) -> s.hashCode()).build(); @Override public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { maybeRemoveImport("java.util.Objects.hash"); return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); diff --git a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java index fb08a033..dbcb4ec4 100644 --- a/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java +++ b/src/test/resources/refaster/ShouldSupportNestedClassesRecipes.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,12 +14,10 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; public final class ShouldSupportNestedClassesRecipes extends Recipe { - @Override public String getDisplayName() { return "`ShouldSupportNestedClasses` Refaster recipes"; @@ -69,15 +52,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> s.length() > 0).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> !s.isEmpty()).build()); + 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(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); @@ -109,15 +92,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> s.length() == 0).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> s.isEmpty()).build()); + 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(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx ); diff --git a/src/test/resources/refaster/UseStringIsEmptyRecipe.java b/src/test/resources/refaster/UseStringIsEmptyRecipe.java index e97ffe14..78f72ddc 100644 --- a/src/test/resources/refaster/UseStringIsEmptyRecipe.java +++ b/src/test/resources/refaster/UseStringIsEmptyRecipe.java @@ -1,18 +1,3 @@ -/* - * Copyright 2023 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. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package foo; import org.openrewrite.ExecutionContext; @@ -29,7 +14,6 @@ import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; import org.openrewrite.java.tree.*; -import java.util.function.Supplier; import java.util.*; @@ -49,15 +33,15 @@ public String getDescription() { @Override public TreeVisitor getVisitor() { JavaVisitor javaVisitor = new AbstractRefasterJavaVisitor() { - final Supplier before = memoize(() -> Semantics.expression(this, "before", (String s) -> s.length() > 0).build()); - final Supplier after = memoize(() -> Semantics.expression(this, "after", (String s) -> !s.isEmpty()).build()); + 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(); @Override public J visitBinary(J.Binary elem, ExecutionContext ctx) { JavaTemplate.Matcher matcher; - if ((matcher = matcher(before, getCursor())).find()) { + if ((matcher = before.matcher(getCursor())).find()) { return embed( - apply(after, getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), + after.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0)), getCursor(), ctx );