PsiClass get PsiClassType vs PsiClassType resolve to PsiClass


Let's say I want to validate if the PsiMethod returns the parent PsiClass, i.e. check if the the PsiMethod is a proper setter of the builder PsiClass.


class Builder<A,B,C>{


Builder<A,B,C> set0(...)... // my validator should say: good method

String set1(...)... // my validator should say: bad method

Builder<Float, Float, Float> set2(...)... // my validator should say: bad method

<A,B,C> Builder<A, B, C> set3(...)... // my validator should say: bad method



In such a check is it better to start with the `psiMethod.getReturnType()`, then try to `resolve()` / `resolveGenerics()` to the PsiClass, and finally compare it with  `psiMethod.getParent()` PsiClass + generics somehow; OR start with `psiMethod.getParent()` to get parent PsiClass and try something like `PsiTypesUtil.getClassType` to get PsiType and somehow compare it with `psiMethod.getReturnType()` ?

1 comment

To me, the first variant seems more intuitive. You can use com.intellij.psi.PsiJvmMember#getContainingClass to obtain the method's "parent" class.


Please sign in to leave a comment.