SwitchReplacer (@NotNull PsiStatement statement, @NotNull List<OldSwitchStatementBranch> branches, boolean isExhaustive, int maxNumberStatementsForExpression) { PsiElement parent = statement.getParent(); PsiElement anchor = parent instanceof PsiLabeledStatement ? parent : statement; PsiLocalVariable assignedVariable = null; List<SwitchBranch> newBranches = new ArrayList<>(); boolean hasAssignedBranch = false; boolean wasDefault = false; boolean isInfo = false; int maxNumberStatementsInBranch = 0; for (int i = 0, size = branches.size(); i < size; i++) { OldSwitchStatementBranch branch = branches.get(i); if (!isConvertibleBranch(branch, i != size - 1)) return null; PsiStatement[] statements = branch.getStatements(); if (branch.isFallthrough() && statements.length == 0) continue; if (statements.length == 0) return null; if (ContainerUtil.exists(statements, st -> !PsiTreeUtil.findChildrenOfAnyType(st, PsiContinueStatement.class, PsiYieldStatement.class, PsiReturnStatement.class).isEmpty())) { return null; } if (ContainerUtil.exists(Arrays.stream(statements).toList().subList(0, statements.length - 1), st -> !PsiTreeUtil.findChildrenOfAnyType(st, PsiBreakStatement.class).isEmpty())) { return null; } if (statements.length > maxNumberStatementsForExpression) { isInfo = true; } if (maxNumberStatementsInBranch < statements.length) { maxNumberStatementsInBranch = statements.length; } PsiStatement last = statements[statements.length - 1]; PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(last); PsiExpression rExpression = null; if (assignment != null) { rExpression = assignment.getRExpression(); PsiLocalVariable var = ExpressionUtils.resolveLocalVariable(assignment.getLExpression()); if (var == null) return null; if (assignedVariable == null) { assignedVariable = var; } else if (assignedVariable != var) { return null; } } SwitchRuleResult result; if (rExpression == null) { PsiThrowStatement throwStatement = tryCast(last, PsiThrowStatement.class); if (throwStatement == null) return null; result = new SwitchStatementBranch(statements); } else { hasAssignedBranch = true; if (statements.length == 1) { result = new SwitchRuleExpressionResult(rExpression); } else { result = new SwitchStatementBranch(withLastStatementReplacedWithYield(statements, rExpression)); } } if (branch.isDefault()) { wasDefault = true; } else { wasDefault = existsDefaultLabelElement(branch.myLabelStatement); } newBranches.add(SwitchBranch.fromOldBranch(branch, result, branch.getRelatedStatements())); } if (assignedVariable == null || !hasAssignedBranch) return null; boolean isRightAfterDeclaration = isRightAfterDeclaration(anchor, assignedVariable); if (!wasDefault && !isExhaustive) { SwitchBranch defaultBranch = getVariableAssigningDefaultBranch(assignedVariable, isRightAfterDeclaration, statement); if (defaultBranch != null) { newBranches.add(defaultBranch); } else { return null; } } return new SwitchExistingVariableReplacer(assignedVariable, statement, newBranches, isRightAfterDeclaration, isInfo, maxNumberStatementsInBranch); }