From 038637c14a864af5132ed84cff924d73c277f5e0 Mon Sep 17 00:00:00 2001 From: Holger Partsch Date: Wed, 1 May 2024 15:42:33 +0200 Subject: [PATCH] try to rewrite method references --- .../migrate/lombok/LombokValueToRecord.java | 22 ++++++ .../lombok/LombokValueToRecordTest.java | 72 +++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java index 7e734eedab..9509385f9d 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java @@ -236,6 +236,28 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu ); } + @Override + public J.MemberReference visitMemberReference(J.MemberReference memberReference, ExecutionContext ctx) { + // when "memberReference" is "a::getTest" + // memberReference.getMethodType() == null + JavaType.Method methodType = memberReference.getMethodType(); + if(methodType == null) { + return memberReference; + } + + JavaType.FullyQualified declaringType = methodType.getDeclaringType(); + String methodName = methodType.getName(); + String classFqn = declaringType.getFullyQualifiedName(); + if(recordTypeToMembers.containsKey(classFqn) + && recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName)) + && methodName.startsWith(STANDARD_GETTER_PREFIX)) { + return memberReference.withMethodType(methodType.withName(getterMethodNameToFluentMethodName(methodName))); + } + + return memberReference; + } + + private boolean isMethodInvocationOnRecordTypeClassMember(J.MethodInvocation methodInvocation) { Expression expression = methodInvocation.getSelect(); if (!isClassExpression(expression)) { diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java index d0ebcd4e6e..7de608b393 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java @@ -24,6 +24,8 @@ import org.openrewrite.test.RewriteTest; import org.openrewrite.test.TypeValidation; +import java.util.function.Supplier; + import static org.openrewrite.java.Assertions.*; class LombokValueToRecordTest implements RewriteTest { @@ -243,6 +245,76 @@ public record A( ); } + public static class A { + String test; + + public String getTest() { + return test; + } + } + @Test + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449") + void methodReferences() { + + var a = new A(); + + //language=java + rewriteRun( + s -> s.typeValidationOptions(TypeValidation.none()), + java( + """ + package example; + + import lombok.Value; + import java.util.function.Supplier; + + @Value + public class A { + String test; + + } + + class B { + + + public static String classMethod() { + return "foo"; + } + + } + + class Using { + + Supplier usingMethodReference() { + A a = new A("foo"); + return a::getTest; + } + + } + """, + """ + package example; + + import java.util.function.Supplier; + + public record A( + String test) { + } + + class Using { + + Supplier usingMethodReference() { + A a = new A("foo"); + return a::test; + } + + } + """ + ) + ); + + } + @Nested class Unchanged { @Test