Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reflection doesn't work in REPL #5947

Closed
scabug opened this issue Jun 19, 2012 · 4 comments
Closed

reflection doesn't work in REPL #5947

scabug opened this issue Jun 19, 2012 · 4 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Jun 19, 2012

scala> class A
defined class A

scala> class B {
     | def foo(x: A) = 1
     | }
defined class B

scala> val cm = reflect.runtime.currentMirror
cm: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader@1f517997 of type class scala.tools.nsc.interpreter.IMain$TranslatingClassLoader with classpath <unknown>

scala> val u = cm.universe
u: reflect.runtime.universe.type = scala.reflect.runtime.JavaUniverse@44cdf872

scala> val im = cm.reflect(new B)
im: reflect.runtime.universe.InstanceMirror = scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror@76ad962c

scala> val method = im.symbol.asType.nonPrivateMember(u.newTermName("foo")).asMethodSymbol
method: reflect.runtime.universe.MethodSymbol = method foo

scala> im.reflectMethod(method)
res0: reflect.runtime.universe.MethodMirror = scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror@40c84051

scala> res0(new A)
java.lang.ClassNotFoundException: no Java class corresponding to object $iw found
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.scala$reflect$runtime$JavaMirrors$JavaMirror$$anonfun$$noClass$1(JavaMirrors.scala:905)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1$$anonfun$apply$3.apply(JavaMirrors.scala:917)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1$$anonfun$apply$3.apply(JavaMirrors.scala:917)
        at scala.Option.getOrElse(Option.scala:120)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:917)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:904)
        at scala.reflect.runtime.TwoWayCache.toJava(TwoWayCache.scala:36)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.classToJava(JavaMirrors.scala:904)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:914)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:904)
        at scala.reflect.runtime.TwoWayCache.toJava(TwoWayCache.scala:36)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.classToJava(JavaMirrors.scala:904)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:914)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$classToJava$1.apply(JavaMirrors.scala:904)
        at scala.reflect.runtime.TwoWayCache.toJava(TwoWayCache.scala:36)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.classToJava(JavaMirrors.scala:904)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.typeToJavaClass(JavaMirrors.scala:974)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$methodToJava$1$$anonfun$21.apply(JavaMirrors.scala:943)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$methodToJava$1$$anonfun$21.apply(JavaMirrors.scala:943)
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:239)
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:239)
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
        at scala.collection.immutable.List.foreach(List.scala:77)
        at scala.collection.TraversableLike$class.map(TraversableLike.scala:239)
        at scala.collection.AbstractTraversable.map(Traversable.scala:105)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$methodToJava$1.apply(JavaMirrors.scala:943)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$methodToJava$1.apply(JavaMirrors.scala:941)
        at scala.reflect.runtime.TwoWayCache.toJava(TwoWayCache.scala:36)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.methodToJava(JavaMirrors.scala:941)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jmeth$lzycompute(JavaMirrors.scala:179)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jmeth(JavaMirrors.scala:178)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.apply(JavaMirrors.scala:192)
        at .<init>(<console>:15)
        at .<clinit>(<console>)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:736)
        at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:991)
        at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:579)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:610)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:574)
        at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:742)
        at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:787)
        at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:699)
        at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:563)
        at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:570)
        at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:573)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:864)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:819)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:819)
        at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
        at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:819)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:79)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:92)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jun 19, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5947?orig=1
Reporter: @xeno-by
Affected Versions: 2.10.0
Duplicates #4023

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 11, 2012

@xeno-by said:
Duplicate of #4023

@scabug scabug closed this Jul 11, 2012
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 11, 2012

@dcsobral said:
Even though this issue has been closed, I'd like to point out something said on issue SI-4316, linked by #4023:

Paul Phillips added a comment - 13/Aug/11 7:58 PM
Don't hold me to too many details, but yes. It is the reverse which gets us elsewhere: reflection expects a class called foo$bar$baz$1 to be nested inside a class called foo$bar$baz, and with us that's not all that likely.

Grzegorz Kossakowski added a comment - 13/Aug/11 8:05 PM
I see. It's interesting, I thought InnerClasses attribute in a class servers exactly this purpose to provide information about nesting. If implementations decided to ignore their own invention and rely on strings then I'd be in favor of ignoring Java reflection entirely.
It's quite likely that we'll be in luxury situation of having native Scala reflection that we can point folks at.

Is Scala reflection making use of that InnerClasses attribute? Could it, to avoid this problem?

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 11, 2012

@xeno-by said:
Let's proceed with the discussion at #4023, okay? Greg has also asked a similar question there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.