PsiTreeUtil.getParentOfType() returns null

509 views Asked by At

I am trying to get parent Method of a PsiElement. I get null by using the PsiTreeUtil.getParentOfType method.

PsiMethod m = PsiTreeUtil.getParentOfType(psiElement, PsiClass.class);

But it works when I iterate through the tree :

PsiMethod m;
while(true){
    psiElement = psiElement.getParent();
    try{
        m = (PsiMethod)psiElement ;
        break;
    }
    catch(ClassCastException e)
    {
    }
}

Can somebody explain me why PsiTreeUtil.getParentOfType gives null while by iterating it doesn't give null and I get required result?

1

There are 1 answers

1
mohanpb On BEST ANSWER

I used this and it works now.

PsiMethod s = PsiTreeUtil.getParentOfType(x, PsiMethod.class, false);