From 3aae7a2cc06722559d8fc9600bbcce7b7ab9af50 Mon Sep 17 00:00:00 2001 From: dralagen Date: Fri, 6 Dec 2024 15:52:32 +0100 Subject: [PATCH 01/12] Add test on RemoveMethodInvocationsVisitor to remove static method --- .../RemoveMethodInvocationsVisitorTest.java | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index c94f455eb16..07d3c75d1b9 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -15,13 +15,14 @@ */ package org.openrewrite.java; +import java.util.List; + import org.junit.jupiter.api.Test; import org.junitpioneer.jupiter.ExpectedToFail; import org.openrewrite.DocumentExample; import org.openrewrite.Recipe; import org.openrewrite.test.RewriteTest; -import java.util.List; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.test.RewriteTest.toRecipe; @@ -492,4 +493,54 @@ public void method() { ) ); } + + @Test + void removeStaticMethodFromImport() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import static java.util.Collections.emptyList; + + class Test { + void method() { + List emptyList = emptyList(); + } + } + """, + """ + class Test { + void method() { + } + } + """ + ) + ); + } + + @Test + void removeStaticMethod() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import java.util.Collections; + + class Test { + void method() { + List emptyList = Collections.emptyList(); + } + } + """, + """ + class Test { + void method() { + } + } + """ + ) + ); + } } From 0258fe479c31092f236ef05f0d312eba4754794c Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 09:40:56 +0100 Subject: [PATCH 02/12] First implementation --- .../java/RemoveMethodInvocationsVisitor.java | 20 ++++++++++++------- .../RemoveMethodInvocationsVisitorTest.java | 12 +++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java index 7cf86789aeb..7dc2ae5a9ab 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java @@ -69,26 +69,32 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) return j; } - private @Nullable J removeMethods(Expression expression, int depth, boolean isLambdaBody, Stack selectAfter) { - if (!(expression instanceof J.MethodInvocation)) { + private @Nullable J removeMethods(@Nullable Expression expression, int depth, boolean isLambdaBody, Stack selectAfter) { + if (!(expression instanceof J.MethodInvocation) || ((J.MethodInvocation) expression).getMethodType() == null) { return expression; } - boolean isStatement = isStatement(); J.MethodInvocation m = (J.MethodInvocation) expression; + boolean isStatic = m.getMethodType().hasFlags(Flag.Static); - if (m.getMethodType() == null || m.getSelect() == null) { + if (m.getSelect() == null && !isStatic) { return expression; } + boolean isStatement = isStatement(); + if (matchers.entrySet().stream().anyMatch(entry -> matches(m, entry.getKey(), entry.getValue()))) { - boolean hasSameReturnType = TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType()); + boolean hasSameReturnType = m.getSelect() != null && TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType()); boolean removable = (isStatement && depth == 0) || hasSameReturnType; if (!removable) { return expression; } - if (m.getSelect() instanceof J.Identifier || m.getSelect() instanceof J.NewClass) { + maybeRemoveImport(m.getMethodType().getDeclaringType()); + + if (isStatic) { + return null; + } else if (m.getSelect() instanceof J.Identifier || m.getSelect() instanceof J.NewClass) { boolean keepSelect = depth != 0; if (keepSelect) { selectAfter.add(getSelectAfter(m)); @@ -241,7 +247,7 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) { @Value @With - static class ToBeRemoved implements Marker { + private static class ToBeRemoved implements Marker { UUID id; static J2 withMarker(J2 j) { return j.withMarkers(j.getMarkers().addIfAbsent(new ToBeRemoved(randomId()))); diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index 07d3c75d1b9..ebf14be8f21 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -497,15 +497,15 @@ public void method() { @Test void removeStaticMethodFromImport() { rewriteRun( - spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + spec -> spec.recipe(createRemoveMethodsRecipe("org.junit.jupiter.api.Assertions assertTrue(..)")), // language=java java( """ - import static java.util.Collections.emptyList; + import static org.junit.jupiter.api.Assertions.assertTrue; class Test { void method() { - List emptyList = emptyList(); + assertTrue(true); } } """, @@ -522,15 +522,15 @@ void method() { @Test void removeStaticMethod() { rewriteRun( - spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + spec -> spec.recipe(createRemoveMethodsRecipe("org.junit.jupiter.api.Assertions assertTrue(..)")), // language=java java( """ - import java.util.Collections; + import org.junit.jupiter.api.Assertions; class Test { void method() { - List emptyList = Collections.emptyList(); + Assertions.assertTrue(true); } } """, From 576388f23c0fc82215732b57d9c563bb5d216caa Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 09:48:55 +0100 Subject: [PATCH 03/12] Improved implementation --- .../java/RemoveMethodInvocationsVisitor.java | 6 +-- .../RemoveMethodInvocationsVisitorTest.java | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java index 7dc2ae5a9ab..c0a601ad7e2 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java @@ -81,11 +81,11 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) return expression; } - boolean isStatement = isStatement(); + boolean isStatementOrStatic = isStatement() || isStatic; if (matchers.entrySet().stream().anyMatch(entry -> matches(m, entry.getKey(), entry.getValue()))) { boolean hasSameReturnType = m.getSelect() != null && TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType()); - boolean removable = (isStatement && depth == 0) || hasSameReturnType; + boolean removable = (isStatementOrStatic && depth == 0) || hasSameReturnType; if (!removable) { return expression; } @@ -100,7 +100,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) selectAfter.add(getSelectAfter(m)); return m.getSelect(); } else { - if (isStatement) { + if (isStatementOrStatic) { return null; } else if (isLambdaBody) { return ToBeRemoved.withMarker(J.Block.createEmptyBlock()); diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index ebf14be8f21..98bf200c612 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -519,6 +519,32 @@ void method() { ); } + @Test + void removeStaticMethodFromImportWithAssignment() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import static java.util.Collections.emptyList; + + class Test { + void method() { + List emptyList = emptyList(); + } + } + """, + """ + class Test { + void method() { + List emptyList; + } + } + """ + ) + ); + } + @Test void removeStaticMethod() { rewriteRun( @@ -543,4 +569,30 @@ void method() { ) ); } + + @Test + void removeStaticMethodWithAssignment() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import java.util.Collections; + + class Test { + void method() { + List emptyList = Collections.emptyList(); + } + } + """, + """ + class Test { + void method() { + List emptyList; + } + } + """ + ) + ); + } } From d31a941a46b3bb559fa821b74114fbe230a0bfa7 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 10:34:39 +0100 Subject: [PATCH 04/12] Improved tests --- .../openrewrite/java/RemoveMethodInvocationsVisitorTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index 98bf200c612..b942905e482 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -531,6 +531,7 @@ void removeStaticMethodFromImportWithAssignment() { class Test { void method() { List emptyList = emptyList(); + emptyList.isEmpty(); } } """, @@ -538,6 +539,7 @@ void method() { class Test { void method() { List emptyList; + emptyList.isEmpty(); } } """ @@ -582,6 +584,7 @@ void removeStaticMethodWithAssignment() { class Test { void method() { List emptyList = Collections.emptyList(); + emptyList.isEmpty(); } } """, @@ -589,6 +592,7 @@ void method() { class Test { void method() { List emptyList; + emptyList.isEmpty(); } } """ From bcc04d4591e568359cc65cc89d1b3dbf5876d181 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 10:35:08 +0100 Subject: [PATCH 05/12] Improved tests --- .../java/RemoveMethodInvocationsVisitorTest.java | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index b942905e482..220c04760a7 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -534,14 +534,6 @@ void method() { emptyList.isEmpty(); } } - """, - """ - class Test { - void method() { - List emptyList; - emptyList.isEmpty(); - } - } """ ) ); @@ -587,14 +579,6 @@ void method() { emptyList.isEmpty(); } } - """, - """ - class Test { - void method() { - List emptyList; - emptyList.isEmpty(); - } - } """ ) ); From a9d25d26fda6b97a4adb3e41200d1b2edada21ef Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 10:39:00 +0100 Subject: [PATCH 06/12] Improved tests --- .../java/RemoveMethodInvocationsVisitorTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index 220c04760a7..fad3a446f62 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -526,6 +526,8 @@ void removeStaticMethodFromImportWithAssignment() { // language=java java( """ + import java.util.List; + import static java.util.Collections.emptyList; class Test { @@ -571,12 +573,13 @@ void removeStaticMethodWithAssignment() { // language=java java( """ + import java.util.List; import java.util.Collections; class Test { void method() { - List emptyList = Collections.emptyList(); - emptyList.isEmpty(); + List l = Collections.emptyList(); + l.size(); } } """ From e3dbe39fd99bcbb18607b49ccc1c8ce8e8927ac2 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 10:40:31 +0100 Subject: [PATCH 07/12] Improved tests [skip ci] --- .../openrewrite/java/RemoveMethodInvocationsVisitorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index fad3a446f62..11f05d14e09 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -578,8 +578,8 @@ void removeStaticMethodWithAssignment() { class Test { void method() { - List l = Collections.emptyList(); - l.size(); + List emptyList = Collections.emptyList(); + emptyList.size(); } } """ From ad7f23069efbc148da200a0e91057e06642653aa Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 11:55:19 +0100 Subject: [PATCH 08/12] Complete it --- .../java/RemoveMethodInvocationsVisitor.java | 13 +++-- .../RemoveMethodInvocationsVisitorTest.java | 47 ++++++++++++++++++- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java index c0a601ad7e2..72691f804b7 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java @@ -77,15 +77,15 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) J.MethodInvocation m = (J.MethodInvocation) expression; boolean isStatic = m.getMethodType().hasFlags(Flag.Static); - if (m.getSelect() == null && !isStatic) { + if ((m.getSelect() == null && !isStatic) || (isStatic && !isStatementInParentBlock(m))) { return expression; } - boolean isStatementOrStatic = isStatement() || isStatic; + boolean isStatement = isStatement(); if (matchers.entrySet().stream().anyMatch(entry -> matches(m, entry.getKey(), entry.getValue()))) { boolean hasSameReturnType = m.getSelect() != null && TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType()); - boolean removable = (isStatementOrStatic && depth == 0) || hasSameReturnType; + boolean removable = ((isStatement || isStatic) && (depth == 0)) || hasSameReturnType; if (!removable) { return expression; } @@ -100,7 +100,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) selectAfter.add(getSelectAfter(m)); return m.getSelect(); } else { - if (isStatementOrStatic) { + if (isStatement) { return null; } else if (isLambdaBody) { return ToBeRemoved.withMarker(J.Block.createEmptyBlock()); @@ -137,6 +137,11 @@ private boolean isStatement() { ).getValue() instanceof J.Block; } + private boolean isStatementInParentBlock(Statement method) { + J.Block parentBlock = getCursor().firstEnclosing(J.Block.class); + return parentBlock == null || parentBlock.getStatements().contains(method); + } + private boolean isLambdaBody() { if (getCursor().getParent() == null) { return false; diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index 11f05d14e09..aa9be6c5c32 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -520,7 +520,7 @@ void method() { } @Test - void removeStaticMethodFromImportWithAssignment() { + void keepStaticMethodFromImportWithAssignment() { rewriteRun( spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), // language=java @@ -541,6 +541,28 @@ void method() { ); } + @Test + void keepStaticMethodFromImportWithAssignment2() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import java.util.List; + + import static java.util.Collections.emptyList; + + class Test { + List emptyList = emptyList(); + void method() { + emptyList.isEmpty(); + } + } + """ + ) + ); + } + @Test void removeStaticMethod() { rewriteRun( @@ -567,7 +589,7 @@ void method() { } @Test - void removeStaticMethodWithAssignment() { + void keepStaticMethodWithAssignment() { rewriteRun( spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), // language=java @@ -586,4 +608,25 @@ void method() { ) ); } + + @Test + void keepStaticMethodWithAssignment2() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), + // language=java + java( + """ + import java.util.List; + import java.util.Collections; + + class Test { + List emptyList = Collections.emptyList(); + void method() { + emptyList.size(); + } + } + """ + ) + ); + } } From 034fcbc1d98f34fb1d9556070a1478b2d251cb04 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 11:57:43 +0100 Subject: [PATCH 09/12] Remove redundant parentheses --- .../org/openrewrite/java/RemoveMethodInvocationsVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java index 72691f804b7..c2361cc8dbf 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java @@ -85,7 +85,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) if (matchers.entrySet().stream().anyMatch(entry -> matches(m, entry.getKey(), entry.getValue()))) { boolean hasSameReturnType = m.getSelect() != null && TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType()); - boolean removable = ((isStatement || isStatic) && (depth == 0)) || hasSameReturnType; + boolean removable = ((isStatement || isStatic) && depth == 0) || hasSameReturnType; if (!removable) { return expression; } From 3094caf2bee23029bf0ade530a5411c62fb81b66 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 12:01:30 +0100 Subject: [PATCH 10/12] Better mame of tests --- .../openrewrite/java/RemoveMethodInvocationsVisitorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index aa9be6c5c32..b3e42e23cf5 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -542,7 +542,7 @@ void method() { } @Test - void keepStaticMethodFromImportWithAssignment2() { + void keepStaticMethodFromImportClassField() { rewriteRun( spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), // language=java @@ -610,7 +610,7 @@ void method() { } @Test - void keepStaticMethodWithAssignment2() { + void keepStaticMethodClassField() { rewriteRun( spec -> spec.recipe(createRemoveMethodsRecipe("java.util.Collections emptyList()")), // language=java From b6e42da3c42402f34c03c90595c8551734bad4ad Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 10 Dec 2024 12:59:52 +0100 Subject: [PATCH 11/12] Add failing test for scenario not yet covered --- .../RemoveMethodInvocationsVisitorTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index b3e42e23cf5..1a17755d596 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -494,6 +494,37 @@ public void method() { ); } + @Test + void removeLocalMethodInvocation() { + rewriteRun( + spec -> spec.recipe(createRemoveMethodsRecipe("a.A b(..)")), + // language=java + java( + """ + package a; + + class A { + void a() { + b(); + } + void b() { + } + } + """, + """ + package a; + + class A { + void a() { + } + void b() { + } + } + """ + ) + ); + } + @Test void removeStaticMethodFromImport() { rewriteRun( From 53df882bd8dbb94ea5101f6718eafefb1b00d203 Mon Sep 17 00:00:00 2001 From: lingenj Date: Tue, 10 Dec 2024 13:26:00 +0100 Subject: [PATCH 12/12] Improvement --- .../java/RemoveMethodInvocationsVisitor.java | 4 +-- .../RemoveMethodInvocationsVisitorTest.java | 32 ------------------- 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java index c2361cc8dbf..4682c999ffa 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/RemoveMethodInvocationsVisitor.java @@ -77,7 +77,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) J.MethodInvocation m = (J.MethodInvocation) expression; boolean isStatic = m.getMethodType().hasFlags(Flag.Static); - if ((m.getSelect() == null && !isStatic) || (isStatic && !isStatementInParentBlock(m))) { + if (isStatic && !isStatementInParentBlock(m)) { return expression; } @@ -92,7 +92,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) maybeRemoveImport(m.getMethodType().getDeclaringType()); - if (isStatic) { + if (m.getSelect() == null) { return null; } else if (m.getSelect() instanceof J.Identifier || m.getSelect() instanceof J.NewClass) { boolean keepSelect = depth != 0; diff --git a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java index 1a17755d596..279e70c1a8b 100644 --- a/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java +++ b/rewrite-java/src/test/java/org/openrewrite/java/RemoveMethodInvocationsVisitorTest.java @@ -173,7 +173,6 @@ void method() { } @Test - @ExpectedToFail void removeWithoutSelect() { rewriteRun( spec -> spec.recipe(createRemoveMethodsRecipe("Test foo()")), @@ -494,37 +493,6 @@ public void method() { ); } - @Test - void removeLocalMethodInvocation() { - rewriteRun( - spec -> spec.recipe(createRemoveMethodsRecipe("a.A b(..)")), - // language=java - java( - """ - package a; - - class A { - void a() { - b(); - } - void b() { - } - } - """, - """ - package a; - - class A { - void a() { - } - void b() { - } - } - """ - ) - ); - } - @Test void removeStaticMethodFromImport() { rewriteRun(