Skip to content

Commit c853276

Browse files
holgpartimtebeekgithub-actions[bot]
authored
LombokValueToRecord should rewrite method references too (#469)
* try to rewrite method references * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Alter implementation and polish tests * Stop ignoring type validation issues * Also update method type * Light polish * Minimize test * Fix annotation order * Pull up suppressions --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 2c351ac commit c853276

File tree

2 files changed

+69
-9
lines changed

2 files changed

+69
-9
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,32 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
254254
);
255255
}
256256

257+
@Override
258+
public J.MemberReference visitMemberReference(J.MemberReference memberRef, ExecutionContext ctx) {
259+
J.MemberReference memberReference = super.visitMemberReference(memberRef, ctx);
260+
261+
Expression containing = memberReference.getContaining();
262+
if (containing.getType() instanceof JavaType.Class) {
263+
String classFqn = ((JavaType.Class) containing.getType()).getFullyQualifiedName();
264+
J.Identifier reference = memberReference.getReference();
265+
String methodName = reference.getSimpleName();
266+
String newSimpleName = getterMethodNameToFluentMethodName(methodName);
267+
if (recordTypeToMembers.containsKey(classFqn) &&
268+
methodName.startsWith(STANDARD_GETTER_PREFIX) &&
269+
recordTypeToMembers.get(classFqn).contains(newSimpleName)) {
270+
271+
JavaType.Method methodType = memberReference.getMethodType();
272+
if (methodType != null) {
273+
methodType = methodType.withName(newSimpleName);
274+
}
275+
return memberReference
276+
.withReference(reference.withSimpleName(newSimpleName))
277+
.withMethodType(methodType);
278+
}
279+
}
280+
return memberReference;
281+
}
282+
257283
private boolean isMethodInvocationOnRecordTypeClassMember(J.MethodInvocation methodInvocation) {
258284
Expression expression = methodInvocation.getSelect();
259285
if (!isClassExpression(expression)) {

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

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

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

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

3132
@Override
@@ -46,8 +47,6 @@ public void defaults(RecipeSpec spec) {
4647
void convertOnlyValueAnnotatedClassWithoutDefaultValuesToRecord() {
4748
//language=java
4849
rewriteRun(
49-
// TODO: find a way to please type validation so this workaround is not required anymore
50-
s -> s.typeValidationOptions(TypeValidation.none()),
5150
java(
5251
"""
5352
package example;
@@ -142,7 +141,6 @@ public String toString() {
142141
void onlyRemoveAnnotationFromRecords() {
143142
//language=java
144143
rewriteRun(
145-
s -> s.typeValidationOptions(TypeValidation.none()),
146144
java(
147145
"""
148146
package example;
@@ -185,7 +183,6 @@ public class B {
185183
void innerRecordsNotStatic() {
186184
//language=java
187185
rewriteRun(
188-
s -> s.typeValidationOptions(TypeValidation.none()),
189186
java(
190187
"""
191188
package example;
@@ -218,7 +215,6 @@ record B(
218215
void interfaceIsImplementedThatDoesNotDefineFieldGetter() {
219216
//language=java
220217
rewriteRun(
221-
s -> s.typeValidationOptions(TypeValidation.none()),
222218
java(
223219
"""
224220
package example;
@@ -248,13 +244,13 @@ public record A(
248244
void plainLombokBuilder() {
249245
//language=java
250246
rewriteRun(
251-
s -> s.typeValidationOptions(TypeValidation.none()),
252247
java(
253248
"""
254249
package example;
255250
256251
import lombok.Value;
257252
import lombok.Builder;
253+
import java.io.Serializable;
258254
259255
@Value
260256
@Builder
@@ -266,6 +262,7 @@ public class A implements Serializable {
266262
package example;
267263
268264
import lombok.Builder;
265+
import java.io.Serializable;
269266
270267
@Builder
271268
public record A(
@@ -274,7 +271,46 @@ public record A(
274271
"""
275272
)
276273
);
274+
}
275+
276+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449")
277+
@Test
278+
void methodReferences() {
279+
//language=java
280+
rewriteRun(
281+
java(
282+
"""
283+
import lombok.Value;
284+
import java.util.function.Supplier;
285+
286+
@Value
287+
class A {
288+
String test;
289+
}
290+
291+
class Using {
292+
Supplier<String> usingMethodReference() {
293+
A a = new A("foo");
294+
return a::getTest;
295+
}
296+
}
297+
""",
298+
"""
299+
import java.util.function.Supplier;
277300
301+
record A(
302+
String test) {
303+
}
304+
305+
class Using {
306+
Supplier<String> usingMethodReference() {
307+
A a = new A("foo");
308+
return a::test;
309+
}
310+
}
311+
"""
312+
)
313+
);
278314
}
279315

280316
@Nested
@@ -304,7 +340,7 @@ public A() {
304340
void classWithFieldAnnotations() {
305341
//language=java
306342
rewriteRun(
307-
s -> s.typeValidationOptions(TypeValidation.none()),
343+
s -> s.typeValidationOptions(TypeValidation.all().identifiers(false)),
308344
java(
309345
"""
310346
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -433,7 +469,6 @@ public class A {
433469
void nonStaticInnerClass() {
434470
//language=java
435471
rewriteRun(
436-
s -> s.typeValidationOptions(TypeValidation.none()),
437472
java(
438473
"""
439474
package example;
@@ -455,7 +490,6 @@ class B {
455490
void staticConstructor() {
456491
//language=java
457492
rewriteRun(
458-
s -> s.typeValidationOptions(TypeValidation.none()),
459493
java(
460494
"""
461495
package example;

0 commit comments

Comments
 (0)