I’m having trouble figuring out how to get the type parameter of Option when the type parameter is Int
I tried the following code:
class TestClass {
val intField: Option[Int] = Some(1)
}
val cls = new TestClass
val field = cls.getDeclaredField("intField")
val typeParams = field.getGenericType.asInstanceOf[ParameterizedType].getActualTypeArguments
typeParams gives me java.lang.Object
my question is how do I get it to return java.lang.Integer to me
The exact generic type information is lost at run-time. See Reflecting generics and note:
There are no subclasses here — hence the reason for
Objectand notInteger. Perhaps look into Manifests (a Scala trick for implicit reified types)?Happy coding.