void (@NotNull PsiForStatement forStatement) { final PsiVariable indexVariable = getIndexVariable(forStatement); if (indexVariable == null) { return; } PsiExpression collectionSize = getMaximum(forStatement); if (collectionSize instanceof PsiReferenceExpression referenceExpression) { final PsiElement target = referenceExpression.resolve(); if (target instanceof PsiVariable variable) { collectionSize = PsiUtil.skipParenthesizedExprDown(variable.getInitializer()); } } if (!(collectionSize instanceof PsiMethodCallExpression)) { return; } final PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)PsiUtil.skipParenthesizedExprDown(collectionSize); if (methodCallExpression == null) { return; } final PsiReferenceExpression listLengthExpression = methodCallExpression.getMethodExpression(); final PsiExpression qualifier = PsiUtil.skipParenthesizedExprDown(ExpressionUtils.getEffectiveQualifier(listLengthExpression)); if (qualifier == null) { return; } final PsiReferenceExpression listReference; if (qualifier instanceof PsiReferenceExpression) { listReference = (PsiReferenceExpression)qualifier; } else { listReference = null; } final PsiType type = qualifier.getType(); if (type == null) { return; } PsiType parameterType = getContentType(type, CommonClassNames.JAVA_UTIL_COLLECTION); if (parameterType == null) { parameterType = TypeUtils.getObjectType(forStatement); } final PsiVariable listVariable; if (listReference == null) { listVariable = null; } else { final PsiElement target = listReference.resolve(); if (!(target instanceof PsiVariable)) { return; } listVariable = (PsiVariable)target; } final PsiStatement body = forStatement.getBody(); final PsiStatement firstStatement = getFirstStatement(body); final boolean isDeclaration = isListElementDeclaration(firstStatement, listVariable, indexVariable); final String contentVariableName; @NonNls final String finalString; final PsiStatement statementToSkip; if (isDeclaration) { final PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)firstStatement; final PsiElement[] declaredElements = declarationStatement.getDeclaredElements(); final PsiElement declaredElement = declaredElements[0]; if (!(declaredElement instanceof PsiVariable variable)) { return; } contentVariableName = variable.getName(); parameterType = variable.getType(); statementToSkip = declarationStatement; if (variable.hasModifierProperty(PsiModifier.FINAL)) { finalString = "final "; } else { finalString = ""; } } else { final String collectionName; if (listReference == null) { collectionName = null; } else { collectionName = listReference.getReferenceName(); } contentVariableName = createNewVariableName(forStatement, parameterType, collectionName); finalString = ""; statementToSkip = null; } final CommentTracker ct = new CommentTracker(); @NonNls final StringBuilder newStatement = new StringBuilder("for("); newStatement.append(finalString).append(parameterType.getCanonicalText()).append(' ').append(contentVariableName).append(": "); @NonNls final String listName; if (listReference == null) { listName = ct.text(qualifier); } else { listName = getVariableReferenceText(listReference, listVariable, forStatement); } newStatement.append(listName).append(')'); if (body != null) { replaceCollectionGetAccess(body, contentVariableName, listVariable, indexVariable, statementToSkip, ct, newStatement); } ct.replaceAndRestoreComments(forStatement, newStatement.toString()); }