private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
    if (original instanceof GrField) {
      final PsiClass containingClass = ((GrField)original).getContainingClass();
      LOG.assertTrue(containingClass != null);
      return (GrVariableDeclaration)containingClass.addBefore(declaration, original.getParent());
    }

    final GrStatementOwner block;
    if (original instanceof PsiParameter) {
      final PsiElement container = original.getParent().getParent();
      if (container instanceof GrMethod) {
        block = ((GrMethod)container).getBlock();
      }
      else if (container instanceof GrClosableBlock) {
        block = (GrCodeBlock)container;
      }
      else if (container instanceof GrForStatement) {
        final GrStatement body = ((GrForStatement)container).getBody();
        if (body instanceof GrBlockStatement) {
          block = ((GrBlockStatement)body).getBlock();
        }
        else {
          GrBlockStatement blockStatement = myFactory.createBlockStatement();
          LOG.assertTrue(blockStatement != null);
          if (body != null) {
            blockStatement.getBlock().addStatementBefore((GrStatement)body.copy(), null);
            blockStatement = (GrBlockStatement)body.replace(blockStatement);
          }
          else {
            blockStatement = (GrBlockStatement)container.add(blockStatement);
          }
          block = blockStatement.getBlock();
        }
      }
      else {
        throw new IncorrectOperationException();
      }

      LOG.assertTrue(block != null);
      return (GrVariableDeclaration)block.addStatementBefore(declaration, null);
    }

    PsiElement parent = original.getParent();
    LOG.assertTrue(parent instanceof GrVariableDeclaration);

    final PsiElement pparent = parent.getParent();

    if (pparent instanceof GrIfStatement) {
      if (((GrIfStatement)pparent).getThenBranch() == parent) {
        block = ((GrIfStatement)pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
      }
      else {
        block = ((GrIfStatement)pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
      }
      parent = block.addStatementBefore(((GrVariableDeclaration)parent), null);
    }
    else if (pparent instanceof GrLoopStatement) {
      block = ((GrLoopStatement)pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
      parent = block.addStatementBefore(((GrVariableDeclaration)parent), null);
    }
    else {
      LOG.assertTrue(pparent instanceof GrStatementOwner);
      block = (GrStatementOwner)pparent;
    }

    return (GrVariableDeclaration)block.addStatementBefore(declaration, (GrStatement)parent);
  }
private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration)