Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(check, new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
if (IMMUTABLE_MATCHER.matches(method) && isParentTypeDownCast()) {
if (IMMUTABLE_MATCHER.matches(method) && isParentTypeDownCast(method)) {
maybeRemoveImport(guavaType);
maybeAddImport(javaType);

Expand Down Expand Up @@ -115,7 +115,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
return super.visitMethodInvocation(method, ctx);
}

private boolean isParentTypeDownCast() {
private boolean isParentTypeDownCast(MethodCall immutableMethod) {
J parent = getCursor().dropParentUntil(J.class::isInstance).getValue();
boolean isParentTypeDownCast = false;
if (parent instanceof J.VariableDeclarations.NamedVariable) {
Expand All @@ -138,14 +138,8 @@ private boolean isParentTypeDownCast() {
}
} else if (parent instanceof J.MethodInvocation) {
J.MethodInvocation m = (J.MethodInvocation) parent;
if (m.getMethodType() != null) {
int index = 0;
for (Expression argument : m.getArguments()) {
if (IMMUTABLE_MATCHER.matches(argument)) {
break;
}
index++;
}
int index = m.getArguments().indexOf(immutableMethod);
if (m.getMethodType() != null && index != -1) {
isParentTypeDownCast = isParentTypeMatched(m.getMethodType().getParameterTypes().get(index));
}
} else if (parent instanceof J.NewClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,41 @@ class Test {
);
}

@Test
void doNotChangeMethodInvocationWithSelect() {
//language=java
rewriteRun(
java(
"""
import java.util.List;

public class A {
Object[] list;
public void method(Object[] list ) {
this.list = list;
}
}
"""
),
version(
//language=java
java(
"""
import com.google.common.collect.ImmutableList;

class Test {
void method() {
A a = new A();
a.method(ImmutableList.of().toArray());
}
}
"""
),
11
)
);
}

@Test
void methodInvocationWithListArgument() {
//language=java
Expand Down