scala 在Scala中通过字符串名称获取对象实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1913092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting object instance by string name in scala
提问by Dave
I need the object (or "singleton object" or "companion object"... anything but the class) defined by a string name. In other words, if I have:
我需要由字符串名称定义的对象(或“单个对象”或“伴随对象”......除了类之外的任何东西)。换句话说,如果我有:
package myPackage
object myObject
...then is there anything like this:
...那么有没有这样的:
GetSingletonObjectByName("myPackage.myObject") match {
case instance: myPackage.myObject => "instance is what I wanted"
}
采纳答案by Thomas Jung
Scala is still missing a reflection API. You can get the an instance of the companion object by loading the companion object class:
Scala 仍然缺少反射 API。您可以通过加载伴生对象类来获取伴生对象的实例:
import scala.reflect._
def companion[T](implicit man: Manifest[T]) : T =
man.erasure.getField("MODULE$").get(man.erasure).asInstanceOf[T]
scala> companion[List$].make(3, "s")
res0: List[Any] = List(s, s, s)
To get the untyped companion object you can use the class directly:
要获得无类型的伴生对象,您可以直接使用该类:
import scala.reflect.Manifest
def companionObj[T](implicit man: Manifest[T]) = {
val c = Class.forName(man.erasure.getName + "$")
c.getField("MODULE$").get(c)
}
scala> companionObj[List[Int]].asInstanceOf[List$].make(3, "s")
res0: List[Any] = List(s, s, s)
This depends on the way scala is mapped to java classes.
这取决于scala 映射到java 类的方式。
回答by Nguyen Duc Dung
In scala 2.10 we can do like this
在 Scala 2.10 中我们可以这样做
import scala.reflect.runtime.universe
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val module = runtimeMirror.staticModule("package.ObjectName")
val obj = runtimeMirror.reflectModule(module)
println(obj.instance)
回答by psp
Adjustment to Thomas Jung's answer above: you would do better to say companion[List.type] because a) this should be a stable way to refer to it, not dependant on the name mangling scheme and b) you get the unerased types.
对上面 Thomas Jung 的回答进行调整:你最好说伴侣 [List.type],因为 a) 这应该是一种稳定的引用方式,不依赖于名称修改方案,并且 b) 你得到了未擦除的类型。
def singleton[T](implicit man: reflect.Manifest[T]) = {
val name = man.erasure.getName()
assert(name endsWith "$", "Not an object: " + name)
val clazz = java.lang.Class.forName(name)
clazz.getField("MODULE$").get(clazz).asInstanceOf[T]
}
scala> singleton[List.type].make(3, "a")
res0: List[java.lang.String] = List(a, a, a)
回答by Daniel C. Sobral
Barring reflection tricks, you can't. Note, for instance, how the method companionis defined on Scala 2.8 collections -- it is there so an instance of a class can get the companion object, which is otherwise not possible.
除了反射技巧,你不能。请注意,例如,该方法companion是如何在 Scala 2.8 集合上定义的——它在那里是这样一个类的实例可以获得伴随对象,否则这是不可能的。

