Scala Programming Language
  1. Scala Programming Language
  2. SI-6323

cannot reflect against instances of local classes

    Details

      Description

      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)
      

        Activity

        Hide
        Eugene Burmako added a comment -

        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)
          }
        }
        
        Show
        Eugene Burmako added a comment - 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) } }
        Show
        Eugene Burmako added a comment - Fixed in https://github.com/scala/scala/commit/0eabb63c06f4041077860875c9ea71216adf4faa and https://github.com/scala/scala/commit/524c90d09f2fb4687b312f2c7597393978d50b6a

          People

          • Assignee:
            Eugene Burmako
            Reporter:
            Eugene Burmako
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development