Platform.getClassForName

24 views
Skip to first unread message

Mark_Grand

unread,
Feb 12, 2012, 12:39:48 PM2/12/12
to scala-user
I am having trouble getting GetClassForName to find classes that I
have defined, but it can find classes from java.lang. What am I doing
wrong.

Here is a simple example of my problem:

scala> import scala.compat.Platform
import scala.compat.Platform

scala> class Foo{}
defined class Foo

scala> val f = new Foo
f: Foo = Foo@171fcdde

scala> f.getClass().getName()
res0: java.lang.String = Foo

scala> Platform.getClassForName("Foo")
java.lang.ClassNotFoundException: Foo
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:
301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at scala.compat.Platform$.getClassForName(Platform.scala:57)
at .<init>(<console>:9)
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> Platform.getClassForName("java.lang.Math")
res2: java.lang.Class[_] = class java.lang.Math

Tom Switzer

unread,
Feb 12, 2012, 1:02:10 PM2/12/12
to Mark_Grand, scala-user

I suspect it has to do with the class being in the default package, as they're often treated specially. What happens if you put the class in a package?

Mark Grand

unread,
Feb 12, 2012, 1:10:07 PM2/12/12
to Tom Switzer, scala-user
The original problem was with a class in a package. The full name of that class is com.peppyandcheap.hytaps.model.datatype.HText
I presented a simplified example to keep my question short.
--
Mark Grand
(404)925 8265
Vision, Values, Verve:
* Visualize the end game with
* Values to lead ethically and with purpose; and
* Verve to execute with shameless audacity.

Erik Engbrecht

unread,
Feb 12, 2012, 1:22:11 PM2/12/12
to scala...@googlegroups.com
This is interesting...

Given this little program:
class Foo                                                                                                                                   
                                                                                                                                            
object TestClass {                                                                                                                          
  def main(args: Array[String]) {                                                                                                           
    try {                                                                                                                                   
      // this fails                                                                                                                         
      val cls = scala.compat.Platform.getClassForName("Foo")                                                                                
      println(cls.getName)                                                                                                                  
    } catch {                                                                                                                               
      // so we get a stack trace here                                                                                                       
      case e: Exception => e.printStackTrace                                                                                                
    }                                                                                                                                       
    // this succeeds                                                                                                                        
    val cls = Class.forName("Foo")                                                                                                          
    println(cls.getName)                                                                                                                    
  }                                                                                                                                         
}                                                                                                                                           
  

Produces the following results:
$ scala TestClass
java.lang.ClassNotFoundException: Foo
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at scala.compat.Platform$.getClassForName(Platform.scala:57)
at TestClass$.main(TestCls.scala:6)
at TestClass.main(TestCls.scala)
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.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Foo

Erik Engbrecht

unread,
Feb 12, 2012, 1:29:54 PM2/12/12
to scala...@googlegroups.com
Even more interesting, if you compile it with -optimize, both calls succeed:

$ scalac -optimize TestCls.scala
$ scala TestClass
Foo
Foo

Probably because Platform.getClassForName as the @inline annotation, so with the -optimize flag the call in Platform to getClassForName is inlined into the TestClass object as Class.forName, while without -optimize the call to Class.forName is made from the Platform object.

Looks like a bug to me.

Erik Engbrecht

unread,
Feb 12, 2012, 1:48:02 PM2/12/12
to scala...@googlegroups.com

Dave

unread,
Feb 12, 2012, 1:55:02 PM2/12/12
to scala-user
Strange

I tested with 2.9.1.final and 2.10.0-M1 compilers and they both work

Only in the REPL it doesn't work.

But I have Windows 7

On 12 feb, 19:48, Erik Engbrecht <erik.engbre...@gmail.com> wrote:
> https://issues.scala-lang.org/browse/SI-5457

Erik Engbrecht

unread,
Feb 12, 2012, 2:58:30 PM2/12/12
to scala...@googlegroups.com
It probably has something to do with differences in the script that is used to run Scala.  If I use this command:
$ java -classpath .:/usr/local/share/scala/lib/scala-library.jar TestClass

Then it works.
Reply all
Reply to author
Forward
0 new messages