Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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,39 @@ 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);

// Handle method references like a::getTest
Expression containing = memberReference.getContaining();
if (containing != null && containing.getType() instanceof JavaType.Class) {
JavaType.Class classType = (JavaType.Class) containing.getType();
String classFqn = classType.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)) {

// Update the method type if present
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 @@ -46,8 +46,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 +140,6 @@ public String toString() {
void onlyRemoveAnnotationFromRecords() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -185,7 +182,6 @@ public class B {
void innerRecordsNotStatic() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -218,7 +214,6 @@ record B(
void interfaceIsImplementedThatDoesNotDefineFieldGetter() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down Expand Up @@ -248,13 +243,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 +261,7 @@ public class A implements Serializable {
package example;

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

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

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449")
void methodReferences() {
//language=java
rewriteRun(
java(
"""
package example;

import lombok.Value;
import java.util.function.Supplier;

@Value
public class A {
String test;
}

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
Expand Down Expand Up @@ -304,7 +343,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 +472,6 @@ public class A {
void nonStaticInnerClass() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand All @@ -455,7 +493,6 @@ class B {
void staticConstructor() {
//language=java
rewriteRun(
s -> s.typeValidationOptions(TypeValidation.none()),
java(
"""
package example;
Expand Down