How can I extract the field values from a case class in scala using the new reflection model in scala 2.10? For example, using the below doesn't pull out the field methods
def getMethods[T:TypeTag](t:T) = typeOf[T].members.collect {
case m:MethodSymbol => m
}
I plan to pump them into
for {field <- fields} {
currentMirror.reflect(caseClass).reflectField(field).get
}
MethodSymbol
has anisCaseAccessor
method that allows you to do precisely this:Now you can write the following:
And you get only the method symbols you want.
If you want to get fancier you can get them in order by inspecting the constructor symbol. This code works even if the case class type in question has multiple constructors defined.