Greg
Greg

Reputation: 11512

Deep access of fields in Scala using runtime reflection

I have code that deeply walks a case class' constructor fields, which of course may themselves be complex (list of things, maps, options, and other case classes). The code I found to extract field values at runtime works great on the highest-level fields but explodes when I try to access deeper fields. Example below.

I real life my application introspects the fields at each level, so I know that 'stuff' is another case class (I have the Symbol/Type), and I know Dos' field Symbols/Types. But this is obtained at runtime so I think it's blowing up because it doesn't know [T]/Manifest[T]. Is there a way to get this at runtime via reflection? How might my code change? The examples I found seemed to all require various things[T], which I wouldn't have for 'dos', right?

case class Uno( name:String, age:Int, pets:List[String], stuff:Dos )
case class Dos( foo:String )

object Boom extends App {
    val ru = scala.reflect.runtime.universe
    val m = ru.runtimeMirror(getClass.getClassLoader)

    val u = Uno("Marcus",19,List("fish","bird"),Dos("wow"))

    println("NAME: "+unpack(u,"name")) // Works
    println("PETS: "+unpack(u,"pets")) // Works

    // ----- Goes Boom -------
    val dos = unpack(u,"stuff")
    println("Other: "+unpack(dos,"foo")) // Boom!
    // -----------------------

    // Get object value for named parameter of target
    def unpack[T]( target:T, name:String )(implicit man:Manifest[T]) : Any = {
        val im = m.reflect(target)
        val fieldX = ru.typeOf[T].declaration(ru.newTermName(name)).asTerm.accessed.asTerm
        im.reflectField(fieldX).get
    }

}

Upvotes: 1

Views: 968

Answers (1)

som-snytt
som-snytt

Reputation: 39577

You're exactly right, the type of your dos is Any.

FieldMirror.symbol.typeSignature is what you'd get from typeOf[Dos].

So consider returning a pair (Any, Type) from unpack to have something to pass to unpack(target, type, name). Somewhat like:

case class Uno(name: String, age: Int, pets: List[String], stuff: Dos)
case class Dos(foo: String)

object Boom extends App {
  import scala.reflect.runtime.universe._
  import scala.reflect.runtime.{ currentMirror => cm }
  import scala.reflect.ClassTag

  val u = Uno("Marcus", 19, List("fish", "bird"), Dos("wow"))

  println("NAME: " + unpack(u, "name")) // Works
  println("PETS: " + unpack(u, "pets")) // Works

  // ----- Goes Boom -------
  val (dos, dosT) = unpack(u, "stuff")
  println("Other: " + unpack(dos, dosT, "foo")) // Boom! ...or fizzle
  // -----------------------

  def unpack[T: TypeTag](target: T, name: String): (Any, Type) = unpack(target, typeOf[T], name)

  // Get object value for named parameter of target
  def unpack[T](target: T, t: Type, name: String): (Any, Type) = {
    val im = cm.reflect(target)(ClassTag(target.getClass))
    val fieldX = t.declaration(newTermName(name)).asTerm.accessed.asTerm
    val fm = im.reflectField(fieldX)
    (fm.get, fm.symbol.typeSignature)
  }
}

Upvotes: 3

Related Questions