Skip to content

LombokValueToRecord should rewrite method references too #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all 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,32 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
);
}

@Override
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
J.MemberReference memberReference = super.visitMemberReference(memberRef, ctx);

Expression containing = memberReference.getContaining();
if (containing.getType() instanceof JavaType.Class) {
String classFqn = ((JavaType.Class) containing.getType()).getFullyQualifiedName();
J.Identifier reference = memberReference.getReference();
String methodName = reference.getSimpleName();
String newSimpleName = getterMethodNameToFluentMethodName(methodName);
if (recordTypeToMembers.containsKey(classFqn) &&
methodName.startsWith(STANDARD_GETTER_PREFIX) &&
recordTypeToMembers.get(classFqn).contains(newSimpleName)) {

JavaType.Method methodType = memberReference.getMethodType();
if (methodType != null) {
methodType = methodType.withName(newSimpleName);
}
return memberReference
.withReference(reference.withSimpleName(newSimpleName))
.withMethodType(methodType);
}
}
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 @@ -26,6 +26,7 @@

import static org.openrewrite.java.Assertions.*;

@SuppressWarnings({"ClassCanBeRecord", "LombokGetterMayBeUsed", "NullableProblems", "TypeParameterExplicitlyExtendsObject"})
class LombokValueToRecordTest implements RewriteTest {

@Override
Expand All @@ -46,8 +47,6 @@ public void defaults(RecipeSpec spec) {
void convertOnlyValueAnnotatedClassWithoutDefaultValuesToRecord() {
//language=java
rewriteRun(
// TODO: find a way to please type validation so this workaround is not required anymore
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -142,7 +141,6 @@ public String toString() {
void onlyRemoveAnnotationFromRecords() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -185,7 +183,6 @@ public class B {
void innerRecordsNotStatic() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -218,7 +215,6 @@ record B(
void interfaceIsImplementedThatDoesNotDefineFieldGetter() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -248,13 +244,13 @@ public record A(
void plainLombokBuilder() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;

import lombok.Value;
import lombok.Builder;
import java.io.Serializable;

@Value
@Builder
Expand All @@ -266,6 +262,7 @@ public class A implements Serializable {
package example;

import lombok.Builder;
import java.io.Serializable;

@Builder
public record A(
Expand All @@ -274,7 +271,46 @@ public record A(
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449")
@Test
void methodReferences() {
//language=java
rewriteRun(
java(
"""
import lombok.Value;
import java.util.function.Supplier;

@Value
class A {
String test;
}

class Using {
Supplier<String> usingMethodReference() {
A a = new A("foo");
return a::getTest;
}
}
""",
"""
import java.util.function.Supplier;

record A(
String test) {
}

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

@Nested
Expand Down Expand Up @@ -304,7 +340,7 @@ public A() {
void classWithFieldAnnotations() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
s -> s.typeValidationOptions(TypeValidation.all().identifiers(false)),
java(
"""
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -433,7 +469,6 @@ public class A {
void nonStaticInnerClass() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand All @@ -455,7 +490,6 @@ class B {
void staticConstructor() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down