private static void replaceElementWithExpression(PsiExpression expression,
                                                   PsiElementFactory factory,
                                                   PsiElement element) throws IncorrectOperationException {
    PsiElement elementToReplace = element;
    PsiElement expressionToReplaceWith = expression;
    if (element.getParent() instanceof PsiExpressionStatement || element.getParent() instanceof PsiExpressionListStatement) {
      elementToReplace = element.getParent();
      expressionToReplaceWith = factory.createStatementFromText((expression == null ? "" : expression.getText()) + ";", null);
      if (isForLoopUpdate(elementToReplace)) {
        PsiElement lastChild = expressionToReplaceWith.getLastChild();
        if (PsiUtil.isJavaToken(lastChild, JavaTokenType.SEMICOLON)) {
          lastChild.delete();
        }
      }
    }
    else if (element.getParent() instanceof PsiDeclarationStatement) {
      expressionToReplaceWith = factory.createStatementFromText((expression == null ? "" : expression.getText()) + ";", null);
    }
    elementToReplace.replace(expressionToReplaceWith);
  }
private static void replaceElementWithExpression(PsiExpression expression,
                                                   PsiElementFactory factory,
                                                   PsiElement element)