diff --git a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java index a87196915b..f707a2a829 100644 --- a/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java +++ b/src/main/java/org/openrewrite/java/migrate/lombok/LombokValueToRecord.java @@ -117,7 +117,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex } private boolean isRelevantClass(J.ClassDeclaration classDeclaration) { - List allAnnotations = classDeclaration.getAllAnnotations(); return classDeclaration.getType() != null && J.ClassDeclaration.Kind.Type.Record != classDeclaration.getKind() && hasMatchingAnnotations(classDeclaration) && @@ -231,6 +230,7 @@ private static class LombokValueToRecordVisitor extends JavaIsoVisitor> recordTypeToMembers; @@ -249,10 +249,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } J.Identifier methodName = methodInvocation.getName(); - return methodInvocation - .withName(methodName - .withSimpleName(getterMethodNameToFluentMethodName(methodName.getSimpleName())) - ); + return methodInvocation.withName( + methodName.withSimpleName( + getterMethodNameToFluentMethodName(methodName.getSimpleName()))); } @Override @@ -264,18 +263,19 @@ public J.MemberReference visitMemberReference(J.MemberReference memberRef, Execu 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); + if (recordTypeToMembers.containsKey(classFqn) && + (methodName.startsWith(STANDARD_GETTER_PREFIX) || methodName.startsWith(BOOLEAN_GETTER_PREFIX))) { + String newSimpleName = getterMethodNameToFluentMethodName(methodName); + if (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 - .withReference(reference.withSimpleName(newSimpleName)) - .withMethodType(methodType); } } return memberReference; @@ -296,8 +296,9 @@ private boolean isMethodInvocationOnRecordTypeClassMember(J.MethodInvocation met String classFqn = classType.getFullyQualifiedName(); return recordTypeToMembers.containsKey(classFqn) && - methodName.startsWith(STANDARD_GETTER_PREFIX) && - recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName)); + (methodName.startsWith(STANDARD_GETTER_PREFIX) || methodName.startsWith(BOOLEAN_GETTER_PREFIX)) && + recordTypeToMembers.get(classFqn).contains(getterMethodNameToFluentMethodName(methodName)); + } private static boolean isClassExpression(@Nullable Expression expression) { @@ -306,7 +307,7 @@ private static boolean isClassExpression(@Nullable Expression expression) { private static String getterMethodNameToFluentMethodName(String methodName) { StringBuilder fluentMethodName = new StringBuilder( - methodName.replace(STANDARD_GETTER_PREFIX, "")); + methodName.replaceFirst("^(get|is)", "")); if (fluentMethodName.length() == 0) { return ""; diff --git a/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java b/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java index 0f0888df4c..e44bcad45a 100644 --- a/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lombok/LombokValueToRecordTest.java @@ -273,6 +273,91 @@ public record A( ); } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/812") + @Test + void booleanFieldWithIsGetter() { + //language=java + rewriteRun( + java( + """ + import lombok.Value; + + @Value + public class Foo { + boolean bar; + } + """, + """ + public record Foo( + boolean bar) { + }""" + ), + java( + """ + public class Baz { + public void baz(Foo foo) { + foo.isBar(); + } + } + """, + """ + public class Baz { + public void baz(Foo foo) { + foo.bar(); + } + } + """ + ) + ); + } + + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/812") + @Test + void multipleBooleanFields() { + //language=java + rewriteRun( + java( + """ + import lombok.Value; + + @Value + public class Config { + boolean enabled; + Boolean active; + String name; + } + """, + """ + public record Config( + boolean enabled, + Boolean active, + String name) { + } + """ + ), + java( + """ + public class ConfigUser { + public void useConfig(Config config) { + if (config.isEnabled() && config.getActive()) { + System.out.println(config.getName()); + } + } + } + """, + """ + public class ConfigUser { + public void useConfig(Config config) { + if (config.enabled() && config.active()) { + System.out.println(config.name()); + } + } + } + """ + ) + ); + } + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/449") @Test void methodReferences() {