StreamModel (PsiLocalVariable var) { PsiElement block = PsiUtil.getVariableCodeBlock(var, null); if (block == null) return null; List<PsiReferenceExpression> references = VariableAccessUtils.getVariableReferences(var, block); List<PsiReferenceExpression> writes = ContainerUtil.filter(references, PsiUtil::isAccessedForWriting); if (writes.isEmpty()) return null; PsiElement commonParent = PsiTreeUtil.findCommonParent(writes); if (commonParent == null) return null; while (commonParent.getParent() != block) { commonParent = commonParent.getParent(); if (commonParent == null) return null; } if (!(commonParent instanceof PsiLoopStatement statement)) return null; final PsiStatement body = statement.getBody(); if (body == null) return null; StreamApiMigrationInspection.StreamSource source = StreamApiMigrationInspection.StreamSource.tryCreate(statement); if (source == null) return null; if (!ExceptionUtil.getThrownCheckedExceptions(body).isEmpty()) return null; TerminalBlock tb = TerminalBlock.from(source, body); BaseStreamApiMigration migration = StreamApiMigrationInspection.findMigration(statement, body, tb, false, false); if (migration == null) return null; ControlFlowUtils.InitializerUsageStatus status = ControlFlowUtils.getInitializerUsageStatus(var, statement); if (status != ControlFlowUtils.InitializerUsageStatus.DECLARED_JUST_BEFORE && status != ControlFlowUtils.InitializerUsageStatus.AT_WANTED_PLACE_ONLY) { return null; } return new StreamModel(migration, body, tb); }