String (ConditionalExpression conditionalExpression) { PsiStatement statement = PsiTreeUtil.getParentOfType(myChainExpression, PsiStatement.class); boolean inReturn = statement instanceof PsiReturnStatement; if (conditionalExpression instanceof ConditionalExpression.Optional) { conditionalExpression = tryUnwrapOptional((ConditionalExpression.Optional)conditionalExpression, inReturn); } if (conditionalExpression instanceof ConditionalExpression.Boolean) { conditionalExpression = tryUnwrapBoolean((ConditionalExpression.Boolean)conditionalExpression, inReturn); } if (inReturn) { setFinisher(conditionalExpression.getFalseBranch()); Object mark = new Object(); PsiTreeUtil.mark(myChainExpression, mark); PsiElement returnCopy = statement.copy(); PsiElement placeHolderCopy = PsiTreeUtil.releaseMark(returnCopy, mark); LOG.assertTrue(placeHolderCopy != null); PsiElement replacement = placeHolderCopy.replace(createExpression(conditionalExpression.getTrueBranch())); if (returnCopy == placeHolderCopy) { returnCopy = replacement; } String text = returnCopy.getText(); if (returnCopy.getLastChild() instanceof PsiComment) { text += "\n"; } return text; } PsiElement parent = PsiUtil.skipParenthesizedExprUp(myChainExpression.getParent()); if (parent instanceof PsiIfStatement ifStatement && ifStatement.getElseBranch() == null && conditionalExpression instanceof ConditionalExpression.Boolean boolCondition && !boolCondition.isInverted()) { PsiStatement thenStatement = ControlFlowUtils.stripBraces(ifStatement.getThenBranch()); if (thenStatement instanceof PsiReturnStatement || thenStatement instanceof PsiThrowStatement) { myChainExpression = parent; return thenStatement.getText(); } if (thenStatement instanceof PsiExpressionStatement) { myChainExpression = parent; return thenStatement.getText() + "\n" + getBreakStatement(); } } if (conditionalExpression instanceof ConditionalExpression.Optional && myChainExpression instanceof PsiExpression) { PsiMethodCallExpression call = ExpressionUtils.getCallForQualifier((PsiExpression)myChainExpression); if (call != null && call.getParent() instanceof PsiExpressionStatement) { PsiExpression[] args = call.getArgumentList().getExpressions(); if (args.length == 1 && "ifPresent".equals(call.getMethodExpression().getReferenceName())) { FunctionHelper fn = FunctionHelper.create(args[0], 1); if (fn != null) { fn.transform(this, ((ConditionalExpression.Optional)conditionalExpression).unwrap("").getTrueBranch()); myChainExpression = call.getParent(); return fn.getStatementText() + getBreakStatement(); } } } } String found = declareResult(conditionalExpression.getCondition(), createType(conditionalExpression.getType()), conditionalExpression.getFalseBranch(), ResultKind.NON_FINAL); return found + " = " + conditionalExpression.getTrueBranch() + ";\n" + getBreakStatement(); }