mardi 14 mai 2019

How to Reflect Construction Method Based on Method Signature In Scala

class Test(a: String, b: Array[String], c: Array[String]){
  def this(b: Array[String], c: Array[String]) {
    this("1", b, c)
  }

  def this() = {
    this(null, null, null)()
  }
}

I have a class Test like above, and I would like use scala reflection to invoke one of them

I try to use the follow code

import scala.reflect.runtime.{universe => ru}
val clsTest = ru.typeOf[Test].typeSymbol.asClass
val cm = m.reflectClass(clsTest)
val ctor =  ru.typeOf[Test].decl(ru.termNames.CONSTRUCTOR).asTerm.alternatives.map(_.asMethod)

but I don't know how to select the method based on method signature. Is there any approach to select the method based on the type signature like java reflect code? Thanks!

I have read the scala doc about reflect, but it don't solve my problem. it has only one constructor method. scala reflect doc





Aucun commentaire:

Enregistrer un commentaire