Skip to content

Commit 038637c

Browse files
committed
try to rewrite method references
1 parent dda4fa4 commit 038637c

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,28 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
236236
);
237237
}
238238

239+
@Override
240+
public J.MemberReference visitMemberReference(J.MemberReference memberReference, ExecutionContext ctx) {
241+
// when "memberReference" is "a::getTest"
242+
// memberReference.getMethodType() == null
243+
JavaType.Method methodType = memberReference.getMethodType();
244+
if(methodType == null) {
245+
return memberReference;
246+
}
247+
248+
JavaType.FullyQualified declaringType = methodType.getDeclaringType();
249+
String methodName = methodType.getName();
250+
String classFqn = declaringType.getFullyQualifiedName();
251+
if(recordTypeToMembers.containsKey(classFqn)
252+
&& recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName))
253+
&& methodName.startsWith(STANDARD_GETTER_PREFIX)) {
254+
return memberReference.withMethodType(methodType.withName(getterMethodNameToFluentMethodName(methodName)));
255+
}
256+
257+
return memberReference;
258+
}
259+
260+
239261
private boolean isMethodInvocationOnRecordTypeClassMember(J.MethodInvocation methodInvocation) {
240262
Expression expression = methodInvocation.getSelect();
241263
if (!isClassExpression(expression)) {

src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.openrewrite.test.RewriteTest;
2525
import org.openrewrite.test.TypeValidation;
2626

27+
import java.util.function.Supplier;
28+
2729
import static org.openrewrite.java.Assertions.*;
2830

2931
class LombokValueToRecordTest implements RewriteTest {
@@ -243,6 +245,76 @@ public record A(
243245
);
244246
}
245247

248+
public static class A {
249+
String test;
250+
251+
public String getTest() {
252+
return test;
253+
}
254+
}
255+
@Test
256+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449")
257+
void methodReferences() {
258+
259+
var a = new A();
260+
261+
//language=java
262+
rewriteRun(
263+
s -> s.typeValidationOptions(TypeValidation.none()),
264+
java(
265+
"""
266+
package example;
267+
268+
import lombok.Value;
269+
import java.util.function.Supplier;
270+
271+
@Value
272+
public class A {
273+
String test;
274+
275+
}
276+
277+
class B {
278+
279+
280+
public static String classMethod() {
281+
return "foo";
282+
}
283+
284+
}
285+
286+
class Using {
287+
288+
Supplier<String> usingMethodReference() {
289+
A a = new A("foo");
290+
return a::getTest;
291+
}
292+
293+
}
294+
""",
295+
"""
296+
package example;
297+
298+
import java.util.function.Supplier;
299+
300+
public record A(
301+
String test) {
302+
}
303+
304+
class Using {
305+
306+
Supplier<String> usingMethodReference() {
307+
A a = new A("foo");
308+
return a::test;
309+
}
310+
311+
}
312+
"""
313+
)
314+
);
315+
316+
}
317+
246318
@Nested
247319
class Unchanged {
248320
@Test

0 commit comments

Comments
 (0)