Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -254,6 +254,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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -277,6 +279,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<String> usingMethodReference() {
A a = new A("foo");
return a::getTest;
}

}
""",
"""
package example;

import java.util.function.Supplier;

public record A(
String test) {
}

class Using {

Supplier<String> usingMethodReference() {
A a = new A("foo");
return a::test;
}

}
"""
)
);

}

@Nested
class Unchanged {
@Test
Expand Down
Loading