Skip to content

Commit 40a44b1

Browse files
committed
Check that getter methods access fields from the method declaring type
1 parent 87495fd commit 40a44b1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,27 @@ static boolean isGetter(J.MethodDeclaration method) {
4242
!(method.getBody().getStatements().get(0) instanceof J.Return)) {
4343
return false;
4444
}
45+
// Check field is declared on method type
46+
JavaType.Method methodType = method.getMethodType();
47+
if (methodType == null) {
48+
return false;
49+
}
50+
JavaType.FullyQualified declaringType = methodType.getDeclaringType();
51+
4552
// Check return: type and matching field name
4653
Expression returnExpression = ((J.Return) method.getBody().getStatements().get(0)).getExpression();
4754
if (returnExpression instanceof J.Identifier) {
4855
J.Identifier identifier = (J.Identifier) returnExpression;
49-
return hasMatchingTypeAndName(method, identifier.getType(), identifier.getSimpleName());
56+
if (identifier.getFieldType() != null && declaringType == identifier.getFieldType().getOwner()) {
57+
return hasMatchingTypeAndName(method, identifier.getType(), identifier.getSimpleName());
58+
}
5059
} else if (returnExpression instanceof J.FieldAccess) {
5160
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
52-
return hasMatchingTypeAndName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
61+
Expression target = fieldAccess.getTarget();
62+
if (target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
63+
declaringType == ((J.Identifier) target).getFieldType().getOwner()) {
64+
return hasMatchingTypeAndName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
65+
}
5366
}
5467
return false;
5568
}

0 commit comments

Comments
 (0)