程序中有一个抽象类
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)
,但得到了相同的东西。
任何帮助都非常感谢!
toolBox.eval(parsedScript)
返回() => Any
在您的示例中,测试文件返回Unit
, compiledScript
类型为BoxedUnit
。
如果您更改测试文件以返回一些值,您将能够访问它。我修改了测试文件如下:
object Testing extends Deployment {
override def run(): Unit = {
logger.info("TEST")
}
}
Testing //This is the SOLUTION