java动态执行scala,在Scala 2.11中运行时动态编译Scala类文件

I have the following code that works in Scala 2.10 to compile external classes at runtime in Scala

/**

* Compile scala files and keep them loaded in memory

* @param classDir Directory storing the generated scala files

* @throws IOException if there is problem reading the source files

* @return Classloader that contains the compiled external classes

*/

@throws[IOException]

def compileFiles(classDir: String): AbstractFileClassLoader = {

val files = recursiveListFiles(new File(classDir))

.filter(_.getName.endsWith("scala"))

println("Loaded files: \n" + files.mkString("[", ",\n", "]"))

val settings: GenericRunnerSettings = new GenericRunnerSettings(err => println("Interpretor error: " + err))

settings.usejavacp.value = true

val interpreter: IMain = new IMain(settings)

files.foreach(f => {

interpreter.compileSources(new BatchSourceFile(AbstractFile.getFile(f)))

})

interpreter.getInterpreterClassLoader()

}

And then elsewhere, I could use the classloader reference to instantiate classes e.g.

val personClass = classLoader.findClass("com.example.dynacsv.PersonData")

val ctor = personClass.getDeclaredConstructors()(0)

val instance = ctor.newInstance("Mr", "John", "Doe", 25: java.lang.Integer, 165: java.lang.Integer, 1: java.lang.Integer)

println("Instantiated class: " + instance.getClass.getCanonicalName)

println(instance.toString)

However the above no longer works as getInterpreterClassLoader method has been removed from scala.tools.nsc.interpreter.IMain. Also, AbstractFileClassLoader has been moved and deprecated. It is no longer allowed to call findClass method in the class loader from an external package.

What is the recommended way to do the above in Scala 2.11? Thanks!

解决方案

If your goal is to run external scala classes in runtime, I'd suggest using eval with scala.tools.reflect.ToolBox (it is included in REPL, but for normal usage you have to add scala-reflect.jar):

import scala.reflect.runtime.universe

import scala.tools.reflect.ToolBox

val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()

tb.eval(tb.parse("""println("hello!")"""))

You also can compile files, using tb.compile.

Modified with example: assume you have external file with

class PersonData() {

val field = 42

}

scala.reflect.classTag[PersonData].runtimeClass

So you do

val clazz = tb.compile(tb.parse(src))().asInstanceOf[Class[_]]

val ctor = clazz.getDeclaredConstructors()(0)

val instance = ctor.newInstance()

Additional possibilities are (almost) unlimited, you can get full tree AST and work with it as you want:

showRaw(tb.parse(src)) // this is AST of external file sources

// this is quasiquote

val q"""

class $name {

..$stats

}

scala.reflect.classTag[PersonData].runtimeClass

""" = tb.parse(src)

// name: reflect.runtime.universe.TypeName = PersonData

// stats: List[reflect.runtime.universe.Tree] = List(val field = 42)

println(name) // PersonData

See official documentation for these tricks:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值