Class file creation from Toolbox compilation


#1

Hi,

I use the following to compile code at runtime (works fine):

  val toolbox =
    reflect.runtime.currentMirror
      .thn(scala.tools.reflect.ToolBox)
      .mkToolBox()

  /** source shouldn't contain package statements (TODO: why?) */
  def objectInstance[A](
      objectDefinition : String,
      objectName       : String /* TODO: parse source to extract name rather? */): A = {

    val source      : String         = objectDefinition.newline(objectName) // because must return an instance it seems
    val tree        : toolbox.u.Tree = toolbox.parse  (source)
    val anyProducer : () => Any      = toolbox.compile(tree)    

    anyProducer.apply.asInstanceOf[A]
  }    

However I can’t seem to find a way to actually write a corresponding class file, if that’s even possible. Anybody knows?

Thanks!