2222import org .openrewrite .java .AnnotationMatcher ;
2323import org .openrewrite .java .service .AnnotationService ;
2424import org .openrewrite .java .tree .Expression ;
25+ import org .openrewrite .java .tree .Flag ;
2526import org .openrewrite .java .tree .J ;
2627import org .openrewrite .java .tree .JavaType ;
2728
@@ -62,6 +63,10 @@ static boolean isGetter(Cursor cursor, AnnotationService service) {
6263 if (returnExpression instanceof J .Identifier ) {
6364 J .Identifier identifier = (J .Identifier ) returnExpression ;
6465 if (identifier .getFieldType () != null && declaringType == identifier .getFieldType ().getOwner ()) {
66+ // Don't replace instance methods accessing static fields
67+ if (identifier .getFieldType ().hasFlags (Flag .Static )) {
68+ return false ;
69+ }
6570 // Check return: type and matching field name
6671 return hasMatchingTypeAndGetterName (method , identifier .getType (), identifier .getSimpleName ());
6772 }
@@ -70,6 +75,11 @@ static boolean isGetter(Cursor cursor, AnnotationService service) {
7075 Expression target = fieldAccess .getTarget ();
7176 if (target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
7277 declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ()) {
78+ // Don't replace instance methods accessing static fields
79+ if (fieldAccess .getName ().getFieldType () != null &&
80+ fieldAccess .getName ().getFieldType ().hasFlags (Flag .Static )) {
81+ return false ;
82+ }
7383 // Check return: type and matching field name
7484 return hasMatchingTypeAndGetterName (method , fieldAccess .getType (), fieldAccess .getSimpleName ());
7585 }
@@ -153,15 +163,22 @@ static boolean isSetter(Cursor cursor, AnnotationService service) {
153163 J .Identifier assignedVar = (J .Identifier ) variable ;
154164 if (hasMatchingSetterMethodName (method , assignedVar .getSimpleName ())) {
155165 // Check field is declared on method type
156- return assignedVar .getFieldType () != null && declaringType == assignedVar .getFieldType ().getOwner ();
166+ if (assignedVar .getFieldType () != null && declaringType == assignedVar .getFieldType ().getOwner ()) {
167+ // Don't replace instance methods accessing static fields
168+ return !assignedVar .getFieldType ().hasFlags (Flag .Static );
169+ }
157170 }
158171 } else if (variable instanceof J .FieldAccess ) {
159172 J .FieldAccess assignedField = (J .FieldAccess ) variable ;
160173 if (hasMatchingSetterMethodName (method , assignedField .getSimpleName ())) {
161174 Expression target = assignedField .getTarget ();
162175 // Check field is declared on method type
163- return target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
164- declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ();
176+ if (target instanceof J .Identifier && ((J .Identifier ) target ).getFieldType () != null &&
177+ declaringType == ((J .Identifier ) target ).getFieldType ().getOwner ()) {
178+ // Don't replace instance methods accessing static fields
179+ return assignedField .getName ().getFieldType () != null &&
180+ !assignedField .getName ().getFieldType ().hasFlags (Flag .Static );
181+ }
165182 }
166183 }
167184
0 commit comments