Skip to content

Commit f4e6852

Browse files
Add Functional and self-calls options to MemberReferenceToMethodInvocation (#771)
* Add Functional and self-calls options to `MemberReferenceToMethodInvocation`. * Update src/test/java/org/openrewrite/java/spring/util/MemberReferenceToMethodInvocationTest.java Co-authored-by: Tim te Beek <[email protected]> * Test both `this` and `super` --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent ea8af5c commit f4e6852

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/main/java/org/openrewrite/java/spring/util/MemberReferenceToMethodInvocation.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import lombok.Value;
1919
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.internal.StringUtils;
2021
import org.openrewrite.java.JavaTemplate;
2122
import org.openrewrite.java.JavaVisitor;
2223
import org.openrewrite.java.VariableNameUtils;
@@ -37,11 +38,23 @@ public J visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx)
3738
}
3839

3940
List<Param> args = getLambdaArgNames(mr.getMethodType());
40-
String templateCode = String.format("(%s) -> %s.%s(%s)",
41-
args.stream().map(Param::toString).collect(joining(", ")),
42-
mr.getContaining(),
43-
mr.getReference().getSimpleName(),
44-
args.stream().map(Param::getName).collect(joining(", ")));
41+
String templateCode;
42+
if (!mr.getMethodType().getParameterTypes().isEmpty()) {
43+
templateCode = String.format("(%s) -> %s.%s(%s)",
44+
args.stream().map(Param::toString).collect(joining(", ")),
45+
mr.getContaining(),
46+
mr.getReference().getSimpleName(),
47+
args.stream().map(Param::getName).collect(joining(", ")));
48+
} else if (mr.getContaining() instanceof J.Identifier && ("this".equals(((J.Identifier) mr.getContaining()).getSimpleName()) || "super".equals(((J.Identifier) mr.getContaining()).getSimpleName()))) {
49+
templateCode = String.format("() -> %s.%s()",
50+
mr.getContaining(),
51+
mr.getReference().getSimpleName());
52+
} else {
53+
templateCode = String.format("(%s) -> %s.%s()",
54+
args.stream().map(Param::toString).collect(joining(", ")),
55+
args.get(0).getName(),
56+
mr.getReference().getSimpleName());
57+
}
4558
return JavaTemplate.builder(templateCode)
4659
.contextSensitive()
4760
.build().apply(getCursor(), mr.getCoordinates().replace())
@@ -61,6 +74,12 @@ private List<Param> getLambdaArgNames(JavaType.Method methodType) {
6174

6275
params.add(new Param(type, uniqueVariableName));
6376
}
77+
if (params.isEmpty()) {
78+
JavaType.FullyQualified type = methodType.getDeclaringType();
79+
String uniqueVariableName = VariableNameUtils.generateVariableName(StringUtils.uncapitalize(type.getClassName()), getCursor(), VariableNameUtils.GenerationStrategy.INCREMENT_NUMBER);
80+
81+
params.add(new Param(type, uniqueVariableName));
82+
}
6483
return params;
6584
}
6685

src/test/java/org/openrewrite/java/spring/util/MemberReferenceToMethodInvocationTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.openrewrite.java.spring.util;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.params.ParameterizedTest;
20+
import org.junit.jupiter.params.provider.ValueSource;
1921
import org.openrewrite.DocumentExample;
2022
import org.openrewrite.InMemoryExecutionContext;
2123
import org.openrewrite.java.JavaParser;
@@ -62,4 +64,61 @@ void test(ListenableFuture<String> future) {
6264
)
6365
);
6466
}
67+
68+
@Test
69+
void methodReferenceWithoutArguments() {
70+
//language=java
71+
rewriteRun(
72+
java(
73+
"""
74+
import java.util.Optional;
75+
76+
class A {
77+
void test() {
78+
byte[] result = Optional.of("Test").map(String::getBytes).orElse(null);
79+
}
80+
}
81+
""",
82+
"""
83+
import java.util.Optional;
84+
85+
class A {
86+
void test() {
87+
byte[] result = Optional.of("Test").map((String string) -> string.getBytes()).orElse(null);
88+
}
89+
}
90+
"""
91+
)
92+
);
93+
}
94+
95+
@ParameterizedTest
96+
@ValueSource(strings = {"this", "super"})
97+
void superMethodReferenceWithoutArguments(String qualifier) {
98+
//language=java
99+
rewriteRun(
100+
java(
101+
"""
102+
import java.util.Optional;
103+
import java.util.function.Supplier;
104+
105+
class A {
106+
public Supplier<String> getString() {
107+
return %s::toString;
108+
}
109+
}
110+
""".formatted(qualifier),
111+
"""
112+
import java.util.Optional;
113+
import java.util.function.Supplier;
114+
115+
class A {
116+
public Supplier<String> getString() {
117+
return () -> %s.toString();
118+
}
119+
}
120+
""".formatted(qualifier)
121+
)
122+
);
123+
}
65124
}

0 commit comments

Comments
 (0)