From 06e561d4c7de57fdb05782c6db4d1a456ff90850 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 4 Feb 2024 14:58:28 +0100 Subject: [PATCH] Drop UseJavaParserBuilderInJavaTemplate following #3285 --- ...seJavaParserBuilderInJavaTemplateTest.java | 77 ------------------- .../UseJavaParserBuilderInJavaTemplate.java | 75 ------------------ 2 files changed, 152 deletions(-) delete mode 100644 rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplateTest.java delete mode 100644 rewrite-java/src/main/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplate.java diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplateTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplateTest.java deleted file mode 100644 index de212874acf..00000000000 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplateTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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 org.openrewrite.java.recipes; - -import org.junit.jupiter.api.Test; -import org.openrewrite.DocumentExample; -import org.openrewrite.java.JavaParser; -import org.openrewrite.test.RecipeSpec; -import org.openrewrite.test.RewriteTest; - -import static org.openrewrite.java.Assertions.java; - -public class UseJavaParserBuilderInJavaTemplateTest implements RewriteTest { - - @Override - public void defaults(RecipeSpec spec) { - spec.recipe(new UseJavaParserBuilderInJavaTemplate()) - .parser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()) - .dependsOn( - """ - package org.openrewrite.java; - - import org.openrewrite.Cursor; - import java.util.function.Supplier; - - public class JavaTemplate { - public static Builder builder(Supplier parentScope, String code) { - return new Builder(); - } - public static class Builder { - public Builder javaParser(Supplier javaParser) { - return this; - } - public Builder javaParser(JavaParser.Builder javaParser) { - return this; - } - } - } - """)); - } - - @DocumentExample - @Test - void useBuilder() { - rewriteRun( - java( - """ - import org.openrewrite.java.*; - class MyTest { - Object o = JavaTemplate.builder(() -> null, "") - .javaParser(() -> JavaParser.fromJavaVersion().build()); - } - """, - """ - import org.openrewrite.java.*; - class MyTest { - Object o = JavaTemplate.builder(() -> null, "") - .javaParser(JavaParser.fromJavaVersion()); - } - """ - ) - ); - } -} diff --git a/rewrite-java/src/main/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplate.java b/rewrite-java/src/main/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplate.java deleted file mode 100644 index 158b830261e..00000000000 --- a/rewrite-java/src/main/java/org/openrewrite/java/recipes/UseJavaParserBuilderInJavaTemplate.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 org.openrewrite.java.recipes; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.search.UsesMethod; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.Space; - -import java.util.Collections; -import java.util.concurrent.atomic.AtomicReference; - -import static java.util.Objects.requireNonNull; - -public class UseJavaParserBuilderInJavaTemplate extends Recipe { - - @Override - public String getDisplayName() { - return "Use `JavaParser.Builder` when constructing `JavaTemplate`"; - } - - @Override - public String getDescription() { - return "Because we can now clone `JavaParser.Builder`, there is no need to fully build the parser inside a `Supplier`. " + - "This also makes room for `JavaTemplate` to add shared `JavaTypeCache` implementations to parsers used to compile templates."; - } - - @Override - public TreeVisitor getVisitor() { - MethodMatcher SUPPLIER_BASED_JAVA_PARSER = new MethodMatcher( - "org.openrewrite.java.JavaTemplate$Builder javaParser(java.util.function.Supplier)"); - MethodMatcher JAVA_PARSER_BUILD = new MethodMatcher( - "org.openrewrite.java.JavaParser$Builder build()"); - return Preconditions.check(new UsesMethod<>(SUPPLIER_BASED_JAVA_PARSER), new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { - J.MethodInvocation m = super.visitMethodInvocation(method, ctx); - if (SUPPLIER_BASED_JAVA_PARSER.matches(m)) { - AtomicReference builder = new AtomicReference<>(); - new JavaIsoVisitor() { - @Override - public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Integer p) { - if (JAVA_PARSER_BUILD.matches(method)) { - builder.set(requireNonNull(method.getSelect()).withPrefix(Space.EMPTY)); - } - return super.visitMethodInvocation(method, p); - } - }.visit(m.getArguments().get(0), 0); - if (builder.get() != null) { - m = m.withArguments(Collections.singletonList(builder.get())); - } - } - return m; - } - }); - } -}