void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiForStatement forStatement = ObjectUtils.tryCast(element.getParent(), PsiForStatement.class); if (forStatement == null) return; CommentTracker commentTracker = new CommentTracker(); PsiStatement initialization = forStatement.getInitialization(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject()); final PsiWhileStatement whileStatement = (PsiWhileStatement)factory.createStatementFromText("while(true) {}", element); final PsiExpression forCondition = forStatement.getCondition(); final PsiExpression whileCondition = whileStatement.getCondition(); if (forCondition != null) { assert whileCondition != null; commentTracker.replace(whileCondition, forCondition); } final PsiBlockStatement blockStatement = (PsiBlockStatement)whileStatement.getBody(); if (blockStatement == null) { return; } final PsiStatement forStatementBody = forStatement.getBody(); final PsiElement loopBody; if (forStatementBody instanceof PsiBlockStatement) { final PsiBlockStatement newWhileBody = (PsiBlockStatement)blockStatement.replace(commentTracker.markUnchanged(forStatementBody)); loopBody = newWhileBody.getCodeBlock(); } else { final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); if (forStatementBody != null && !(forStatementBody instanceof PsiEmptyStatement)) { codeBlock.add(commentTracker.markUnchanged(forStatementBody)); } loopBody = codeBlock; } final PsiStatement update = forStatement.getUpdate(); if (update != null) { final PsiStatement[] updateStatements; if (update instanceof PsiExpressionListStatement expressionListStatement) { final PsiExpressionList expressionList = expressionListStatement.getExpressionList(); final PsiExpression[] expressions = expressionList.getExpressions(); updateStatements = new PsiStatement[expressions.length]; for (int i = 0; i < expressions.length; i++) { updateStatements[i] = factory.createStatementFromText(commentTracker.text(expressions[i]) + ';', element); } } else { final PsiStatement updateStatement = factory.createStatementFromText(commentTracker.markUnchanged(update).getText() + ';', element); updateStatements = new PsiStatement[]{updateStatement}; } final Collection<PsiContinueStatement> continueStatements = PsiTreeUtil.findChildrenOfType(loopBody, PsiContinueStatement.class); for (PsiContinueStatement continueStatement : continueStatements) { BlockUtils.addBefore(continueStatement, updateStatements); } for (PsiStatement updateStatement : updateStatements) { loopBody.addBefore(updateStatement, loopBody.getLastChild()); } } if (initialization == null || initialization instanceof PsiEmptyStatement) { commentTracker.replaceAndRestoreComments(forStatement, whileStatement); } else { initialization = (PsiStatement)commentTracker.markUnchanged(initialization).copy(); PsiStatement newStatement = (PsiStatement)commentTracker.replaceAndRestoreComments(forStatement, whileStatement); if (initialization instanceof PsiDeclarationStatement) { JavaCodeStyleManager manager = JavaCodeStyleManager.getInstance(newStatement.getProject()); if (hasConflictingName((PsiDeclarationStatement)initialization, newStatement, manager)) { PsiBlockStatement emptyBlockStatement = (PsiBlockStatement)factory.createStatementFromText("{}", newStatement); emptyBlockStatement.getCodeBlock().add(newStatement); newStatement = ((PsiBlockStatement)newStatement.replace(emptyBlockStatement)).getCodeBlock().getStatements()[0]; } } BlockUtils.addBefore(newStatement, initialization); } }