user461697
user461697

Reputation:

Scala get constructor parameters at runtime

I'm trying to use reflection to get the types of constructor parameters of a class at runtime. The method below is in the abstract superclass. However this.type is not behaving as expected. Any help would be much appreciated.

  /**
   * Maps constructor field names to types
   *
   * @return Map[String, Type]
   */
  def getParamTypes: Map[String, ru.Type] =
    ru.typeOf[this.type].
      member(ru.termNames.CONSTRUCTOR)
      .asMethod.paramLists(0).
      foldRight(Map(): Map[String, ru.Type])((p, a) => {
        a + (p.name.decodedName.toString -> p.typeSignature)
      })

Upvotes: 0

Views: 2512

Answers (1)

senia
senia

Reputation: 38045

Method typeOf requires implicit TypeTag. You should use TypeTag directly to get type information available only on call-side:

trait Test {
  def getParamTypes(implicit ttag: ru.TypeTag[this.type]) =
    ttag.tpe.
      member(ru.termNames.CONSTRUCTOR).
      asMethod.paramLists(0).
      foldRight(Map(): Map[String, ru.Type])((p, a) => {
        a + (p.name.decodedName.toString -> p.typeSignature)
      })
}

class TestC(i: Int, s: String) extends Test

val t = new TestC(1, "")
t.getParamTypes
// Map[String,Type] = Map(s -> String, i -> Int)

Alternatively you could get Type of this from Class.

trait Test2 {
  def getParamTypes: Map[String, ru.Type] = {
    val clazz = getClass
    val tpe = ru.runtimeMirror(clazz.getClassLoader).classSymbol(clazz).toType
    tpe.
      member(ru.termNames.CONSTRUCTOR).
      asMethod.paramLists(0).
      foldRight(Map(): Map[String, ru.Type])((p, a) => {
        a + (p.name.decodedName.toString -> p.typeSignature)
      })
  }
}

class TestC2(i: Int, s: String) extends Test2

new TestC2(1, "").getParamTypes
// Map[String,Type] = Map(s -> String, i -> Int)

Upvotes: 4

Related Questions