void (@NotNull Project project, @NotNull PsiElement element, @NotNull ModPsiUpdater updater) { if (!(element instanceof PsiConditionalExpression expression)) { return; } CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(expression); if (surrounder == null) return; CodeBlockSurrounder.SurroundResult result = surrounder.surround(); expression = (PsiConditionalExpression)result.getExpression(); PsiStatement statement = result.getAnchor(); final PsiVariable variable = statement instanceof PsiDeclarationStatement ? PsiTreeUtil.getParentOfType(expression, PsiVariable.class) : null; PsiExpression thenExpression = expression.getThenExpression(); PsiExpression elseExpression = expression.getElseExpression(); final PsiExpression condition = PsiUtil.skipParenthesizedExprDown(expression.getCondition()); CommentTracker tracker = new CommentTracker(); String ifText = "if(" + (condition == null ? "" : tracker.text(condition)) + ");\nelse;"; PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText(ifText, condition); if (variable != null) { final String name = variable.getName(); PsiExpression initializer = variable.getInitializer(); if (initializer == null) { return; } PsiTypeElement typeElement = variable.getTypeElement(); if (typeElement != null && typeElement.isInferredType() && PsiTypesUtil.replaceWithExplicitType(typeElement) == null) { return; } if (initializer instanceof PsiArrayInitializerExpression) { final int conditionIdx = ArrayUtilRt.find(((PsiArrayInitializerExpression)initializer).getInitializers(), expression); if (conditionIdx >= 0) { initializer = (PsiExpression)initializer.replace( CommonJavaRefactoringUtil.convertInitializerToNormalExpression(initializer, variable.getType())); final PsiArrayInitializerExpression arrayInitializer = ((PsiNewExpression)initializer).getArrayInitializer(); LOG.assertTrue(arrayInitializer != null, initializer.getText()); expression = (PsiConditionalExpression)arrayInitializer.getInitializers()[conditionIdx]; thenExpression = expression.getThenExpression(); elseExpression = expression.getElseExpression(); } } String thenAssignment = name + "=" + getReplacement(initializer, expression, thenExpression, tracker).getText() + ";"; String elseAssignment = name + "=" + getReplacement(initializer, expression, elseExpression, tracker).getText() + ";"; ifStatement.setThenBranch(factory.createStatementFromText(thenAssignment, initializer)); ifStatement.setElseBranch(factory.createStatementFromText(elseAssignment, initializer)); tracker.delete(initializer); final PsiElement parent = statement.getParent(); ifStatement = (PsiIfStatement)parent.addAfter(ifStatement, statement); tracker.insertCommentsBefore(ifStatement); } else { final boolean addBraces = PsiTreeUtil.getParentOfType(expression, PsiIfStatement.class, true, PsiStatement.class) != null; PsiStatement thenBranch = (PsiStatement)getReplacement(statement, expression, thenExpression, tracker); PsiStatement elseBranch = (PsiStatement)getReplacement(statement, expression, elseExpression, tracker); if (addBraces) { thenBranch = (PsiStatement)BlockUtils.expandSingleStatementToBlockStatement(thenBranch).getParent().getParent(); elseBranch = (PsiStatement)BlockUtils.expandSingleStatementToBlockStatement(elseBranch).getParent().getParent(); } ifStatement.setThenBranch(thenBranch); ifStatement.setElseBranch(elseBranch); ifStatement = (PsiIfStatement)tracker.replaceAndRestoreComments(statement, ifStatement); } final PsiStatement elseBranch = ifStatement.getElseBranch(); if (elseBranch instanceof PsiReturnStatement returnStatement) { final PsiExpression value = returnStatement.getReturnValue(); if (value instanceof PsiParenthesizedExpression parenthesizedExpression) { if (parenthesizedExpression.getExpression() == null) { elseBranch.delete(); } } } else if (elseBranch instanceof PsiExpressionStatement expressionStatement) { final PsiExpression statementExpression = expressionStatement.getExpression(); if (statementExpression instanceof PsiAssignmentExpression assignmentExpression) { final PsiExpression rhs = assignmentExpression.getRExpression(); if (rhs instanceof PsiParenthesizedExpression parenthesizedExpression) { if (parenthesizedExpression.getExpression() == null) { rhs.delete(); } } } } ifStatement = (PsiIfStatement)CodeStyleManager.getInstance(project).reformat( JavaCodeStyleManager.getInstance(project).shortenClassReferences(ifStatement)); if (!ControlFlowUtils.statementMayCompleteNormally(ifStatement.getThenBranch())) { if (!(ifStatement.getParent() instanceof PsiCodeBlock)) { ifStatement = BlockUtils.expandSingleStatementToBlockStatement(ifStatement); } final PsiStatement resultingElse = ifStatement.getElseBranch(); if (resultingElse != null) { ifStatement.getParent().addAfter(ControlFlowUtils.stripBraces(resultingElse), ifStatement); resultingElse.delete(); } } }