PsiInstanceOfExpression (PsiFile file, int caretOffset) { PsiElement element = file.findElementAt(caretOffset); if (element == null) return null; PsiInstanceOfExpression expression = PsiTreeUtil.getParentOfType(element, PsiInstanceOfExpression.class); if (expression != null) { return expression; } if (caretOffset > 0) { PsiInstanceOfExpression prev = PsiTreeUtil.getParentOfType(file.findElementAt(caretOffset - 1), PsiInstanceOfExpression.class); if (prev != null) { return prev; } } PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiIfStatement.class, PsiWhileStatement.class); if (statement instanceof PsiIfStatement) { final PsiExpression condition = ((PsiIfStatement)statement).getCondition(); return retrieveInstanceOfFromCondition(element, statement, condition); } else if (statement instanceof PsiWhileStatement) { final PsiExpression condition = ((PsiWhileStatement)statement).getCondition(); return retrieveInstanceOfFromCondition(element, statement, condition); } return null; }