private void resolveOldReferences(PsiElement expr, PsiElement oldExpr) throws IncorrectOperationException {
    if (expr == null || !expr.isValid() || oldExpr == null) return;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    PsiElement newExpr = expr;  // references continue being resolved in the children of newExpr

    if (oldExpr instanceof GrReferenceExpression oldRef) {
      if (isThisReferenceToContainingClass(oldExpr) || isSimpleSuperReference(oldExpr)) {
        if (myInstanceRef != null) {
          newExpr.replace(getInstanceRef(factory));
        }
        return;
      }

      newExpr = newExpr.replace(decodeReferenceExpression((GrReferenceExpression)newExpr, oldRef));
      //newExpr = ((GrReferenceExpression)newExpr).getReferenceNameElement();
      final GroovyResolveResult adv = oldRef.advancedResolve();
      final PsiElement scope = getClassContainingResolve(adv);
      final PsiElement owner = PsiUtil.getContextClass(oldExpr);

      if (myToReplaceIn instanceof GrClosableBlock || (owner != null && scope != null && PsiTreeUtil.isContextAncestor(owner, scope, false))) {

        final PsiElement subj = adv.getElement();

        // Parameters
        if (subj instanceof PsiParameter) {
          int index = ArrayUtil.indexOf(myParameters, subj);
          if (index < 0) return;
          if (index < myParameters.length) {
            newExpr = inlineParam(newExpr, getActualArg(index), ((PsiParameter)subj));
          }
        }
        // "naked" field and methods  (should become qualified)
        else if ((subj instanceof PsiField || subj instanceof PsiMethod) && oldRef.getQualifierExpression() == null) {

          PsiElement newResolved = newExpr instanceof GrReferenceExpression ? ((GrReferenceExpression)newExpr).resolve() : null;
          if (myInstanceRef != null || !subj.getManager().areElementsEquivalent(newResolved, subj)) {
            boolean isStatic = subj instanceof PsiField && ((PsiField)subj).hasModifierProperty(PsiModifier.STATIC) ||
                               subj instanceof PsiMethod && ((PsiMethod)subj).hasModifierProperty(PsiModifier.STATIC);

            String name = getNewName((PsiNamedElement)subj, adv.isInvokedOnProperty());
            boolean shouldBeAt = subj instanceof PsiField &&
                                 !PsiTreeUtil.isAncestor(((PsiMember)subj).getContainingClass(), newExpr, true) &&
                                 GroovyPropertyUtils.findGetterForField((PsiField)subj) != null;
            final GrReferenceExpression fromText = factory.createReferenceExpressionFromText("qualifier." + (shouldBeAt ? "@" : "") + name);
            if (isStatic) {
              final GrReferenceExpression qualifier = factory.createReferenceElementForClass(((PsiMember)subj).getContainingClass());
              newExpr = newExpr.replace(fromText);
              ((GrReferenceExpression)newExpr).setQualifier(qualifier);
              newExpr = ((GrReferenceExpression)newExpr).getReferenceNameElement();
            }
            else {
              if (myInstanceRef != null) {
                GrExpression instanceRef = getInstanceRef(factory);
                fromText.setQualifier(instanceRef);
                newExpr = newExpr.replace(fromText);
                newExpr = ((GrReferenceExpression)newExpr).getReferenceNameElement();
              }
            }
          }
        }

        if (subj instanceof PsiField) {
          // probably replacing field with a getter
          if (myReplaceFieldsWithGetters != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE) {
            if (myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_ALL ||
                myReplaceFieldsWithGetters == IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_INACCESSIBLE &&
                !JavaPsiFacade.getInstance(myProject).getResolveHelper().isAccessible((PsiMember)subj, newExpr, null)) {
              newExpr = replaceFieldWithGetter(newExpr, (PsiField)subj);
            }
          }
        }
      }
    }
    else {
      PsiClass refClass = oldExpr.getCopyableUserData(ChangeContextUtil.REF_CLASS_KEY);
      if (refClass != null && refClass.isValid()) {
        PsiReference ref = newExpr.getReference();
        if (ref != null) {
          final String qualifiedName = refClass.getQualifiedName();
          if (qualifiedName != null) {
            if (JavaPsiFacade.getInstance(refClass.getProject()).findClass(qualifiedName, oldExpr.getResolveScope()) != null) {
              newExpr = ref.bindToElement(refClass);
            }
          }
        }
      }
    }


    PsiElement[] oldChildren = oldExpr.getChildren();
    PsiElement[] newChildren = newExpr.getChildren();

    if (oldExpr instanceof GrNewExpression && newExpr instanceof GrNewExpression) { //special new-expression case
      resolveOldReferences(((GrNewExpression)newExpr).getReferenceElement(),
                           ((GrNewExpression)oldExpr).getReferenceElement());

      resolveOldReferences(((GrNewExpression)newExpr).getArgumentList(), ((GrNewExpression)oldExpr).getArgumentList());
      if (newChildren[1] instanceof GrArrayDeclaration) {
        for (GrExpression expression : ((GrArrayDeclaration)newChildren[1]).getBoundExpressions()) {
          resolveOldReferences(expression, oldChildren[1]);
        }
      }
    }
    else {
      if (oldExpr instanceof GrReferenceExpression && newExpr instanceof GrReferenceExpression) {
        final GrExpression oldQualifier = ((GrReferenceExpression)oldExpr).getQualifierExpression();
        final GrExpression newQualifier = ((GrReferenceExpression)newExpr).getQualifierExpression();
        if (oldQualifier != null && newQualifier != null) {
          resolveOldReferences(newQualifier, oldQualifier);
          return;
        }
      }

      if (oldChildren.length == newChildren.length) {
        for (int i = 0; i < newChildren.length; i++) {
          resolveOldReferences(newChildren[i], oldChildren[i]);
        }
      }
    }
  }
private void resolveOldReferences(PsiElement expr, PsiElement oldExpr)