Skip to content
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 @@ -117,7 +117,6 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
}

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

private final @Nullable Boolean useExactToString;
private final Map<String, Set<String>> recordTypeToMembers;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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 "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down