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

cannot reflect against instances of local classes #6323

Closed
scabug opened this issue Sep 6, 2012 · 3 comments
Closed

cannot reflect against instances of local classes #6323

scabug opened this issue Sep 6, 2012 · 3 comments
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Sep 6, 2012

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => m}
import scala.reflect.runtime.{universe => u}

object Test {
  def main(args: Array[String]) {
    case class Test(a:String,b:List[Int])

    val lookAtMe = m.reflect(Test("a",List(5)))
    val value = u.typeOf[Test]
    val members = value.members
    val member = value.members.filter(_.name.encoded == "a")
    val aAccessor = lookAtMe.reflectMethod(member.head.asMethod)
    val thisShouldBeA = aAccessor.apply()
    println(thisShouldBeA)
  }
}
C:\Projects\Kepler\sandbox @ ticket/6246>myke run Test.scala
Main class (default is Test):
scala.ScalaReflectionException: expected a member of class Test$3, you provided value Test.Test.a
        at scala.reflect.runtime.JavaMirrors$JavaMirror.ErrorNotMember(JavaMirrors.scala:127)
        at scala.reflect.runtime.JavaMirrors$JavaMirror.scala$reflect$runtime$JavaMirrors$JavaMirror$$checkMemberOf(JavaMirrors.scala:163)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:191)
        at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror.reflectMethod(JavaMirrors.scala:173)
        at Test$.main(Test.scala:13)
        at Test.main(Test.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:71)
        at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:139)
        at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:71)
        at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:139)
        at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:28)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:45)
        at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:35)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:45)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 6, 2012

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 6, 2012

@xeno-by said:
A workaround:

import scala.reflect.runtime.universe._
import scala.reflect.runtime.{currentMirror => m}
import scala.reflect.runtime.{universe => u}

object Test {
  def main(args: Array[String]) {
    case class Test(a:String,b:List[Int])

    val lookAtMe = m.reflect(Test("a",List(5)))
//    val value = u.typeOf[Test]
    val value = m.classSymbol(classOf[Test]).typeSignature
    val members = value.members
    val member = value.members.filter(_.name.encoded == "a")
    val aAccessor = lookAtMe.reflectMethod(member.head.asMethod)
    val thisShouldBeA = aAccessor.apply()
    println(thisShouldBeA)
  }
}
@scabug scabug closed this Sep 14, 2012
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Sep 14, 2012

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.