Skip to content

Commit c7d3feb

Browse files
committed
Push down method and variable name matching into utils
1 parent 91c579b commit c7d3feb

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.common.collect.ImmutableMap;
1919
import lombok.AccessLevel;
20+
import org.jspecify.annotations.Nullable;
2021
import org.openrewrite.internal.StringUtils;
2122
import org.openrewrite.java.tree.Expression;
2223
import org.openrewrite.java.tree.J;
@@ -41,21 +42,27 @@ static boolean isEffectivelyGetter(J.MethodDeclaration method) {
4142
!(method.getBody().getStatements().get(0) instanceof J.Return)) {
4243
return false;
4344
}
45+
// Check return: type and matching field name
4446
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
45-
//returns just an identifier
4647
if (returnExpression instanceof J.Identifier) {
4748
J.Identifier identifier = (J.Identifier) returnExpression;
48-
JavaType.Variable fieldType = identifier.getFieldType();
49-
return method.getType().equals(fieldType.getType());
49+
return hasMatchingTypeAndName(method, identifier.getType(), identifier.getSimpleName());
5050
} else if (returnExpression instanceof J.FieldAccess) {
5151
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
52-
JavaType fieldType = fieldAccess.getType();
53-
return method.getType().equals(fieldType);
52+
return hasMatchingTypeAndName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
5453
}
5554
return false;
5655
}
5756

58-
static String deriveGetterMethodName(JavaType type, String fieldName) {
57+
private static boolean hasMatchingTypeAndName(J.MethodDeclaration method, @Nullable JavaType type, String simpleName) {
58+
if (method.getType().equals(type)) {
59+
String deriveGetterMethodName = deriveGetterMethodName(type, simpleName);
60+
return method.getSimpleName().equals(deriveGetterMethodName);
61+
}
62+
return false;
63+
}
64+
65+
private static String deriveGetterMethodName(@Nullable JavaType type, String fieldName) {
5966
if (type == JavaType.Primitive.Boolean) {
6067
boolean alreadyStartsWithIs = fieldName.length() >= 3 &&
6168
fieldName.substring(0, 3).matches("is[A-Z]");

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.Set;
3535

3636
import static java.util.Comparator.comparing;
37-
import static org.openrewrite.java.tree.JavaType.Variable;
3837

3938
@Value
4039
@EqualsAndHashCode(callSuper = false)
@@ -92,24 +91,18 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
9291
@Override
9392
public J.@Nullable MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
9493
if (method.getMethodType() != null && LombokUtils.isEffectivelyGetter(method)) {
95-
J.Return return_ = (J.Return) method.getBody().getStatements().get(0);
96-
Expression returnExpression = return_.getExpression();
94+
Set<Finding> set = getCursor().getNearestMessage(FIELDS_TO_DECORATE_KEY);
95+
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
9796
if (returnExpression instanceof J.Identifier) {
98-
J.Identifier identifier = (J.Identifier) returnExpression;
99-
String deriveGetterMethodName = LombokUtils.deriveGetterMethodName(identifier.getType(), identifier.getSimpleName());
100-
if (method.getSimpleName().equals(deriveGetterMethodName)) {
101-
((Set<Finding>) getCursor().getNearestMessage(FIELDS_TO_DECORATE_KEY))
102-
.add(new Finding(identifier.getSimpleName(), LombokUtils.getAccessLevel(method.getModifiers())));
103-
return null;
104-
}
97+
set.add(new Finding(
98+
((J.Identifier) returnExpression).getSimpleName(),
99+
LombokUtils.getAccessLevel(method.getModifiers())));
100+
return null;
105101
} else if (returnExpression instanceof J.FieldAccess) {
106-
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
107-
String deriveGetterMethodName = LombokUtils.deriveGetterMethodName(fieldAccess.getType(), fieldAccess.getSimpleName());
108-
if (method.getSimpleName().equals(deriveGetterMethodName)) {
109-
((Set<Finding>) getCursor().getNearestMessage(FIELDS_TO_DECORATE_KEY))
110-
.add(new Finding(fieldAccess.getSimpleName(), LombokUtils.getAccessLevel(method.getModifiers())));
111-
return null;
112-
}
102+
set.add(new Finding(
103+
((J.FieldAccess) returnExpression).getSimpleName(),
104+
LombokUtils.getAccessLevel(method.getModifiers())));
105+
return null;
113106
}
114107
}
115108
return method;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class A {
248248
public A() {
249249
}
250250
251-
long getfoo() {//return type wrong
251+
long getFoo() { //return type wrong
252252
return foo;
253253
}
254254
}

0 commit comments

Comments
 (0)