void (@NotNull PsiSwitchStatement switchStatement) { final PsiExpression switchExpression = switchStatement.getExpression(); if (switchExpression == null) { return; } final PsiType switchExpressionType = CommonJavaRefactoringUtil.getTypeByExpressionWithExpectedType(switchExpression); if (switchExpressionType == null) { return; } CommentTracker commentTracker = new CommentTracker(); PsiCodeBlock body = switchStatement.getBody(); if (body == null) { return; } // Should execute getFallThroughTargets and statementMayCompleteNormally before converting breaks Set<PsiSwitchLabelStatementBase> fallThroughTargets = getFallThroughTargets(body); boolean mayCompleteNormally = ControlFlowUtils.statementMayCompleteNormally(switchStatement); BreakConverter converter = BreakConverter.from(switchStatement); if (converter == null) return; converter.process(); final List<SwitchStatementBranch> allBranches = extractBranches(body, fallThroughTargets); boolean needNullCheck = false; final boolean isSwitchOnString = switchExpressionType.equalsToText(CommonClassNames.JAVA_LANG_STRING); boolean useEquals = isSwitchOnString; if (!useEquals) { final PsiClass aClass = PsiUtil.resolveClassInType(switchExpressionType); if (aClass != null) { String fqn = aClass.getQualifiedName(); useEquals = !aClass.isEnum() && (fqn == null || !TypeConversionUtil.isPrimitiveWrapper(fqn)); Nullability nullability = NullabilityUtil.getExpressionNullability(switchExpression, true); needNullCheck = nullability != Nullability.NOT_NULL && (fqn == null || !TypeConversionUtil.isPrimitiveWrapper(fqn)) && !hasNullCase(allBranches); } } final String declarationString; final boolean hadSideEffects; final String expressionText; final Project project = switchStatement.getProject(); int totalCases = allBranches.stream().mapToInt(br -> br.getCaseElements().size()).sum(); List<PsiExpression> sideEffectExpressions = SideEffectChecker.extractSideEffectExpressions(switchExpression); if (totalCases > 0) { commentTracker.markUnchanged(switchExpression); } if (totalCases > 1 && !sideEffectExpressions.isEmpty()) { hadSideEffects = true; final String variableName = new VariableNameGenerator(switchExpression, VariableKind.LOCAL_VARIABLE) .byExpression(switchExpression).byType(switchExpressionType).byName(isSwitchOnString ? "s" : "i").generate(true); expressionText = variableName; declarationString = switchExpressionType.getCanonicalText() + ' ' + variableName + " = " + switchExpression.getText() + ';'; } else { hadSideEffects = totalCases == 0 && !sideEffectExpressions.isEmpty(); declarationString = null; int exprPrecedence = useEquals ? ParenthesesUtils.METHOD_CALL_PRECEDENCE : ParenthesesUtils.EQUALITY_PRECEDENCE; expressionText = ParenthesesUtils.getPrecedence(switchExpression) > exprPrecedence ? '(' + switchExpression.getText() + ')' : switchExpression.getText(); } final StringBuilder ifStatementBuilder = new StringBuilder(); boolean firstBranch = true; SwitchStatementBranch defaultBranch = null; boolean java7plus = PsiUtil.getLanguageLevel(switchStatement).isAtLeast(LanguageLevel.JDK_1_7); for (SwitchStatementBranch branch : allBranches) { if (branch.isDefault()) { defaultBranch = branch; } else { dumpBranch(branch, expressionText, firstBranch, useEquals, needNullCheck && java7plus, ifStatementBuilder, commentTracker); firstBranch = false; } } if (needNullCheck && !java7plus) { if (!firstBranch) { ifStatementBuilder.append("else "); } ifStatementBuilder.append("if(").append(expressionText).append("==null) { throw new NullPointerException(); }"); } boolean unwrapDefault = false; if (defaultBranch != null) { unwrapDefault = defaultBranch.isAlwaysExecuted() || (switchStatement.getParent() instanceof PsiCodeBlock && !mayCompleteNormally); if (!unwrapDefault && defaultBranch.hasStatements()) { ifStatementBuilder.append("else "); dumpBody(defaultBranch, ifStatementBuilder, commentTracker); } } String ifStatementText = ifStatementBuilder.toString(); if (ifStatementText.isEmpty()) { if (!unwrapDefault) return; ifStatementText = ";"; } final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiCodeBlock parent = ObjectUtils.tryCast(switchStatement.getParent(), PsiCodeBlock.class); if (unwrapDefault || hadSideEffects) { if (parent == null) { commentTracker.grabComments(switchStatement); switchStatement = BlockUtils.expandSingleStatementToBlockStatement(switchStatement); parent = (PsiCodeBlock)(switchStatement.getParent()); } } JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project); if (hadSideEffects) { if (declarationString == null) { sideEffectExpressions.forEach(commentTracker::markUnchanged); PsiStatement[] statements = StatementExtractor.generateStatements(sideEffectExpressions, switchExpression); for (PsiStatement statement : statements) { javaCodeStyleManager.shortenClassReferences(parent.addBefore(statement, switchStatement)); } } else { final PsiStatement declarationStatement = factory.createStatementFromText(declarationString, switchStatement); javaCodeStyleManager.shortenClassReferences(parent.addBefore(declarationStatement, switchStatement)); } } final PsiStatement ifStatement = factory.createStatementFromText(ifStatementText, switchStatement); if (unwrapDefault) { PsiElement addedIf = parent.addBefore(ifStatement, switchStatement); StringBuilder sb = new StringBuilder(); dumpBody(defaultBranch, sb, commentTracker); PsiBlockStatement defaultBody = (PsiBlockStatement)factory.createStatementFromText(sb.toString(), switchStatement); if (!BlockUtils.containsConflictingDeclarations(Objects.requireNonNull(switchStatement.getBody()), parent)) { commentTracker.grabComments(switchStatement); BlockUtils.inlineCodeBlock(switchStatement, defaultBody.getCodeBlock()); } else { commentTracker.replace(switchStatement, defaultBody); } commentTracker.insertCommentsBefore(addedIf); if (ifStatementText.equals(";")) { addedIf.delete(); } else { javaCodeStyleManager.shortenClassReferences(addedIf); } } else { javaCodeStyleManager.shortenClassReferences(commentTracker.replaceAndRestoreComments(switchStatement, ifStatement)); } }