Skip to content

Commit a118477

Browse files
committed
Add remaining checks to make all tests pass
1 parent 868328b commit a118477

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ static boolean isGetter(J.MethodDeclaration method) {
4848
J.Identifier identifier = (J.Identifier) returnExpression;
4949
if (identifier.getFieldType() != null && declaringType == identifier.getFieldType().getOwner()) {
5050
// Check return: type and matching field name
51-
return hasMatchingTypeAndName(method, identifier.getType(), identifier.getSimpleName());
51+
return hasMatchingTypeAndGetterName(method, identifier.getType(), identifier.getSimpleName());
5252
}
5353
} else if (returnExpression instanceof J.FieldAccess) {
5454
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
5555
Expression target = fieldAccess.getTarget();
5656
if (target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
5757
declaringType == ((J.Identifier) target).getFieldType().getOwner()) {
5858
// Check return: type and matching field name
59-
return hasMatchingTypeAndName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
59+
return hasMatchingTypeAndGetterName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
6060
}
6161
}
6262
return false;
6363
}
6464

65-
private static boolean hasMatchingTypeAndName(J.MethodDeclaration method, @Nullable JavaType type, String simpleName) {
65+
private static boolean hasMatchingTypeAndGetterName(J.MethodDeclaration method, @Nullable JavaType type, String simpleName) {
6666
if (method.getType() == type) {
6767
String deriveGetterMethodName = deriveGetterMethodName(type, simpleName);
6868
return method.getSimpleName().equals(deriveGetterMethodName);
@@ -83,6 +83,10 @@ private static String deriveGetterMethodName(@Nullable JavaType type, String fie
8383
return "get" + StringUtils.capitalize(fieldName);
8484
}
8585

86+
private static boolean hasMatchingSetterMethodName(J.MethodDeclaration method, String simpleName) {
87+
return method.getSimpleName().equals("set" + StringUtils.capitalize(simpleName));
88+
}
89+
8690
static boolean isSetter(J.MethodDeclaration method) {
8791
// Check return type: void
8892
if (method.getType() != JavaType.Primitive.Void) {
@@ -98,39 +102,39 @@ static boolean isSetter(J.MethodDeclaration method) {
98102
!(method.getBody().getStatements().get(0) instanceof J.Assignment)) {
99103
return false;
100104
}
101-
J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0);
102-
J.FieldAccess assignedField = (J.FieldAccess) assignment.getVariable();
103-
if (!method.getSimpleName().equals(deriveSetterMethodName(assignedField))) {
104-
return false;
105-
}
105+
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
106106

107-
// Check argument is assigned to field
107+
// Method parameter
108108
J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) method.getParameters().get(0);
109109
J.VariableDeclarations.NamedVariable param = variableDeclarations.getVariables().get(0);
110110

111-
// type of parameter and field have to match
112-
if (!param.getType().equals(assignedField.getType())) {
111+
// Check there's no up/down cast between parameter and field
112+
J.Assignment assignment = (J.Assignment) method.getBody().getStatements().get(0);
113+
Expression variable = assignment.getVariable();
114+
if (param.getType() != variable.getType()) {
113115
return false;
114116
}
115117

116-
// Check field is declared on method type
117-
JavaType.FullyQualified declaringType = method.getMethodType().getDeclaringType();
118-
if (assignedField.getTarget() instanceof J.Identifier) {
119-
J.Identifier target = (J.Identifier) assignedField.getTarget();
120-
return target.getFieldType() != null && declaringType == target.getFieldType().getOwner();
121-
} else if (assignedField.getTarget() instanceof J.FieldAccess) {
122-
Expression target = ((J.FieldAccess) assignedField.getTarget()).getTarget();
123-
return target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
124-
declaringType == ((J.Identifier) target).getFieldType().getOwner();
118+
// Method name has to match
119+
if (variable instanceof J.Identifier) {
120+
J.Identifier assignedVar = (J.Identifier) variable;
121+
if (hasMatchingSetterMethodName(method, assignedVar.getSimpleName())) {
122+
// Check field is declared on method type
123+
return assignedVar.getFieldType() != null && declaringType == assignedVar.getFieldType().getOwner();
124+
}
125+
} else if (variable instanceof J.FieldAccess) {
126+
J.FieldAccess assignedField = (J.FieldAccess) variable;
127+
if (hasMatchingSetterMethodName(method, assignedField.getSimpleName())) {
128+
Expression target = assignedField.getTarget();
129+
// Check field is declared on method type
130+
return target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
131+
declaringType == ((J.Identifier) target).getFieldType().getOwner();
132+
}
125133
}
126134

127135
return false;
128136
}
129137

130-
private static String deriveSetterMethodName(J.FieldAccess fieldAccess) {
131-
return "set" + StringUtils.capitalize(fieldAccess.getSimpleName());
132-
}
133-
134138
static AccessLevel getAccessLevel(J.MethodDeclaration methodDeclaration) {
135139
if (methodDeclaration.hasModifier(Public)) {
136140
return PUBLIC;

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Copyright 2024 the original author or authors.
33
* <p>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* Licensed under the Moderne Source Available License (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
* <p>
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
99
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,13 +23,12 @@
2323
import org.openrewrite.Recipe;
2424
import org.openrewrite.TreeVisitor;
2525
import org.openrewrite.java.JavaIsoVisitor;
26+
import org.openrewrite.java.tree.Expression;
2627
import org.openrewrite.java.tree.J;
2728

2829
import java.util.Collections;
2930
import java.util.Set;
3031

31-
import static org.openrewrite.java.tree.JavaType.Variable;
32-
3332
@Value
3433
@EqualsAndHashCode(callSuper = false)
3534
public class UseLombokSetter extends Recipe {
@@ -41,7 +40,6 @@ public String getDisplayName() {
4140

4241
@Override
4342
public String getDescription() {
44-
//language=markdown
4543
return "Convert trivial setter methods to `@Setter` annotations on their respective fields.";
4644
}
4745

@@ -56,11 +54,21 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5654
@Override
5755
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
5856
if (LombokUtils.isSetter(method)) {
59-
J.Assignment assignment_ = (J.Assignment) method.getBody().getStatements().get(0);
60-
J.FieldAccess fieldAccess = (J.FieldAccess) assignment_.getVariable();
61-
Variable fieldType = fieldAccess.getName().getFieldType();
62-
doAfterVisit(new FieldAnnotator(Setter.class, fieldType, LombokUtils.getAccessLevel(method)));
63-
return null; //delete
57+
Expression assignmentVariable = ((J.Assignment) method.getBody().getStatements().get(0)).getVariable();
58+
if (assignmentVariable instanceof J.FieldAccess &&
59+
((J.FieldAccess) assignmentVariable).getName().getFieldType() != null) {
60+
doAfterVisit(new FieldAnnotator(Setter.class,
61+
((J.FieldAccess) assignmentVariable).getName().getFieldType(),
62+
LombokUtils.getAccessLevel(method)));
63+
return null; //delete
64+
65+
} else if (assignmentVariable instanceof J.Identifier &&
66+
((J.Identifier) assignmentVariable).getFieldType() != null) {
67+
doAfterVisit(new FieldAnnotator(Setter.class,
68+
((J.Identifier) assignmentVariable).getFieldType(),
69+
LombokUtils.getAccessLevel(method)));
70+
return null; //delete
71+
}
6472
}
6573
return method;
6674
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* Copyright 2024 the original author or authors.
33
* <p>
4-
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* Licensed under the Moderne Source Available License (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
* <p>
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
99
* <p>
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,

0 commit comments

Comments
 (0)