Instantiating a case class with default args via reflection

后端 未结 4 1510
孤街浪徒
孤街浪徒 2021-01-24 04:03

I need to be able to instantiate various case classes through reflection, both by figuring out the argument types of the constructor, as well as invoking the constructor with al

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-24 04:30

    Not minimized... and not endorsing...

    scala> import scala.reflect.runtime.universe
    import scala.reflect.runtime.universe
    
    scala> import scala.reflect.internal.{ Definitions, SymbolTable, StdNames }
    import scala.reflect.internal.{Definitions, SymbolTable, StdNames}
    
    scala> val ds = universe.asInstanceOf[Definitions with SymbolTable with StdNames]
    ds: scala.reflect.internal.Definitions with scala.reflect.internal.SymbolTable with scala.reflect.internal.StdNames = scala.reflect.runtime.JavaUniverse@52a16a10
    
    scala> val n = ds.newTermName("foo")
    n: ds.TermName = foo
    
    scala> ds.nme.defaultGetterName(n,1)
    res1: ds.TermName = foo$default$1
    

提交回复
热议问题