1

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!

1 Answer 1

2

toolBox.eval(parsedScript) returns () => Any In Your case the test file returns Unit and compiledScript is of type BoxedUnit.

If you change the test file to return some value, you will be able to access it. I have modified test file as follows:

object Testing extends Deployment {
  override def run(): Unit = {
    logger.info("TEST")
  }
}
Testing //This is the SOLUTION
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much, looking back on this it makes much more sense!
Yes, much more sense! and I would like to add that it's possible to use anonymous object here, new Deployment { override def run(): Unit = { logger.info("TEST") } } // end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.