Created
August 10, 2011 10:14
-
-
Save debasishg/1136502 to your computer and use it in GitHub Desktop.
Problem loading a Scala *object* reflectively ..
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Foo.scala as .. | |
package net.debasishg.domain.trade.service | |
trait Foo { | |
final val foo: Int => Int = {i => i * 2} | |
} | |
object Foo extends Foo | |
and a test spec TSpec.scala as .. | |
@RunWith(classOf[JUnitRunner]) | |
class TSpec extends Spec with ShouldMatchers { | |
describe("serializable") { | |
it("should do") { | |
import java.net._ | |
val urls = Array( | |
new URL("file:///home/debasish/my-projects/cqrs-akka/target/scala_2.9.0/classes/"), | |
new URL("file:////home/debasish/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.9.0.jar")) | |
val cl = new URLClassLoader(urls).loadClass("net.debasishg.domain.trade.service.Foo$") | |
println(cl) | |
println(cl.newInstance) | |
} | |
} | |
} | |
running the test case I get the following exception in the newInstance statement .. | |
java.lang.IllegalAccessException: Class net.debasishg.domain.trade.service.TSpec$$anonfun$1$$anonfun$apply$mcV$sp$1 can not access a member of class net.debasishg.domain.trade.service.Foo$ with modifiers "private" | |
[info] at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:95) | |
[info] at java.lang.Class.newInstance0(Class.java:366) | |
.... | |
.... | |
Help ? Anyone ? What's the private thing that it refers to ? When I do a javap on Foo$, I get .. | |
debasish@debasishg:~/my-projects/cqrs-akka$ javap -private -classpath ~/my-projects/cqrs-akka/target/scala_2.9.0/classes net.debasishg.domain.trade.service.Foo$ | |
Compiled from "Foo.scala" | |
public final class net.debasishg.domain.trade.service.Foo$ extends java.lang.Object implements net.debasishg.domain.trade.service.Foo,scala.ScalaObject{ | |
public static final net.debasishg.domain.trade.service.Foo$ MODULE$; | |
private final scala.Function1 foo; | |
public static {}; | |
public scala.Function1 foo(); | |
public void net$debasishg$domain$trade$service$Foo$_setter_$foo_$eq(scala.Function1); | |
private net.debasishg.domain.trade.service.Foo$(); | |
} |
Thanks for the feedback .. got it now working. Missed the MODULE$ field. Should have opened at the generated class before complaining :-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The constructor is private. You need to get the MODULE$ field.