PsiLocalVariable (TerminalBlock tb) { CountingLoopSource loop = tb.getLastOperation(CountingLoopSource.class); if (loop == null || loop.myIncluding) return null; PsiAssignmentExpression assignment = tb.getSingleExpression(PsiAssignmentExpression.class); if (assignment == null || !assignment.getOperationTokenType().equals(JavaTokenType.EQ)) return null; PsiArrayAccessExpression arrayAccess = tryCast(assignment.getLExpression(), PsiArrayAccessExpression.class); if (arrayAccess == null) return null; if (!ExpressionUtils.isReferenceTo(arrayAccess.getIndexExpression(), loop.getVariable())) return null; PsiReferenceExpression arrayReference = tryCast(arrayAccess.getArrayExpression(), PsiReferenceExpression.class); if (arrayReference == null) return null; PsiLocalVariable arrayVariable = tryCast(arrayReference.resolve(), PsiLocalVariable.class); if (arrayVariable == null || ControlFlowUtils.getInitializerUsageStatus(arrayVariable, tb.getStreamSourceStatement()) == UNKNOWN) { return null; } PsiNewExpression initializer = tryCast(arrayVariable.getInitializer(), PsiNewExpression.class); if (initializer == null) return null; PsiArrayType arrayType = tryCast(initializer.getType(), PsiArrayType.class); if (arrayType == null || !StreamApiUtil.isSupportedStreamElement(arrayType.getComponentType())) return null; PsiExpression dimension = ArrayUtil.getFirstElement(initializer.getArrayDimensions()); if (dimension == null) return null; PsiExpression bound = PsiUtil.skipParenthesizedExprDown(loop.myBound); if (!isArrayLength(arrayVariable, dimension, bound)) return null; if (VariableAccessUtils.variableIsUsed(arrayVariable, assignment.getRExpression())) return null; return arrayVariable; }