mardi 17 février 2015

Get Java reflection representation of Scala type

This seems like a simple question, but it's very challenging to search for, so I'm asking a new question. My apologies if it's already been asked.


Due to the compiler bug described here Scala 2.11.5 compiler crash with type aliases and manifests (also here http://ift.tt/1zLQP4i), I need to use scala TypeTags and friends for discovery of type parameters to methods. However, I then need to use that type information in a Java library that uses java.lang.Class and java.lang.reflect.Type.


How can I convert a scala.reflect.runtime.universe Type into a java.lang.reflect.Type or java.lang.Class?


Put concretely, how would I fill out the body of this method:



def typeFor[T](implicit tag: TypeTag[T]): java.lang.reflect.Type = ...


or, if that's not possible:



def typeFor[T](implicit tag: TypeTag[T]): java.lang.Class[T] = ...


And note, due to the bug posted above, I cannot use scala.reflect.Manifest.






Aucun commentaire:

Enregistrer un commentaire