Last active
July 27, 2020 04:07
-
-
Save miguelortegarodriguez/22d51ebb2a13dccbc34a to your computer and use it in GitHub Desktop.
Convert scala type to java type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.{ParameterizedType, Type => JType} | |
import scala.reflect.runtime.universe._ | |
object TypeConverter { | |
val defaultMirror = runtimeMirror(getClass.getClassLoader) | |
implicit val implicitConverter = scalaToJavaType(defaultMirror) _ | |
def scalaToJavaType(mirror: Mirror)(tpe: Type): JType = | |
tpe.typeArgs match { | |
case Nil => mirror.runtimeClass(tpe.typeConstructor.typeSymbol.asClass) | |
case x:List[Type] => | |
new ParameterizedType { | |
def getRawType = mirror.runtimeClass(tpe.typeConstructor.typeSymbol.asClass) | |
def getActualTypeArguments = x.map(t => mirror.runtimeClass(t.typeSymbol.asClass)).toArray | |
def getOwnerType = null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment