Skip to content

Commit 9f029e3

Browse files
Create is methods for boolean fields in LombokValueToRecord (#813)
* Create `is` methods for boolean fields in `LombokValueToRecord` Fixes #812 * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Also convert boolean methods * Update expectations around `Boolean` fields * Condense logic --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent bc6f9dc commit 9f029e3

File tree

2 files changed

+104
-18
lines changed

2 files changed

+104
-18
lines changed

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
117117
}
118118

119119
private boolean isRelevantClass(J.ClassDeclaration classDeclaration) {
120-
List<J.Annotation> allAnnotations = classDeclaration.getAllAnnotations();
121120
return classDeclaration.getType() != null &&
122121
J.ClassDeclaration.Kind.Type.Record != classDeclaration.getKind() &&
123122
hasMatchingAnnotations(classDeclaration) &&
@@ -231,6 +230,7 @@ private static class LombokValueToRecordVisitor extends JavaIsoVisitor<Execution
231230
private static final String TO_STRING_MEMBER_LINE_PATTERN = "\"%s=\" + %s +";
232231
private static final String TO_STRING_MEMBER_DELIMITER = "\", \" +\n";
233232
private static final String STANDARD_GETTER_PREFIX = "get";
233+
private static final String BOOLEAN_GETTER_PREFIX = "is";
234234

235235
private final @Nullable Boolean useExactToString;
236236
private final Map<String, Set<String>> recordTypeToMembers;
@@ -249,10 +249,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
249249
}
250250

251251
J.Identifier methodName = methodInvocation.getName();
252-
return methodInvocation
253-
.withName(methodName
254-
.withSimpleName(getterMethodNameToFluentMethodName(methodName.getSimpleName()))
255-
);
252+
return methodInvocation.withName(
253+
methodName.withSimpleName(
254+
getterMethodNameToFluentMethodName(methodName.getSimpleName())));
256255
}
257256

258257
@Override
@@ -264,18 +263,19 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Execu
264263
String classFqn = ((JavaType.Class) containing.getType()).getFullyQualifiedName();
265264
J.Identifier reference = memberReference.getReference();
266265
String methodName = reference.getSimpleName();
267-
String newSimpleName = getterMethodNameToFluentMethodName(methodName);
268-
if (recordTypeToMembers.containsKey(classFqn) &&
269-
methodName.startsWith(STANDARD_GETTER_PREFIX) &&
270-
recordTypeToMembers.get(classFqn).contains(newSimpleName)) {
271266

272-
JavaType.Method methodType = memberReference.getMethodType();
273-
if (methodType != null) {
274-
methodType = methodType.withName(newSimpleName);
267+
if (recordTypeToMembers.containsKey(classFqn) &&
268+
(methodName.startsWith(STANDARD_GETTER_PREFIX) || methodName.startsWith(BOOLEAN_GETTER_PREFIX))) {
269+
String newSimpleName = getterMethodNameToFluentMethodName(methodName);
270+
if (recordTypeToMembers.get(classFqn).contains(newSimpleName)) {
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);
275278
}
276-
return memberReference
277-
.withReference(reference.withSimpleName(newSimpleName))
278-
.withMethodType(methodType);
279279
}
280280
}
281281
return memberReference;
@@ -296,8 +296,9 @@ private boolean isMethodInvocationOnRecordTypeClassMember(J.MethodInvocation met
296296
String classFqn = classType.getFullyQualifiedName();
297297

298298
return recordTypeToMembers.containsKey(classFqn) &&
299-
methodName.startsWith(STANDARD_GETTER_PREFIX) &&
300-
recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName));
299+
(methodName.startsWith(STANDARD_GETTER_PREFIX) || methodName.startsWith(BOOLEAN_GETTER_PREFIX)) &&
300+
recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName));
301+
301302
}
302303

303304
private static boolean isClassExpression(@Nullable Expression expression) {
@@ -306,7 +307,7 @@ private static boolean isClassExpression(@Nullable Expression expression) {
306307

307308
private static String getterMethodNameToFluentMethodName(String methodName) {
308309
StringBuilder fluentMethodName = new StringBuilder(
309-
methodName.replace(STANDARD_GETTER_PREFIX, ""));
310+
methodName.replaceFirst("^(get|is)", ""));
310311

311312
if (fluentMethodName.length() == 0) {
312313
return "";

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,91 @@ public record A(
273273
);
274274
}
275275

276+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/812")
277+
@Test
278+
void booleanFieldWithIsGetter() {
279+
//language=java
280+
rewriteRun(
281+
java(
282+
"""
283+
import lombok.Value;
284+
285+
@Value
286+
public class Foo {
287+
boolean bar;
288+
}
289+
""",
290+
"""
291+
public record Foo(
292+
boolean bar) {
293+
}"""
294+
),
295+
java(
296+
"""
297+
public class Baz {
298+
public void baz(Foo foo) {
299+
foo.isBar();
300+
}
301+
}
302+
""",
303+
"""
304+
public class Baz {
305+
public void baz(Foo foo) {
306+
foo.bar();
307+
}
308+
}
309+
"""
310+
)
311+
);
312+
}
313+
314+
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/812")
315+
@Test
316+
void multipleBooleanFields() {
317+
//language=java
318+
rewriteRun(
319+
java(
320+
"""
321+
import lombok.Value;
322+
323+
@Value
324+
public class Config {
325+
boolean enabled;
326+
Boolean active;
327+
String name;
328+
}
329+
""",
330+
"""
331+
public record Config(
332+
boolean enabled,
333+
Boolean active,
334+
String name) {
335+
}
336+
"""
337+
),
338+
java(
339+
"""
340+
public class ConfigUser {
341+
public void useConfig(Config config) {
342+
if (config.isEnabled() && config.getActive()) {
343+
System.out.println(config.getName());
344+
}
345+
}
346+
}
347+
""",
348+
"""
349+
public class ConfigUser {
350+
public void useConfig(Config config) {
351+
if (config.enabled() && config.active()) {
352+
System.out.println(config.name());
353+
}
354+
}
355+
}
356+
"""
357+
)
358+
);
359+
}
360+
276361
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449")
277362
@Test
278363
void methodReferences() {

0 commit comments

Comments
 (0)