scala eval

package com.jason

case class JJ(d: Double*)

object Ss {
  def main(args: Array[String]): Unit = {
    import scala.reflect.runtime.universe._
    import scala.tools.reflect.ToolBox

    val code = """(x: String) => x.replace("$", "")"""
    val toolbox = runtimeMirror(Ss.getClass.getClassLoader).mkToolBox()
    val func = toolbox.eval(toolbox.parse(code)).asInstanceOf[String => String]
    println(func("$10.50")) // prints "10.50"

    val n = 10
    val code2 = s"println($n)"
    toolbox.eval(toolbox.parse(code2))

    val list = "List(1,2,3)"
    val ll = toolbox.eval(toolbox.parse(list)).asInstanceOf[List[Int]]
    println(ll)

    val mm = "new com.jason.MM()" //一定要写package.classname 否则会报错not found: type MM
    val mc = toolbox.eval(toolbox.parse(mm)).asInstanceOf[MM]
    println(mc.x)
  }
}

 需要有如下依赖

<dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-compiler</artifactId>
            <version>2.11.8</version>
        </dependency>

 

posted @ 2018-09-29 16:45  生心无住  阅读(959)  评论(1编辑  收藏  举报