EncapsulateFieldUsageInfo (@NotNull EncapsulateFieldsDescriptor descriptor, @NotNull FieldDescriptor fieldDescriptor, @NotNull PsiReference reference) { if (!(reference instanceof PsiReferenceExpression ref)) return null; boolean findSet = descriptor.isToEncapsulateSet(); boolean findGet = descriptor.isToEncapsulateGet(); // [Jeka] to avoid recursion in the field's accessors if (findGet && isUsedInExistingAccessor(descriptor.getTargetClass(), fieldDescriptor.getGetterPrototype(), ref)) return null; if (findSet && isUsedInExistingAccessor(descriptor.getTargetClass(), fieldDescriptor.getSetterPrototype(), ref)) return null; if (!findGet) { if (!PsiUtil.isAccessedForWriting(ref)) return null; } if (!findSet || fieldDescriptor.getField().hasModifierProperty(PsiModifier.FINAL)) { if (!PsiUtil.isAccessedForReading(ref)) return null; } if (!descriptor.isToUseAccessorsWhenAccessible()) { PsiModifierList newModifierList = createNewModifierList(descriptor); PsiClass accessObjectClass = null; PsiExpression qualifier = ref.getQualifierExpression(); if (qualifier != null) { accessObjectClass = (PsiClass)PsiUtil.getAccessObjectClass(qualifier).getElement(); } final PsiResolveHelper helper = JavaPsiFacade.getInstance(((PsiReferenceExpression)reference).getProject()).getResolveHelper(); if (helper.isAccessible(fieldDescriptor.getField(), newModifierList, ref, accessObjectClass, null)) { return null; } } return new EncapsulateFieldUsageInfo(ref, fieldDescriptor); }