getSimpleName missbehaves for case classes

25 views
Skip to first unread message

Razvan Cojocaru

unread,
Sep 13, 2011, 8:01:20 PM9/13/11
to scala...@googlegroups.com

I found a June thread but with no reply… I remember having this issue myself in the past, so here goes for future reference.

 

$ scala

Failed to created JLineReader: java.lang.NoClassDefFoundError: Could not initialize class org.fusesource.jansi.internal.

Kernel32

Falling back to SimpleReader.

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).

Type in expressions to have them evaluated.

Type :help for more information.

 

scala> case class C

warning: there were 1 deprecation warnings; re-run with -deprecation for details

defined class C

 

scala> C.getClass.getDeclaredMethods.map(_.getReturnType.getSimpleName)

java.lang.InternalError: Malformed class name

        at java.lang.Class.getSimpleName(Class.java:1133)

        at $anonfun$1.apply(<console>:10)

        at $anonfun$1.apply(<console>:10)

        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)

        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)

        at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)

        at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)

        at scala.collection.TraversableLike$class.map(TraversableLike.scala:194)

        at scala.collection.mutable.ArrayOps.map(ArrayOps.scala:38)

        at .<init>(<console>:10)

        at .<clinit>(<console>)

        at .<init>(<console>:11)

        at .<clinit>(<console>)

        at $print(<console>)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

        at java.lang.reflect.Method.invoke(Method.java:597)

        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)

        at scala.tools.nsc.interpreter.IMain$Request$$anonfun$14.apply(IMain.scala:920)

        at scala.tools.nsc.interpreter.Line$$anonfun$1.apply$mcV$sp(Line.scala:43)

        at scala.tools.nsc.io.package$$anon$2.run(package.scala:25)

        at java.lang.Thread.run(Thread.java:662)

 

scala>

 

Well, the reason is the REPL mangles some of the types and makes them look like: $line4.$read$$iw$$iw$C AND Java.lang.Class really doesn’t like that.

 

… meh, 2 more hours spent chasing dollars… or rather dollar signs… oh, well. The thing is you have no way of figuring that out without some medieval debugging

 

Anyways, it makes looking for classnames rather painful. This after dealing with a bunch of add-ons like MODULE$ or readResolve.

 

If you’re browsing class hierarchies via reflection, I strongly suggest you rather Snakk them, which works for now with both Java/Scala, compiled code and interpreted:

 

http://t.co/RODIXwo

 

case class C4 val c="wow" }
case class C3 val c=C4() }
case class C2 val c=C3() }
case class C1 val c=C2() }

razie.Snakk.bean(C1()) \\ "C4" \@@ "c"

 

 

Happy snakking!

 

 

 

 

 

Reply all
Reply to author
Forward
0 new messages