mardi 3 mai 2016

How to obtain generic type at runtime in Scala

I have a following class

package myapp.model

case class Person(
 name: String,
 age: Option[Int]
)

I would like to implement following function:

def getFieldClass(className: String, fieldName:String): java.lang.Class = {
    // case normal field return its class
    // case Option field return generic type of Option
}

So that for following input:

  • className="myapp.model.Person"
  • fieldName="age"

the function will return class object: scala.Int





Aucun commentaire:

Enregistrer un commentaire