Is it possible to tell if a class is being implemented in Java versus Scala?

Is there a way to find out if a class has been implemented in Scala?

I am building on the serialization system for the JVM which is reflection intensive. Java classes have already been processed, but the method is not suitable for Scala classes (for example, in Scala, List [Int] turns to List [Object] in Java reflection system, but this lost information is recovered with Scala reflection) It's like a switch to say should i do scala -special handling of the class.

+3


source to share


1 answer


def isJavaClass( cls: Class[_] ): Boolean = {
  import scala.reflect.runtime.{universe=>ru}
  val sym = ru.runtimeMirror(cls.getClassLoader).classSymbol(cls)
  sym.isJava
}

      

REPL test:



scala> isJavaClass(classOf[List[Any]])
res0: Boolean = false

scala> isJavaClass(classOf[java.util.List[Any]])
res1: Boolean = true

      

+5


source







All Articles