- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Platform.getClassForName
Sun, 2012-02-12, 18:40
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 .(:9)
at .()
at .(:11)
at .()
at $print()
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
Sun, 2012-02-12, 19:21
#2
Re: Platform.getClassForName
The original problem was with a class in a package. The full name of that class is com.peppyandcheap.hytaps.model.datatype.HTextI presented a simplified example to keep my question short.
On Sun, Feb 12, 2012 at 1:02 PM, Tom Switzer <thomas.switzer@gmail.com> wrote:
--
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.
On Sun, Feb 12, 2012 at 1:02 PM, Tom Switzer <thomas.switzer@gmail.com> wrote:
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?
On 2012-02-12 12:40 PM, "Mark_Grand" <mgrand@markgrand.com> wrote: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
--
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.
Sun, 2012-02-12, 19:31
#3
Re: Platform.getClassForName
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 TestClassjava.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
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 TestClassjava.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
Sun, 2012-02-12, 19:41
#4
Re: Platform.getClassForName
Even more interesting, if you compile it with -optimize, both calls succeed:
$ scalac -optimize TestCls.scala$ scala TestClassFooFoo
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.
$ scalac -optimize TestCls.scala$ scala TestClassFooFoo
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.
Sun, 2012-02-12, 19:51
#5
Re: Platform.getClassForName
Sun, 2012-02-12, 20:01
#6
Re: Platform.getClassForName
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 wrote:
> https://issues.scala-lang.org/browse/SI-5457
Sun, 2012-02-12, 21:11
#7
Re: Platform.getClassForName
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.
Then it works.
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?
On 2012-02-12 12:40 PM, "Mark_Grand" <mgrand@markgrand.com> wrote: