PsiSwitchStatement (@NotNull PsiVariable variable, @NotNull PsiElement topBlock, @NotNull PsiElement readPoint) { PsiSwitchStatement switchForAll = null; for (PsiReferenceExpression reference : VariableAccessUtils.getVariableReferences(variable, topBlock)) { if (PsiUtil.isAccessedForWriting(reference)) { PsiSwitchStatement switchStatement = PsiTreeUtil.getParentOfType(reference, PsiSwitchStatement.class); if (switchStatement == null || !PsiTreeUtil.isAncestor(topBlock, switchStatement, true)) return null; if (switchForAll != null) { if (switchForAll != switchStatement) return null; } else { switchForAll = switchStatement; } } } if (switchForAll == null) return null; if (SwitchUtils.calculateBranchCount(switchForAll) < 0) return null; List<PsiSwitchLabelStatementBase> labels = PsiTreeUtil.getChildrenOfTypeAsList(switchForAll.getBody(), PsiSwitchLabelStatementBase.class); LocalsOrMyInstanceFieldsControlFlowPolicy policy = LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance(); ControlFlow controlFlow; try { controlFlow = ControlFlowFactory.getInstance(topBlock.getProject()).getControlFlow(topBlock, policy); } catch (AnalysisCanceledException e) { return null; } int switchOffset = controlFlow.getStartOffset(switchForAll); int readOffset = controlFlow.getStartOffset(readPoint); if (switchOffset == -1 || readOffset == -1 || !ControlFlowUtil.isDominator(controlFlow, switchOffset, readOffset)) return null; boolean[] offsets = ControlFlowUtil.getVariablePossiblyUnassignedOffsets(variable, controlFlow); for (PsiSwitchLabelStatementBase label : labels) { int offset = controlFlow.getStartOffset(label); if (offset == -1 || offsets[offset]) return null; } return switchForAll; }