private void replaceReferences(PsiCodeBlock body) {
    final PsiManager manager = myMethod.getManager();
    JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    final PsiElementFactory factory = facade.getElementFactory();
    final Map<PsiElement, PsiElement> replaceMap = new HashMap<>();
    body.accept(new JavaRecursiveElementVisitor() {
      @Override public void visitThisExpression(@NotNull PsiThisExpression expression) {
        final PsiClass classReferencedByThis = MoveInstanceMembersUtil.getClassReferencedByThis(expression);
        if (classReferencedByThis != null && !PsiTreeUtil.isAncestor(myMethod, classReferencedByThis, false)) {
          final PsiElementFactory factory = JavaPsiFacade.getElementFactory(myProject);
          String paramName = getParameterNameToCreate(classReferencedByThis);
          try {
            final PsiExpression refExpression = factory.createExpressionFromText(paramName, null);
            replaceMap.put(expression, refExpression);
          }
          catch (IncorrectOperationException e) {
            LOG.error(e);
          }
        }
      }

      @Override public void visitReferenceExpression(@NotNull PsiReferenceExpression expression) {
        try {
          final PsiExpression qualifier = expression.getQualifierExpression();
          final PsiElement resolved = expression.resolve();
          if (ExpressionUtils.isReferenceTo(qualifier, myTargetVariable)) {
            if (resolved instanceof PsiField) {
              String fieldName = ((PsiField)resolved).getName();
              for (PsiParameter parameter : myMethod.getParameterList().getParameters()) {
                if (Comparing.strEqual(parameter.getName(), fieldName) ||
                    facade.getResolveHelper().resolveReferencedVariable(fieldName, expression) != null) {
                  qualifier.replace(factory.createExpressionFromText("this", null));
                  return;
                }
              }
            }
            if (expression instanceof PsiMethodReferenceExpression) {
              qualifier.replace(factory.createExpressionFromText("this", null));
            }
            else {
              //Target is a field, replace target.m -> m
              qualifier.delete();
            }
            return;
          }
          if (myTargetVariable.equals(resolved)) {
            PsiThisExpression thisExpression = RefactoringChangeUtil.createThisExpression(
              manager, PsiTreeUtil.isAncestor(myMethod, PsiUtil.getContainingClass(expression), true) ? myTargetClass : null);
            replaceMap.put(expression, thisExpression);
            return;
          }
          if (!myMethod.equals(resolved)) {
            PsiClass classReferencedByThis = MoveInstanceMembersUtil.getClassReferencedByThis(expression);
            if (classReferencedByThis != null) {
              final String paramName = getParameterNameToCreate(classReferencedByThis);
              if (paramName != null) {
                PsiReferenceExpression newQualifier = (PsiReferenceExpression)factory.createExpressionFromText(paramName, null);
                expression.setQualifierExpression(newQualifier);
                return;
              }
            }
          }
          super.visitReferenceExpression(expression);
        }
        catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }

      @Override public void visitNewExpression(@NotNull PsiNewExpression expression) {
        try {
          final PsiExpression qualifier = expression.getQualifier();
          if (ExpressionUtils.isReferenceTo(qualifier, myTargetVariable)) {
            //Target is a field, replace target.new A() -> new A()
            qualifier.delete();
          } else {
            final PsiClass classReferencedByThis = MoveInstanceMembersUtil.getClassReferencedByThis(expression);
            if (classReferencedByThis != null) {
              if (qualifier != null) qualifier.delete();
              final String paramName = getParameterNameToCreate(classReferencedByThis);
              final PsiExpression newExpression = factory.createExpressionFromText(paramName + "." + expression.getText(), null);
              replaceMap.put(expression, newExpression);
            }
          }
          super.visitNewExpression(expression);
        }
        catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }

      @Override public void visitMethodCallExpression(@NotNull PsiMethodCallExpression expression) {
        correctMethodCall(expression, true);
        super.visitMethodCallExpression(expression);
      }
    });
    for (PsiElement element : replaceMap.keySet()) {
      final PsiElement replacement = replaceMap.get(element);
      element.replace(replacement);
    }
  }
private void replaceReferences(PsiCodeBlock body)