我的程序中有一个抽象类
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
}
当我尝试加载类时,它们是源代码形式,所以我在加载时编译它们
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()
这是测试文件
import au.com.cleanstream.kumo.deploy.Deployment
class testing extends Deployment {
override def run(): Unit = {
logger.info("TEST")
}
}
但是当我运行代码时,我得到java.lang.AssertionError: assertion failed
,我也已经累了toolBox.compile(parsedScript)
,但却得到了相同的东西。
非常感谢任何帮助!
答案 0 :(得分:2)
toolBox.eval(parsedScript)
返回() => Any
在您的情况下,测试文件返回Unit
,compiledScript
的类型为BoxedUnit
。
如果更改测试文件以返回某个值,则可以访问它。 我修改了测试文件如下:
object Testing extends Deployment {
override def run(): Unit = {
logger.info("TEST")
}
}
Testing //This is the SOLUTION