【发布时间】:2020-04-21 21:37:42
【问题描述】:
这就是问题所在。
trait TestTrait[T, R] extends (T => R)
// Class implementing TestTrait. This is one class, there are a number of class implementing TestTrait
class TestClass(val input: Map[String, String])extends ConfigurableDomainExtractor[String, String] {
override def apply(value: String): String = ???
}
// Companion Object
object TestClass {
def apply(properties: Map[String, String]): TestClass = TestClass(properties)
}
我想做的是在 util 类中定义一个方法让我们说
class CreateInstance {
def getInstance(fullyQualifiedClassName: String, properties: Map[String, String]): TestTrait = ???
// fullyQualifiedClassName is the name of the class that needs to be instantiated using its companion object. It can be TestClass or any class implementing TestTrait
// properties is the map that needs to be pass to the apply method of the companion object.
}
当传递类名时,将首先获取一个伴随对象,然后在传递映射的伴随对象上调用apply以生成类实例,从而生成同一类的对象。
我知道我们在 scala 中有反射,我尝试了几件事但无济于事。以下是我尝试过的几件事。
import scala.reflect.runtime.universe
def getInstance(fullyQualifiedClassName: String, properties: Map[String, String]): TestTrait = {
val runtimeMirror = universe.runtimeMirror(getClass.getClassLoader)
val module = runtimeMirror.staticModule(fullyQualifiedClassName)
val obj = runtimeMirror.reflectModule(module)
obj.instance
.asInstanceOf[TestTrait[String, String]]
.apply(properties)
.asInstanceOf[TestTrait[String, String]]
}
谁能帮我完成def getInstance 方法。 ?
【问题讨论】:
-
在这里使用 Java 反射可能就足够了...?这要容易得多。
-
@SethTisue 如何使用 Java 反射 API?
标签: scala reflection companion-object scala-reflect