How to get to type parameters of a reflect.runtime

2019-07-23 12:50发布

问题:

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

回答1:

Scala 2.11

You need rt.typeArgs, which returns a list of types (in your case it contains a single element: Int).

Scala 2.10

val args = returnType match {
  case r: universe.TypeRefApi => r.args
  case _ => List()
}


回答2:

Stack overflow has the answer for 2.10 here: Finding type parameters via reflection in Scala 2.10?

rt.asInstanceOf[TypeRefApi].args

. . . . . .