void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { final PsiForeachStatement statement = (PsiForeachStatement)element.getParent(); assert statement != null; final PsiExpression iteratedValue = statement.getIteratedValue(); if (iteratedValue == null) { return; } CommentTracker tracker = new CommentTracker(); @NonNls final StringBuilder newStatement = new StringBuilder(); final PsiParameter iterationParameter = statement.getIterationParameter(); boolean generateFinalLocals = JavaFileCodeStyleFacade.forContext(element.getContainingFile()).isGenerateFinalLocals(); tracker.markUnchanged(iteratedValue); if (iteratedValue.getType() instanceof PsiArrayType) { final PsiType type = iterationParameter.getType(); final String index = new VariableNameGenerator(statement, VariableKind.LOCAL_VARIABLE) .byType(PsiTypes.intType()).byName("i", "j", "k").generate(true); newStatement.append("for(int ").append(index).append(" = 0;"); newStatement.append(index).append('<').append(iteratedValue.getText()).append(".length;"); newStatement.append(index).append("++)").append("{ "); if (generateFinalLocals) { newStatement.append("final "); } newStatement.append(type.getCanonicalText()).append(' ').append(iterationParameter.getName()); newStatement.append(" = ").append(iteratedValue.getText()).append('[').append(index).append("];"); } else { @NonNls final StringBuilder methodCall = new StringBuilder(); if (ParenthesesUtils.getPrecedence(iteratedValue) > ParenthesesUtils.METHOD_CALL_PRECEDENCE) { methodCall.append('(').append(iteratedValue.getText()).append(')'); } else { methodCall.append(iteratedValue.getText()); } methodCall.append(".iterator()"); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiExpression iteratorCall = factory.createExpressionFromText(methodCall.toString(), iteratedValue); final PsiType variableType = GenericsUtil.getVariableTypeByExpressionType(iteratorCall.getType()); if (variableType == null) { return; } final PsiType parameterType = iterationParameter.getType(); final String typeText = parameterType.getCanonicalText(); newStatement.append("for(").append(variableType.getCanonicalText()).append(' '); final String iterator = new VariableNameGenerator(statement, VariableKind.LOCAL_VARIABLE) .byName("iterator", "iter", "itr").generate(true); newStatement.append(iterator).append("=").append(iteratorCall.getText()).append(';'); newStatement.append(iterator).append(".hasNext();){"); if (generateFinalLocals) { newStatement.append("final "); } newStatement.append(typeText).append(' ').append(iterationParameter.getName()).append(" = ").append(iterator).append(".next();"); } final PsiStatement body = statement.getBody(); if (body instanceof PsiBlockStatement blockStatement) { final PsiCodeBlock block = blockStatement.getCodeBlock(); final PsiElement[] children = block.getChildren(); for (int i = 1; i < children.length - 1; i++) { //skip the braces newStatement.append(tracker.text(children[i])); } } else { final String bodyText; if (body == null) { bodyText = ""; } else { bodyText = tracker.text(body); } newStatement.append(bodyText); } newStatement.append('}'); PsiReplacementUtil.replaceStatementAndShortenClassNames(statement, newStatement.toString(), tracker); } }