TernaryNotNullContext (@NotNull PsiConditionalExpression ternary) { PsiBinaryExpression binOp = tryCast(PsiUtil.skipParenthesizedExprDown(ternary.getCondition()), PsiBinaryExpression.class); if(binOp == null) return null; PsiExpression value = ExpressionUtils.getValueComparedWithNull(binOp); PsiReferenceExpression referenceExpression = tryCast(PsiUtil.skipParenthesizedExprDown(value), PsiReferenceExpression.class); if(referenceExpression == null) return null; PsiVariable variable = tryCast(referenceExpression.resolve(), PsiVariable.class); if(variable == null) return null; boolean negated = binOp.getOperationTokenType() == JavaTokenType.NE; PsiExpression nullBranch = negated ? ternary.getElseExpression() : ternary.getThenExpression(); if(ClassUtils.isPrimitive(variable.getType())) return null; PsiExpression nonNullBranch = negated ? ternary.getThenExpression() : ternary.getElseExpression(); if(!ExpressionUtils.isReferenceTo(nonNullBranch, variable)) return null; if(NullabilityUtil.getExpressionNullability(nullBranch, true) != Nullability.NOT_NULL) return null; if(!LambdaGenerationUtil.canBeUncheckedLambda(nullBranch)) return null; return new TernaryNotNullContext(ternary, nullBranch, referenceExpression); }