TernaryNullCheck (@Nullable PsiConditionalExpression ternary) { if (ternary == null) return null; PsiExpression condition = ternary.getCondition(); boolean isNull = true; PsiReferenceExpression ref = ExpressionUtils.getReferenceExpressionFromNullComparison(condition, true); if (ref == null) { isNull = false; ref = ExpressionUtils.getReferenceExpressionFromNullComparison(condition, false); } if (ref == null) return null; PsiVariable variable = tryCast(ref.resolve(), PsiVariable.class); if (variable == null || (variable instanceof PsiField && !ExpressionUtil.isEffectivelyUnqualified(ref))) return null; PsiExpression nullBranch = isNull ? ternary.getThenExpression() : ternary.getElseExpression(); PsiExpression notNullBranch = isNull ? ternary.getElseExpression() : ternary.getThenExpression(); if (nullBranch == null || notNullBranch == null) { return null; } return new TernaryNullCheck(variable, nullBranch, notNullBranch); }