3
votes

Is there a way to tell whether a class was implemented in Scala?

I'm building on a serialization system for the JVM that deals extensively with reflection. Java classes are already handled but the method is inadequate for Scala classes (for example, in Scala List[Int] turns into List[Object] in Java's reflection system, but this lost information is recoverable with scala's reflection). It like a switch to say whether I should perform scala-specific handling of a class.

1
For 2.10 an easy option: checking for ScalaObject (scala-lang.org/api/2.10.4/index.html#scala.ScalaObject).Gábor Bakos
.. . says its deprecated :(user48956
Yes, it was removed in 2.11. But it was present in the 2.10.x series.Gábor Bakos

1 Answers

5
votes
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