void (@NotNull PsiElement element) { final PsiForeachStatement statement = (PsiForeachStatement)element.getParent(); if (statement == null) { return; } final PsiExpression iteratedValue = statement.getIteratedValue(); if (iteratedValue == null) { return; } final PsiParameter iterationParameter = statement.getIterationParameter(); final PsiType type = iterationParameter.getType(); final PsiType iteratedValueType = iteratedValue.getType(); if (iteratedValueType == null) { return; } final CommentTracker tracker = new CommentTracker(); final boolean isArray = iteratedValueType instanceof PsiArrayType; final PsiElement parent = statement.getParent(); PsiStatement context = (parent instanceof PsiLabeledStatement) ? (PsiStatement)parent : statement; final PsiElement reference = getReferenceToIterate(iteratedValue, context); @NonNls final StringBuilder newStatement = new StringBuilder(); final String indexText = createVariableName("i", PsiTypes.intType(), statement); final String iteratedValueText = (reference instanceof PsiVariable) ? ((PsiVariable)reference).getName() : tracker.text(reference); createForLoopDeclaration(statement, isArray, iteratedValueText, indexText, newStatement); if (JavaCodeStyleSettings.getInstance(statement.getContainingFile()).GENERATE_FINAL_LOCALS) { newStatement.append("final "); } PsiTypeElement typeElement = iterationParameter.getTypeElement(); newStatement.append(typeElement != null && typeElement.isInferredType() ? PsiKeyword.VAR : type.getCanonicalText()); newStatement.append(' '); newStatement.append(iterationParameter.getName()); newStatement.append('='); newStatement.append(iteratedValueText); if (isArray) { newStatement.append('['); newStatement.append(indexText); newStatement.append("];"); } else { newStatement.append(".get("); newStatement.append(indexText); newStatement.append(");"); } final PsiStatement body = statement.getBody(); if (body == null) { return; } if (body instanceof PsiBlockStatement) { final PsiCodeBlock block = ((PsiBlockStatement)body).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 { newStatement.append(tracker.text(body)); } newStatement.append('}