PsiTypeElement (@NotNull PsiJavaCodeReferenceElement ref) { PsiElement root = SyntaxTraverser.psiApi() .parents(ref.getParent()) .takeWhile(it -> it instanceof PsiTypeElement || it instanceof PsiReferenceParameterList || it instanceof PsiJavaCodeReferenceElement) .last(); return ObjectUtils.tryCast(root, PsiTypeElement.class); }