@Override
    public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
      super.visitReferenceExpression(expression);
      boolean isLockedOnInstance = false;
      boolean isLockedOnClass = false;
      final GrMethod containingMethod =
          PsiTreeUtil.getParentOfType(expression, GrMethod.class);
      if (containingMethod != null &&
          containingMethod.hasModifierProperty(
              PsiModifier.SYNCHRONIZED)) {
        if (containingMethod.hasModifierProperty(
            PsiModifier.STATIC)) {
          isLockedOnClass = true;
        } else {
          isLockedOnInstance = true;
        }
      }
      PsiElement elementToCheck = expression;
      while (true) {
        final GrSynchronizedStatement syncStatement = PsiTreeUtil.getParentOfType(elementToCheck, GrSynchronizedStatement.class);
        if (syncStatement == null) {
          break;
        }
        final GrExpression lockExpression = syncStatement.getMonitor();

        if (lockExpression instanceof GrReferenceExpression && PsiUtil.isThisReference(lockExpression)) {
          isLockedOnInstance = true;
        }
        else if (lockExpression instanceof GrReferenceExpression reference) {
          final PsiElement referent = reference.resolve();
          if (referent instanceof PsiField referentField) {
            if (referentField.hasModifierProperty(PsiModifier.STATIC)) {
              isLockedOnClass = true;
            } else {
              isLockedOnInstance = true;
            }
          }
        }
        elementToCheck = syncStatement;
      }
      if (!isLockedOnInstance || isLockedOnClass) {
        return;
      }
      final PsiElement referent = expression.resolve();
      if (!(referent instanceof PsiField referredField)) {
        return;
      }
      if (!referredField.hasModifierProperty(PsiModifier.STATIC) ||
          isConstant(referredField)) {
        return;
      }
      final PsiClass containingClass = referredField.getContainingClass();
      if (!PsiTreeUtil.isAncestor(containingClass, expression, false)) {
        return;
      }
      registerError(expression);
    }
@Override
    public void visitReferenceExpression(@NotNull GrReferenceExpression expression)