如何使用Scala反射来检测从ToolBox解析的包中的代码?

时间:2016-11-11 21:43:05

标签: scala scala-2.11 scala-reflect

以下简单示例(在Scala 2.11.8中运行):

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

object Main extends App {

  val toolbox = currentMirror.mkToolBox()

  val tree = toolbox.parse {
    """
    package foo.bar {
       trait Baz
    }
    """
  }
  println(tree)

  val checkedTree = toolbox.typecheck(tree)
  println(tree)
}

会在toolbox.typecheck(tree)行产生以下异常:

Exception in thread "main" java.lang.AssertionError: assertion failed: value <local <expression-owner>>
    at scala.reflect.internal.Symbols$Symbol.newPackage(Symbols.scala:310)
    at scala.tools.nsc.typechecker.Namers$Namer.createPackageSymbol(Namers.scala:381)
    at scala.tools.nsc.typechecker.Namers$Namer.createPackageSymbol(Namers.scala:374)
    ...

如何获得类型检查的AST树?

0 个答案:

没有答案