PsiElement (@NotNull PsiJavaCodeReferenceElement ref) { PsiElement candidate = ref; while (candidate instanceof PsiJavaCodeReferenceElement) { if (((PsiJavaCodeReferenceElement)candidate).resolve() instanceof PsiPackage) return candidate; candidate = ((PsiJavaCodeReferenceElement)candidate).getQualifier(); } return ref; }