PsiConditionalExpression (@NotNull PsiExpression expression) { PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent()); if (!(parent instanceof PsiConditionalExpression conditional)) return null; PsiType type = conditional.getType(); if (type == null) return null; PsiExpression thenExpression = conditional.getThenExpression(); if (thenExpression == null) return null; PsiType thenType = thenExpression.getType(); if (thenType == null) return null; PsiExpression elseExpression = conditional.getElseExpression(); if (elseExpression == null) return null; PsiType elseType = elseExpression.getType(); if (elseType == null) return null; if (elseType instanceof PsiPrimitiveType && thenType instanceof PsiClassType) { if (PsiTreeUtil.isAncestor(thenExpression, expression, false) && thenType.isAssignableFrom(elseType)) return conditional; } else if (elseType instanceof PsiClassType && thenType instanceof PsiPrimitiveType) { if (PsiTreeUtil.isAncestor(elseExpression, expression, false) && elseType.isAssignableFrom(thenType)) return conditional; } return null; }