diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveMethodCallVisitor.java b/src/main/java/org/openrewrite/staticanalysis/RemoveMethodCallVisitor.java deleted file mode 100644 index da23007b2..000000000 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveMethodCallVisitor.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2022 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.staticanalysis; - -import lombok.AllArgsConstructor; -import org.jspecify.annotations.Nullable; -import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.tree.*; - -import java.util.function.BiPredicate; - -/** - * Removes all {@link MethodCall} matching both the - * {@link RemoveMethodCallVisitor#methodMatcher} - * and the - * {@link RemoveMethodCallVisitor#argumentPredicate} for all arguments. - *
- * Only removes {@link MethodCall} where the call's return value is unused. - */ -@AllArgsConstructor -// TODO Consider moving this to `org.openrewrite.java.RemoveMethodInvocationsVisitor` in rewrite-java -public class RemoveMethodCallVisitor
extends JavaIsoVisitor
{
- /**
- * The {@link MethodCall} to match to be removed.
- */
- private final MethodMatcher methodMatcher;
- /**
- * All arguments must match the predicate for the {@link MethodCall} to be removed.
- */
- private final BiPredicate
- * 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.staticanalysis;
-
-import org.junit.jupiter.api.Test;
-import org.openrewrite.DocumentExample;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import static org.openrewrite.java.Assertions.java;
-import static org.openrewrite.test.RewriteTest.toRecipe;
-
-@SuppressWarnings("FunctionName")
-class RemoveMethodCallVisitorTest implements RewriteTest {
-
- @Override
- public void defaults(RecipeSpec spec) {
- spec.recipe(toRecipe(() -> new RemoveMethodCallVisitor<>(new MethodMatcher("* assertTrue(..)"),
- (arg, expr) -> arg == 0 && J.Literal.isLiteralValue(expr, true))));
- }
-
- @DocumentExample
- @Test
- void assertTrueIsRemoved() {
- rewriteRun(
- //language=java
- java(
- """
- abstract class Test {
- abstract void assertTrue(boolean condition);
-
- void test() {
- System.out.println("Hello");
- assertTrue(true);
- System.out.println("World");
- }
- }
- """, """
- abstract class Test {
- abstract void assertTrue(boolean condition);
-
- void test() {
- System.out.println("Hello");
- System.out.println("World");
- }
- }
- """
- )
- );
- }
-
- @Test
- void assertTrueFalseIsNotRemoved() {
- rewriteRun(
- //language=java
- java(
- """
- abstract class Test {
- abstract void assertTrue(boolean condition);
-
- void test() {
- System.out.println("Hello");
- assertTrue(false);
- System.out.println("World");
- }
- }
- """
- )
- );
- }
-
- @Test
- void assertTrueTwoArgIsRemoved() {
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> new RemoveMethodCallVisitor<>(
- new MethodMatcher("* assertTrue(..)"), (arg, expr) -> arg != 1 || J.Literal.isLiteralValue(expr, true)
- ))),
- //language=java
- java(
- """
- abstract class Test {
- abstract void assertTrue(String message, boolean condition);
-
- void test() {
- System.out.println("Hello");
- assertTrue("message", true);
- System.out.println("World");
- }
- }
- """,
- """
- abstract class Test {
- abstract void assertTrue(String message, boolean condition);
-
- void test() {
- System.out.println("Hello");
- System.out.println("World");
- }
- }
- """
- )
- );
- }
-
- @Test
- void doesNotRemoveAssertTrueIfReturnValueIsUsed() {
- rewriteRun(
- //language=java
- java(
- """
- abstract class Test {
- abstract int assertTrue(boolean condition);
-
- void test() {
- System.out.println("Hello");
- int value = assertTrue(true);
- System.out.println("World");
- }
- }
- """
- )
- );
- }
-
- @Test
- void removeMethodCallFromFluentChain() {
- rewriteRun(
- spec -> spec.recipe(RewriteTest.toRecipe(() -> new RemoveMethodCallVisitor<>(
- new MethodMatcher("java.lang.StringBuilder append(..)"), (i, e) -> true))),
- // language=java
- java(
- """
- class Main {
- void hello() {
- final String s = new StringBuilder("hello")
- .delete(1, 2)
- .append("world")
- .toString();
- }
- }
- """,
- """
- class Main {
- void hello() {
- final String s = new StringBuilder("hello")
- .delete(1, 2)
- .toString();
- }
- }
- """
- )
- );
- }
-}
> isTrue = args -> J.Literal.isLiteralValue(args.get(0), true);
+ Predicate
> isFalse = args -> J.Literal.isLiteralValue(args.get(0), false);
- // Junit 4
- compilationUnit = invokeRemoveMethodCallVisitor.invoke(
- compilationUnit,
- JUNIT_ASSERT_TRUE_MATCHER,
- (arg, expr) -> arg.equals(0) && J.Literal.isLiteralValue(expr, true)
- );
- compilationUnit = invokeRemoveMethodCallVisitor.invoke(
- compilationUnit,
- JUNIT_ASSERT_FALSE_MATCHER,
- (arg, expr) -> arg.equals(0) && J.Literal.isLiteralValue(expr, false)
- );
- compilationUnit = invokeRemoveMethodCallVisitor.invoke(
- compilationUnit,
- JUNIT_ASSERT_MESSAGE_TRUE_MATCHER,
- (arg, expr) -> !arg.equals(1) || J.Literal.isLiteralValue(expr, true)
- );
- compilationUnit = invokeRemoveMethodCallVisitor.invoke(
- compilationUnit,
- JUNIT_ASSERT_MESSAGE_FALSE_MATCHER,
- (arg, expr) -> !arg.equals(1) || J.Literal.isLiteralValue(expr, false)
- );
- return compilationUnit;
- }
+ Map