Search code examples
kotlingenericsintellij-ideaintellij-pluginintellij-platform-psi

How to get generic type of PsiField?


Making an IDEA Plugin and stuck with this thing. Here is the class I have:

open class Class1<A, B : Class2> {
    open var value1: Map<String, A>? = null
    open var value2: Map<String, B> = emptyMap()
}

In my plugin I need to know somehow the real types of variables value1 and value2 Is that possible with PSI library?

So I have PsiClassReferenceType object, it's canonicalText method returns java.util.Map<java.lang.String,? extends B>.

psiType.reference.typeParameters returns two parameters:

  1. PsiClassReferenceType - this is for String parameter
  2. PsiWildcardType - this is for generic value parameter (having canonicalText as ? extends B)

Also tried to use PsiUtil.resolveGenericsClassInType(psiType).substitutor.substitutionMap, but this still brings me ? extends B and nothing more.

Any suggestions of how to get actual type parameter (which im my case is Class2)?


Solution

  • Had to admit that there is no way to achieve desired result by just a couple of utility methods. So the best way I can find is to get the type parameters of the field and then parse class name to extract real types by the names of generics.