Skip to content
Open
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
20 changes: 13 additions & 7 deletions src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ static boolean isSetter(Cursor cursor) {
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
if (variable instanceof J.Identifier) {
J.Identifier assignedVar = (J.Identifier) variable;
if (hasMatchingSetterMethodName(method, assignedVar.getSimpleName())) {
if (hasMatchingSetterMethodName(method, assignedVar.getType(), assignedVar.getSimpleName())) {
if (assignedVar.getFieldType() != null && declaringType == assignedVar.getFieldType().getOwner()) {
// Don't replace instance methods accessing static fields
return !assignedVar.getFieldType().hasFlags(Flag.Static);
}
}
} else if (variable instanceof J.FieldAccess) {
J.FieldAccess assignedField = (J.FieldAccess) variable;
if (hasMatchingSetterMethodName(method, assignedField.getSimpleName())) {
if (hasMatchingSetterMethodName(method, assignedField.getType(), assignedField.getSimpleName())) {
Expression target = assignedField.getTarget();
// Only handle "this.field" pattern, not "someObject.field"
if (target instanceof J.Identifier && "this".equals(((J.Identifier) target).getSimpleName())) {
Expand All @@ -167,9 +167,9 @@ static boolean isSetter(Cursor cursor) {
return false;
}

private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, String simpleName) {
return method.getSimpleName().equals("set" + StringUtils.capitalize(simpleName));
}
private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, @Nullable JavaType type, String simpleName) {
return method.getSimpleName().equals(deriveSetterMethodName(type, simpleName));
}

public static boolean isEffectivelySetter(J.MethodDeclaration method) {
if (method.getType() != JavaType.Primitive.Void) {
Expand Down Expand Up @@ -201,8 +201,14 @@ public static boolean isEffectivelySetter(J.MethodDeclaration method) {
}

public static String deriveSetterMethodName(JavaType.Variable fieldType) {
return "set" + StringUtils.capitalize(fieldType.getName());
}
return deriveSetterMethodName(fieldType.getType(), fieldType.getName());
}

private static String deriveSetterMethodName(@Nullable JavaType type, String fieldName) {
String setterBaseName = type == JavaType.Primitive.Boolean && IS_PREFIX.matcher(fieldName).lookingAt() ?
fieldName.substring(2) : fieldName;
return "set" + StringUtils.capitalize(setterBaseName);
}

static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) {
if (methodDeclaration.hasModifier(Public)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,46 @@ class A {
);
}

@Test
void renamePrimitiveBooleanWithIsPrefix() {
rewriteRun(// language=java
java(
"""
class A {
boolean isFoo;
void storeFoo(boolean isFoo) { this.isFoo = isFoo; }
}
""",
"""
class A {
boolean isFoo;
void setFoo(boolean isFoo) { this.isFoo = isFoo; }
}
"""
)
);
}

@Test
void renameBoxedBooleanWithIsPrefix() {
rewriteRun(// language=java
java(
"""
class A {
Boolean isFoo;
void storeFoo(Boolean isFoo) { this.isFoo = isFoo; }
}
""",
"""
class A {
Boolean isFoo;
void setIsFoo(Boolean isFoo) { this.isFoo = isFoo; }
}
"""
)
);
}

@Test
void renameClassBooleanInSingleClass() {
rewriteRun(// language=java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,51 @@ class A {
);
}

@Test
void replacePrimitiveBooleanSetterWithIsPrefix() {
rewriteRun(// language=java
java(
"""
class A {

boolean isFoo;

public void setFoo(boolean isFoo) {
this.isFoo = isFoo;
}
}
""",
"""
import lombok.Setter;

class A {

@Setter
boolean isFoo;
}
"""
)
);
}

@Test
void doesNotReplacePrimitiveBooleanSetterWithWrongLombokName() {
rewriteRun(// language=java
java(
"""
class A {

boolean isFoo;

public void setIsFoo(boolean isFoo) {
this.isFoo = isFoo;
}
}
"""
)
);
}

@Test
void replaceBoolean() {
rewriteRun(// language=java
Expand Down