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 @@ -159,6 +159,7 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
}

if (hasSelectWithPotentialSideEffects(method) ||
hasSelectWhoseReferenceMightChange(method) ||
!methodArgumentsMatchLambdaParameters(method, lambda) ||
method instanceof J.MemberReference) {
return l;
Expand Down Expand Up @@ -212,6 +213,14 @@ private boolean hasSelectWithPotentialSideEffects(MethodCall method) {
((J.MethodInvocation) method).getSelect() instanceof MethodCall;
}

private boolean hasSelectWhoseReferenceMightChange(MethodCall method) {
if (method instanceof J.MethodInvocation && ((J.MethodInvocation) method).getSelect() instanceof J.Identifier) {
JavaType.Variable fieldType = ((J.Identifier) ((J.MethodInvocation) method).getSelect()).getFieldType();
return fieldType != null && fieldType.getOwner() instanceof JavaType.Class && !fieldType.hasFlags(Flag.Final);
}
return false;
}

private boolean methodArgumentsMatchLambdaParameters(MethodCall method, J.Lambda lambda) {
JavaType.Method methodType = method.getMethodType();
if (methodType == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ void multipleMethodInvocations() {
"""
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;import java.util.stream.Collectors;
import java.util.List;
import java.util.stream.Collectors;

class Test {
Path path = Paths.get("");
Expand Down Expand Up @@ -190,6 +191,7 @@ List<Object> method(List<Object> input) {
}
}
""",
//language=java
"""
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -237,6 +239,7 @@ List<Object> method(List<Object> input) {
}
}
""",
//language=java
"""
import org.test.CheckType;

Expand Down Expand Up @@ -288,6 +291,7 @@ Stream<Integer> method(List<CheckType> input) {
}
}
""",
//language=java
"""
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -409,8 +413,10 @@ public void run() {
Collections.singletonList(1).forEach(n -> run());
}
}
Test t = new Test();
Runnable r = () -> t.run();
void foo() {
Test t = new Test();
Runnable r = () -> t.run();
}
}
""",
"""
Expand All @@ -423,8 +429,10 @@ public void run() {
Collections.singletonList(1).forEach(n -> run());
}
}
Test t = new Test();
Runnable r = t::run;
void foo() {
Test t = new Test();
Runnable r = t::run;
}
}
"""
)
Expand Down Expand Up @@ -595,6 +603,7 @@ List<Object> filter(List<Object> l) {
}
}
""",
//language=java
"""
import org.test.CheckType;

Expand Down Expand Up @@ -1293,8 +1302,8 @@ private Integer foo(String bar) {

@Test
void newClassSelector() {
//language=java
rewriteRun(
//language=java
java(
"""
class A {
Expand Down Expand Up @@ -1345,4 +1354,22 @@ public void groupOnGetClass() {
);
}

@Test
void dontReplaceNullableFieldReferences() {
//language=java
rewriteRun(
java(
"""
import java.util.function.Supplier;
class A {
Object field;
void foo() {
// Runtime exception when replaced with field::toString
Supplier<String> supplier = () -> field.toString();
}
}
"""
)
);
}
}