Scala object reference with reflection

I am new to reflection API.

I want to get a link to an object from its name. I did this to such an extent that I can get the link using the class name of the object.

$ scala
Welcome to Scala version 2.11.7 ...

scala> case object Foo { val x = 5 }
defined object Foo

scala> import scala.reflect.runtime.{universe => ru}
import scala.reflect.runtime.{universe=>ru}

scala> val m = ru.runtimeMirror(getClass.getClassLoader)
m: reflect.runtime.universe.Mirror...

scala> val f = m.reflectModule(m.staticModule(Foo.getClass.getName)).instance.asInstanceOf[Foo.type]
f: Foo.type = Foo

scala> f.x
res0: Int = 5

It works well. However, trying to use the computed type name as a string does not work:

scala> m.staticModule(Foo.getClass.getName)
res2: reflect.runtime.universe.ModuleSymbol = object iw$Foo$

scala> Foo.getClass.getName
res1: String = Foo$

scala> m.staticModule("Foo$")
scala.ScalaReflectionException: object Foo$ not found.
  at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:162)
  at scala.reflect.internal.Mirrors$RootsBase.staticModule(Mirrors.scala:22)
  ... 33 elided

What am I missing here? Thanks.

+4
source share
1 answer

This problem appears only in REPL. In the REPL, do the following:

scala> Foo.getClass.getName.length
res5: Int = 25

So, "Foo $" is not the full name of the Foo class

scala> new String(Foo.getClass.getName.getBytes("UTF-8").map(b => if(b==36) '?'.toByte else b), "UTF-8")
res6: String = ?line3.?read??iw??iw?Foo?

And you can call without problems:

scala>m.staticModule("$line3.$read$$iw$$iw$Foo$")
res7: reflect.runtime.universe.ModuleSymbol = object iw$Foo$

See also: https://issues.scala-lang.org/browse/SI-9335

+1
source

Source: https://habr.com/ru/post/1617855/


All Articles