1

I have the following code:

object Repro {
    def main(args: Array[String]): Unit = {
        println(classOf[Foo.Bar].getSimpleName)
    }

    object Foo {
        trait Bar
    }
}

When I run it I get:

Exception in thread "main" java.lang.InternalError: Malformed class name
    at java.lang.Class.getSimpleName(Class.java:1322)
    at Tests$.main(Tests.scala:3)
    at Tests.main(Tests.scala)

Is this the intended behavior? If so, why? What am I missing here?

I'm using JDK 1.8.0_25 and Scala 2.11.7.

1

1 Answer 1

4

You are hitting a very old Scala bug: SI-2034.

You could use Scala reflection instead of plain Java reflection, and achieve the same thing:

import scala.reflect.runtime.universe._

scala> val t = typeOf[A.B.type]
t: reflect.runtime.universe.Type = A.B.type

scala> t.termSymbol.name
res11: reflect.runtime.universe.Symbol#NameType = B

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.