PsiInstanceOfExpression (@Nullable PsiExpression condition, @NotNull PsiTypeCastExpression cast, boolean whenTrue) { if (condition == null) return null; if (condition instanceof PsiParenthesizedExpression) { return findInstanceOf(((PsiParenthesizedExpression)condition).getExpression(), cast, whenTrue); } if (BoolUtils.isNegation(condition)) { return findInstanceOf(BoolUtils.getNegated(condition), cast, !whenTrue); } if (condition instanceof PsiPolyadicExpression polyadic) { IElementType tokenType = polyadic.getOperationTokenType(); if (tokenType == JavaTokenType.ANDAND && whenTrue || tokenType == JavaTokenType.OROR && !whenTrue) { for (PsiExpression operand : polyadic.getOperands()) { if (PsiTreeUtil.isAncestor(operand, cast, false)) return null; PsiInstanceOfExpression result = findInstanceOf(operand, cast, whenTrue); if (result != null) { return result; } } } } if (condition instanceof PsiInstanceOfExpression instanceOf && whenTrue) { PsiTypeElement typeElement = findCheckTypeElement(instanceOf); if (typeElement != null) { PsiType type = typeElement.getType(); PsiType castType = Objects.requireNonNull(cast.getCastType()).getType(); PsiExpression castOperand = Objects.requireNonNull(cast.getOperand()); if (typeCompatible(type, castType, castOperand) && PsiEquivalenceUtil.areElementsEquivalent(instanceOf.getOperand(), castOperand)) { return instanceOf; } } } return null; }