Check if PsiType is a generic type


For example in 

public String<T> getId(T obj)

How do I check if  `T` is a generic type (and not just a class named T)?

Or in a more complicated scenario: 

public String<T> getId(T[] obj)

Check if T[] is an array of generics, and not an array of normal classes.

Comment actions Permalink

Did you ever find a solution for this?

Comment actions Permalink


Get PsiTypeElement from the method parameter list and its PsiTypeElement.getInnermostComponentReferenceElement(). Then call PsiJavaCodeReferenceElement.resolve() and check if the resolved element is of PsiTypeParameter type.

Example for your case:

PsiParameter parameter = PsiMethod.getParametersList().getParameter(n); // where n - index of the checked parameter
PsiTypeElement typeElement = parameter.getTypeElement();
PsiJavaCodeReferenceElement = referenceElement.getInnermostComponentReferenceElement();
PsiElement resolved = referenceElement.resolve();
boolean isGeneric = resolved instanceof PsiTypeParameter;


Please sign in to leave a comment.