Skip to content

Commit

Permalink
Do not ReplaceLambdaWithMethodReference for class fields (#256)
Browse files Browse the repository at this point in the history
* Do not ReplaceLambdaWithMethodReference for class fields

As references might change.

* Also check if fields are final
  • Loading branch information
timtebeek authored Feb 9, 2024
1 parent 8760cf0 commit 59bf480
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
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();
}
}
"""
)
);
}
}

0 comments on commit 59bf480

Please sign in to comment.