Skip to content
Merged
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
31 changes: 18 additions & 13 deletions src/main/java/org/openrewrite/java/migrate/lombok/LombokUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ static boolean isGetter(Cursor cursor, AnnotationService service) {
} else if (returnExpression instanceof J.FieldAccess) {
J.FieldAccess fieldAccess = (J.FieldAccess) returnExpression;
Expression target = fieldAccess.getTarget();
if (target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
declaringType == ((J.Identifier) target).getFieldType().getOwner()) {
// Don't replace instance methods accessing static fields
// Only handle "this.field" pattern, not "someObject.field"
if (target instanceof J.Identifier && "this".equals(((J.Identifier) target).getSimpleName())) {
// Check field is declared on method type
if (fieldAccess.getName().getFieldType() != null &&
fieldAccess.getName().getFieldType().hasFlags(Flag.Static)) {
return false;
declaringType == fieldAccess.getName().getFieldType().getOwner()) {
// Don't replace instance methods accessing static fields
if (fieldAccess.getName().getFieldType().hasFlags(Flag.Static)) {
return false;
}
// Check return: type and matching field name
return hasMatchingTypeAndGetterName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
}
// Check return: type and matching field name
return hasMatchingTypeAndGetterName(method, fieldAccess.getType(), fieldAccess.getSimpleName());
}
}
return false;
Expand Down Expand Up @@ -172,12 +175,14 @@ static boolean isSetter(Cursor cursor, AnnotationService service) {
J.FieldAccess assignedField = (J.FieldAccess) variable;
if (hasMatchingSetterMethodName(method, assignedField.getSimpleName())) {
Expression target = assignedField.getTarget();
// Check field is declared on method type
if (target instanceof J.Identifier && ((J.Identifier) target).getFieldType() != null &&
declaringType == ((J.Identifier) target).getFieldType().getOwner()) {
// Don't replace instance methods accessing static fields
return assignedField.getName().getFieldType() != null &&
!assignedField.getName().getFieldType().hasFlags(Flag.Static);
// Only handle "this.field" pattern, not "someObject.field"
if (target instanceof J.Identifier && "this".equals(((J.Identifier) target).getSimpleName())) {
// Check field is declared on method type
if (assignedField.getName().getFieldType() != null &&
declaringType == assignedField.getName().getFieldType().getOwner()) {
// Don't replace instance methods accessing static fields
return !assignedField.getName().getFieldType().hasFlags(Flag.Static);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,29 @@ int getField() {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/872")
@Test
void noChangeWhenMethodReturnsFieldOfAnotherObject() {
rewriteRun(// language=java
java(
"""
public class A {

private class Sample {
public Long number;
}

private class Inner {
static Sample sample = null;

private Long getNumber() {
return sample.number;
}
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,29 @@ void setField(int field) {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/872")
@Test
void noChangeWhenMethodSetsFieldOfAnotherObject() {
rewriteRun(// language=java
java(
"""
public class A {

private class Sample {
public Long number;
}

private class Inner {
static Sample sample = null;

private void setNumber(Long value) {
sample.number = value;
}
}
}
"""
)
);
}
}