jeudi 10 mars 2016

In scala, using reflection to get real type of varargs parameters of method

I'm using reflection to get parameter type of method, but there is some problem with varargs. Here is my demo

import reflect.runtime.universe._

case class C() {
  def f(x: String*) = x
}

object Main {
  def main(args: Array[String]): Unit = {
    val c = C()
    val r = runtimeMirror(getClass.getClassLoader).reflect(c)
    val x = r.symbol.typeSignature.member(newTermName("f"))
    val params = x.asMethod.paramss.head.map {
      _.typeSignature.typeSymbol
    }
    for (param <- params) {
      println(param)
    }
  }
}

The result is: class < repeated> // There is no space between '<' and 'r', i add one, otherwise the words will disappear.

How can i get the real type 'String'? Or is there any way else to do this?





Aucun commentaire:

Enregistrer un commentaire