SwitchReplacer (@NotNull PsiStatement statement, @NotNull List<OldSwitchStatementBranch> branches) { for (int i = 0, size = branches.size(); i < size; i++) { OldSwitchStatementBranch branch = branches.get(i); if (!isConvertibleBranch(branch, i != size - 1) && //example: //case 0: break !(!branch.isFallthrough() && branch.getStatements().length == 0)) { return null; } } List<SwitchBranch> switchRules = new ArrayList<>(); for (int i = 0, branchesSize = branches.size(); i < branchesSize; i++) { OldSwitchStatementBranch branch = branches.get(i); if (branch.isFallthrough() && branch.getStatements().length == 0) continue; boolean allBranchRefsWillBeValid = StreamEx.of(branch.getStatements()) .limit(i) // only previous branches .flatMap((PsiElement stmt) -> StreamEx.ofTree(stmt, el -> StreamEx.of(el.getChildren()))) .select(PsiReferenceExpression.class) .map(PsiReference::resolve) .select(PsiLocalVariable.class) .allMatch(variable -> isInBranchOrOutside(statement, branch, variable)); if (!allBranchRefsWillBeValid) return null; if (branch.isFallthrough() && branch.getStatements().length == 0) continue; PsiStatement[] statements = branch.getStatements(); switchRules.add(SwitchBranch.fromOldBranch(branch, new SwitchStatementBranch(statements), branch.getRelatedStatements())); } return new SwitchStatementReplacer(statement, switchRules); }