如何以编程方式编译和运行 Scala 代码 [英] How to compile and run Scala code programmatically

查看:63
本文介绍了如何以编程方式编译和运行 Scala 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我想即时编译并运行它.

I have the following code and I would like to compile it on the fly and run it.

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

到目前为止,我已经尝试过如下所示:

So far I have tried something like below:

import scala.reflect.runtime.currentMirror
import scala.tools.reflect.ToolBox

object MainScala {
  def main(args: Array[String]): Unit = {
    val toolbox = currentMirror.mkToolBox()
    val source =
      """
        |object HelloWorld {
        |  def main(args: Array[String]): Unit = {
        |    println("Hello, world!")
        |  }
        |}
        |""".stripMargin
    val tree = toolbox.parse(source)
    val binary = toolbox.compile(tree)
    var c = binary.getClass
    val m = c.getMethod("main", classOf[Array[String]])
    val params = Array("Apple", "Banana", "Orange")
    m.invoke(null, null)
  }
}

toolbox.compile(tree) 之后,我无法获得编译代码的 Class 对象.

After toolbox.compile(tree) I am not able to get the Class object of the compiled code.

推荐答案

如果您查看 binary 的类型,您会看到它是 () =>任何.因此,当您询问 .getClass 时,您实际上会得到 Function1 的子类,它没有 .main.

If you look at the type of binary you'll see it's () => Any. So when you ask .getClass you actually get a subclass of Function1, which doesn't have .main.

工具箱不应该那样使用.

Toolbox isn't supposed to be used like that.

https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/reflect/ToolBox.scala#L120-L129

https://docs.scala-lang.org/overviews/reflection/symbols-trees-types.html#tree-creation-via-parse-on-toolboxes

试试

val source =
  """
    |object HelloWorld {
    |  def main(args: Array[String]): Unit = {
    |    println("Hello, world!")
    |  }
    |}
    |
    |HelloWorld.main(Array())
    |""".stripMargin
val tree = toolbox.parse(source)
val binary = toolbox.compile(tree)
binary() // Hello, world!

val params = """Array("Apple", "Banana", "Orange")"""
val source =
  s"""
    |object HelloWorld {
    |  def main(args: Array[String]): Unit = {
    |    println(args.toList)
    |  }
    |}
    |
    |HelloWorld.main($params)
    |""".stripMargin
val tree = toolbox.parse(source)
val binary = toolbox.compile(tree)
binary() // List(Apple, Banana, Orange)

import scala.reflect.runtime.universe._
val params = q"""Array("Apple", "Banana", "Orange")"""
val tree =
  q"""
    object HelloWorld {
      def main(args: Array[String]): Unit = {
        println(args.toList)
      }
    }

    HelloWorld.main($params)
    """
val binary = toolbox.compile(tree)
binary() // List(Apple, Banana, Orange)

val params = """Array("Apple", "Banana", "Orange")"""
val source =
  """
    |object HelloWorld {
    |  def main(args: Array[String]): Unit = {
    |    println(args.toList)
    |  }
    |}
    |""".stripMargin
val tree = toolbox.parse(source)
val symbol = toolbox.define(tree.asInstanceOf[ImplDef])
val params1 = toolbox.parse(params)
val tree1 = q"$symbol.main($params1)"
val binary = toolbox.compile(tree1)
binary() // List(Apple, Banana, Orange)

这篇关于如何以编程方式编译和运行 Scala 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆