lundi 5 juin 2017

scala reflection instantiate class with multiple paramLists

Assume we have class A

class A(val x: Any)(val y: Any)

then we want to instantiate it reflectively having x & y values:

def instantiate[T](aClass: Class[T], args: List[Any]): T = {
    val mirror = ru.runtimeMirror(aClass.getClassLoader)
    val classMirror = mirror.reflectClass(mirror.classSymbol(aClass))
    val constructorMirror = classMirror.reflectConstructor(getPrimaryConstructor(aClass))
    constructorMirror.apply(args: _*).asInstanceOf[T]
}

this method works fine for classes with one paramList and how to do instantiate class with ()() in primary constructor? I suppose my instantiate method would have signature:

instantiate[T](aClass: Class[T], args: List[List[Any]]): T = ???

thanx





Aucun commentaire:

Enregistrer un commentaire