mercredi 2 septembre 2015

Compiling Scala source in a string at runtime

I have an abstract class in my program as such

abstract class Deployment {

  /**
   * Defines a logger to be used by the deployment script
   */
  protected val logger = LoggerFactory.getLogger(this.getClass)

  /**
   * Defines the entry point to the deployment
   *
   * @throws java.lang.Exception Thrown on the event of an unrecoverable error
   */
  def run(): Unit
}

And when I try loading classes they are in their source form so I compile them on load like so

val scriptFile = new File("testing.scala")

val mirror = universe.runtimeMirror(getClass.getClassLoader)
val toolBox = mirror.mkToolBox()

val parsedScript = toolBox.parse(Source.fromURI(scriptFile.toURI).mkString)
val compiledScript = toolBox.eval(parsedScript)

val deployment = compiledScript.asInstanceOf[Deployment]
deployment.run()

And this is the test file

import au.com.cleanstream.kumo.deploy.Deployment

class testing extends Deployment {

  override def run(): Unit = {
    logger.info("TEST")
  }

}

But when I run the code I get java.lang.AssertionError: assertion failed, I have also tired toolBox.compile(parsedScript) but got the same thing.

Any help is greatly appreciated!





Aucun commentaire:

Enregistrer un commentaire