在Scala 2.10中,我如何从字符串生成一个类(可能使用Toolbox API),以便稍后使用Scala反射进行实例化?
关于编译工具箱,它们只能运行表达式并返回值,而不能返回生成的类或文件/字节数组的编译结果。
但是你仍然可以实现你想要的功能,因为在Scala中使用隐式值非常容易从类型级别转到值级别:
编辑。 在2.10.0-RC1中,ToolBox
的某些方法已更名。 parseExpr
现在称为parse
,而runExpr
现在称为eval
。
scala> import scala.reflect.runtime._ // requires scala-reflect.jar
// in REPL it's implicitly added
// to the classpath
// but in your programs
// you need to do this on your own
import scala.reflect.runtime
scala> val cm = universe.runtimeMirror(getClass.getClassLoader)
cm @ 41d0fe80: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader...
scala> import scala.tools.reflect.ToolBox // requires scala-compiler.jar
// in REPL it's implicitly added
// to the classpath
// but in your programs
// you need to do this on your own
import scala.tools.reflect.ToolBox
scala> val tb = cm.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@3a962da5
scala> tb.runExpr(tb.parseExpr("class C; scala.reflect.classTag[C].runtimeClass"))
res2: Any = class __wrapper$1$f9d572ca0d884bca9333e251c64e980d$C$1
更新 #1. 如果你不需要一个java.lang.Class,只需要实例化编译后的类,那么你可以在提交给runExpr
的字符串中直接写new C
。
更新 #2. 也可以使用自定义映射从变量名到运行时值来调用runExpr
。例如:
scala> val build = scala.reflect.runtime.universe.build
build: reflect.runtime.universe.BuildApi = scala.reflect.internal.BuildUtils$BuildImpl@50d5afff
scala> val x = build.setTypeSignature(build.newFreeTerm("x", 2), typeOf[Int])
x: reflect.runtime.universe.FreeTermSymbol = free term x
scala> tb.runExpr(Apply(Select(Ident(x), newTermName("$plus")), List(Literal(Constant(2)))))
res0: Any = 4
在这个例子中,我创建了一个自由项,它的值为2(值不一定是原始值——也可以是您的自定义对象),并将标识符绑定到它上面。然后,在编译和运行工具箱执行的代码中直接使用该值。
<mirror>.classSymbol(<instance of java.lang.Class>)
,其中 <mirror> =scala.reflect.runtime.universe.runtimeMirror(<instance of java.lang.Class>.getClassLoader)
。然后您将获得一个Scala反射符号,可以使用Scala反射API进行检查。 - Eugene Burmako