Consider the following code:
class Foo(var name: String = \"bar\")
Now i try to get the value and the correct type of it via reflection:
<
This is how one can get list of fieldnames and its value of a case class:
First, using reflection, get fields info as follows -
val TUPLE2_OF_FIELDNAME_TO_GETTERS = typeOf[].members
.filter(!_.isMethod)
.map(x => (x.name.toString, classOf[].getDeclaredMethod(x.name.toString.trim)))
How to use it?
getFieldNameAndValue(obj: ): Seq[(String, String)] {
var output = Seq[(String, String)]()
for(fieldToGetter <- TUPLE2_OF_FIELDNAME_TO_GETTERS) {
val fieldNameAsString = fieldToGetter._1
val getter = fieldToGetter._2
val fieldValue = getter.invoke(obj).toString
output += (fieldName, fieldValue)
}
}