简体   繁体   中英

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 https://issues.scala-lang.org/browse/SI-9155 ), I need to use scala TypeTag s 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 .

The short answer is no, but you can try to do something similar to this SO question . However there is an open ticket... .

This may have some limitations I'm not aware of, but you could drop down to Java reflection and try something like:

import scala.util.control.Exception._

def typeMe[T](implicit t: TypeTag[T]) = {
  catching(classOf[Exception]) opt Class.forName(t.tpe.typeSymbol.asClass.fullName)
}

println(typeMe[String])
println(typeMe[ClassTag[_]])

Results in:

Some(class java.lang.String)
Some(interface scala.reflect.ClassTag)

The way I solved it with manifests, was:

  private def typeFromManifest(m: Manifest[_]): Type = {
if (m.typeArguments.isEmpty) { m.runtimeClass }
else new ParameterizedType {
  def getRawType = m.runtimeClass
  def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
  def getOwnerType = null
}

}

Right now I'm trying to solve this using something other than Manifest which should be removed from scala runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM