Skip to content

Commit

Permalink
Add one more style we ought to cover
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 15, 2024
1 parent 81c9699 commit 868328b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,32 @@ static boolean isSetter(J.MethodDeclaration method) {
return false;
}
J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0);
J.FieldAccess fieldAccess = (J.FieldAccess) assignment.getVariable();
if (!method.getSimpleName().equals(deriveSetterMethodName(fieldAccess))) {
J.FieldAccess assignedField = (J.FieldAccess) assignment.getVariable();
if (!method.getSimpleName().equals(deriveSetterMethodName(assignedField))) {
return false;
}

// Check argument is assigned to field
J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) method.getParameters().get(0);
J.VariableDeclarations.NamedVariable param = variableDeclarations.getVariables().get(0);
JavaType paramType = param.getType();
String paramName = param.getName().toString();

// type of parameter and field have to match
if (!param.getType().equals(assignedField.getType())) {
return false;
}

return
// assigned value is exactly the parameter
assignment.getAssignment().toString().equals(paramName) // type of parameter and field have to match
&&

// type of parameter and field have to match
param.getType().equals(fieldAccess.getType());
// Check field is declared on method type
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
if (assignedField.getTarget() instanceof J.Identifier) {
J.Identifier target = (J.Identifier) assignedField.getTarget();
return target.getFieldType() != null && declaringType == target.getFieldType().getOwner();
} else if (assignedField.getTarget() instanceof J.FieldAccess) {
Expression target = ((J.FieldAccess) assignedField.getTarget()).getTarget();
return target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
declaringType == ((J.Identifier) target).getFieldType().getOwner();
}

return false;
}

private static String deriveSetterMethodName(J.FieldAccess fieldAccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,60 @@ class A {
);
}

@Test
void replaceSetterWhenArgNameWithUnderscore() {
rewriteRun(// language=java
java(
"""
class A {
int foo = 9;
public void setFoo(int foo_) {
this.foo = foo_;
}
}
""",
"""
import lombok.Setter;
class A {
@Setter
int foo = 9;
}
"""
)
);
}

@Test
void replaceSetterWhenArgNameWithUnderscoreAndUnqualifiedFieldAccess() {
rewriteRun(// language=java
java(
"""
class A {
int foo = 9;
public void setFoo(int foo_) {
foo = foo_;
}
}
""",
"""
import lombok.Setter;
class A {
@Setter
int foo = 9;
}
"""
)
);
}

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

0 comments on commit 868328b

Please sign in to comment.