import scala.reflect.runtime.universe
import scala.tools.reflect.ToolBox
val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()
val parsed = tb.parse(“object Test”)
scala> Test
^
error: not found: value Test
How to make the toolbox to use the application classloader to create the class so it can be seen here?
there is some related past discussion at https://github.com/scala/bug/issues/7081
For one thing, you only parsed object Test
. You didn’t compile or evaluate it.
Another thing is that Test
is an object, not a class. You definitely can’t define a value in the code that you pass to the toolbox and then access it by name like you defined it in your actual program.
You can do:
val test = tb.eval(tb.parse(“object Test; Test”))
And then test
with hold a reference to object Test
because the code in the toolbox evaluates to Test
. But value test
will have type Any
. The type Test.type
is not accessible from the outside.
I believe that scala.tools.nsc.interpreter.IMain
allows you to extract values by their name from the current session.