This question already has an answer here:
- Finding type parameters via reflection in Scala 2.10? 2 answers
Suppose I get a Type representing List[Int]:
> import scala.reflect.runtime.universe
> val mirror = universe.runtimeMirror(this.getClass.getClassLoader)
mirror: reflect.runtime.universe.Mirror = JavaMirror with ...
> class X{ def getList():List[Int] = null }
defined class X
> val method = mirror.
classSymbol(classOf[X]).
toType.
declarations.
filter{_.isMethod}.
map{_.asInstanceOf[universe.MethodSymbol]}.
filter{ m => m.
name.
toString()=="getList" }.
head
method: reflect.runtime.universe.MethodSymbol = method getList
> val rt = method.returnType
rt: reflect.runtime.universe.Type = scala.List[scala.Int]
How to I go from reflect.runtime.universe.Type (that apparently knows about the type parameter scala.Int) to the actual type parameters?
I see that TypeSymbol has a parm
Some experiments..
rt.typeSymbol // reflect.runtime.universe.Symbol = class List ... Int is erased
rt.typeSymbol.asInstanceOf[TypeSymbol].typeParams // List[reflect.runtime.universe.Symbol] = List(type A)
rt.takesTypeArgs // false !!
rt.termSymbol //None
rt.typeSymbol.typeSignature // Much, talk about generics, but no Int
rt.typeSymbol.asInstanceOf[TypeSymbol].typeParams // List[reflect.runtime.universe.Symbol] = List(type A)
rt.getClass // Class[_ <: reflect.runtime.universe.Type] = class scala.reflect.internal.Types$TypeRef$$anon$1
rt.asInstanceOf[TypeRef].sym // List