Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RemoveMethodInvocationsVisitor can remove static method #4754

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -69,32 +69,38 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return j;
}

private @Nullable J removeMethods(Expression expression, int depth, boolean isLambdaBody, Stack<Space> selectAfter) {
if (!(expression instanceof J.MethodInvocation)) {
private @Nullable J removeMethods(@Nullable Expression expression, int depth, boolean isLambdaBody, Stack<Space> 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 isStatementOrStatic = isStatement() || isStatic;

if (matchers.entrySet().stream().anyMatch(entry -> matches(m, entry.getKey(), entry.getValue()))) {
boolean hasSameReturnType = TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType());
boolean removable = (isStatement && depth == 0) || hasSameReturnType;
boolean hasSameReturnType = m.getSelect() != null && TypeUtils.isAssignableTo(m.getMethodType().getReturnType(), m.getSelect().getType());
boolean removable = (isStatementOrStatic && 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));
return m.getSelect();
} else {
if (isStatement) {
if (isStatementOrStatic) {
return null;
} else if (isLambdaBody) {
return ToBeRemoved.withMarker(J.Block.createEmptyBlock());
Expand Down Expand Up @@ -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 extends J> J2 withMarker(J2 j) {
return j.withMarkers(j.getMarkers().addIfAbsent(new ToBeRemoved(randomId())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -492,4 +493,106 @@ public void method() {
)
);
}

@Test
void removeStaticMethodFromImport() {
rewriteRun(
spec -> spec.recipe(createRemoveMethodsRecipe("org.junit.jupiter.api.Assertions assertTrue(..)")),
// language=java
java(
"""
import static org.junit.jupiter.api.Assertions.assertTrue;

class Test {
void method() {
assertTrue(true);
}
}
""",
"""
class Test {
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<Object> emptyList = emptyList();
}
}
""",
"""
class Test {
void method() {
List<Object> emptyList;
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
}
}
"""
)
);
}

@Test
void removeStaticMethod() {
rewriteRun(
spec -> spec.recipe(createRemoveMethodsRecipe("org.junit.jupiter.api.Assertions assertTrue(..)")),
// language=java
java(
"""
import org.junit.jupiter.api.Assertions;

class Test {
void method() {
Assertions.assertTrue(true);
}
}
""",
"""
class Test {
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<Object> emptyList = Collections.emptyList();
}
}
""",
"""
class Test {
void method() {
List<Object> emptyList;
jevanlingen marked this conversation as resolved.
Show resolved Hide resolved
}
}
"""
)
);
}
}