List<PsiExpression> (PsiLoopStatement loop) { if (loop instanceof PsiForeachStatement foreachStatement) { PsiExpression expression = ExpressionUtils.resolveExpression(foreachStatement.getIteratedValue()); expression = PsiUtil.skipParenthesizedExprDown(expression); if (expression instanceof PsiArrayInitializerExpression arrayInitializer) { return Arrays.asList(arrayInitializer.getInitializers()); } if (expression instanceof PsiNewExpression newExpression) { PsiArrayInitializerExpression initializer = newExpression.getArrayInitializer(); return initializer == null ? Collections.emptyList() : Arrays.asList(initializer.getInitializers()); } if (expression instanceof PsiMethodCallExpression call) { if (Holder.SINGLETON_CONSTRUCTOR.test(call)) { return Arrays.asList(call.getArgumentList().getExpressions()); } if (Holder.LIST_CONSTRUCTOR.test(call)) { PsiExpression[] args = call.getArgumentList().getExpressions(); if (args.length > 1 || MethodCallUtils.isVarArgCall(call)) { return Arrays.asList(args); } } if (CallMatcher.enumValues().test(call)) { PsiType type = call.getType(); if (type instanceof PsiArrayType arrayType) { PsiClass enumClass = PsiUtil.resolveClassInClassTypeOnly(arrayType.getComponentType()); if (enumClass != null && enumClass.isEnum()) { List<PsiEnumConstant> constants = ContainerUtil.filterIsInstance(enumClass.getFields(), PsiEnumConstant.class); return generatedList(loop, constants.size(), index -> enumClass.getQualifiedName() + "." + constants.get(index).getName()); } } } } if (ExpressionUtils.isSafelyRecomputableExpression(expression)) { PsiType type = expression.getType(); if (type instanceof PsiArrayType) { DfType dfType = CommonDataflow.getDfType(expression); Integer arraySize = SpecialField.ARRAY_LENGTH.getFromQualifier(dfType).getConstantOfType(Integer.class); if (arraySize != null) { PsiExpression array = expression; return generatedList(loop, arraySize, index -> ParenthesesUtils.getText(array, PsiPrecedenceUtil.POSTFIX_PRECEDENCE) +"["+index+"]"); } } if (InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_LIST)) { DfType dfType = CommonDataflow.getDfType(expression); Integer listSize = SpecialField.COLLECTION_SIZE.getFromQualifier(dfType).getConstantOfType(Integer.class); if (listSize != null) { PsiExpression list = expression; return generatedList(loop, listSize, index -> ParenthesesUtils.getText(list, PsiPrecedenceUtil.METHOD_CALL_PRECEDENCE) +".get("+index+")"); } } } } if (loop instanceof PsiForStatement forStatement) { CountingLoop countingLoop = CountingLoop.from(forStatement); if (countingLoop != null) { boolean descending = countingLoop.isDescending(); long multiplier = descending ? -1 : 1; Object from = CommonDataflow.computeValue(countingLoop.getInitializer()); if (!(from instanceof Integer) && !(from instanceof Long)) return Collections.emptyList(); long fromValue = ((Number)from).longValue(); Object to = CommonDataflow.computeValue(countingLoop.getBound()); if (!(to instanceof Integer) && !(to instanceof Long)) return Collections.emptyList(); long toValue = ((Number)to).longValue(); long diff = multiplier * (toValue - fromValue); String suffix = PsiTypes.longType().equals(countingLoop.getCounter().getType()) ? "L" : ""; if (countingLoop.isIncluding()) { diff++; // overflow is ok: diff will become negative and we will exit } if (diff < 0 || diff > MAX_BODY_SIZE_ESTIMATE) return Collections.emptyList(); int size = (int)(diff); // Less or equal to MAX_ITERATIONS => fits to int return generatedList(loop, size, index -> (fromValue + multiplier * index) + suffix); } } return Collections.emptyList(); }