得到一个内模件的同伴对象实例与斯卡拉反射API(Get the companion object i

2019-09-02 06:30发布

我实现中提到的代码获取随对象实例与新斯卡拉反射API (从这里代码https://gist.github.com/xeno-by/4985929 )。

object Reflection {
    def getCompanionObject(caseclassinstance:Product):Any = {
        import scala.reflect.runtime.{currentMirror => cm}
        val classSymbol = cm.classSymbol(caseclassinstance.getClass)
        val moduleSymbol = classSymbol.companionSymbol.asModule
        val moduleMirror = cm.reflectModule(moduleSymbol)
        moduleMirror.instance
    }
}

这对于任何标准类case类的工作正常。 不幸的是该项目的一些课程我得到一个异常: scala.ScalaReflectionException: object Tensor is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror唯一的例外是很清楚,所以我试图改变我的代码如下:

object Reflection {
    def getCompanionObject(caseclassinstance:Product):Any = {
        import scala.reflect.runtime.{currentMirror => cm}
        val classSymbol = cm.classSymbol(caseclassinstance.getClass)
        val moduleSymbol = classSymbol.companionSymbol.asModule
        val instanceMirror = cm.reflect(caseclassinstance)
        val moduleMirror = instanceMirror.reflectModule(moduleSymbol)
        moduleMirror.instance
    }
}

但现在我得到一个scala.ScalaReflectionException: expected a member of class Tensor, you provided object Prototype2.SPL.SPL_Exp.Tensor和我没有找到如何改变代码解决这个问题。 任何帮助是极大的赞赏!

更新:我提供更好的可重复性一些代码:

scala> trait SPL {
     | case class Tensor()
     | }
defined trait SPL

scala> val s = new SPL {}
s: SPL = $anon$1@165f5a4

scala> val t = s.Tensor()
t: s.Tensor = Tensor()

scala> object Reflection { /* as in the first code snippet*/}
defined module Reflection

scala> Reflection.getCompanionObject(t)
scala.ScalaReflectionException: object Tensor is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror
...

scala> object Reflection { /* as in the second code snippet*/}
defined module Reflection

scala> Reflection.getCompanionObject(t)
scala.ScalaReflectionException: expected a member of class Tensor, you provided object SPL.Tensor
...

Answer 1:

你应该有模块的一个实例。 你可以得到镜像Tensor只从镜子象征Spl

trait Spl {
  case class Tensor(s: String)
}

val spl = new Spl {}

val t = spl.Tensor("T")

// mirror for module spl
val moduleMirror = cm.reflect(spl)
// class symbol for Tensor
val instanceSymbol = cm.classSymbol(t.getClass)
// symbol for companion object
val companionSymbol = instanceSymbol.companionSymbol.asModule
// mirror for companion object symbol in spl module mirror
val companionMirror = moduleMirror.reflectModule(companionSymbol)

scala> companionMirror.instance
res0: Any = Tensor

你可以得到的一个实例Spl使用有点丑陋的法宝:

val outer =
  t.
  getClass.
  getFields.
  find(_.getName == """$outer""").
  get.
  get(t)

你不应该这样做,直到你可以。

def getCompanionObject(t: Product):Any = {
  import scala.reflect.runtime.{currentMirror => cm}
  val outerField = t.getClass.getFields.find(_.getName == """$outer""")
  val moduleMirror = outerField.map{ _.get(t) }.map{ cm.reflect(_) }
  val instanceSymbol = cm.classSymbol(t.getClass)
  val companionSymbol = instanceSymbol.companionSymbol.asModule
  val companionMirror =
    moduleMirror.
      map{ _.reflectModule(companionSymbol) }.
      getOrElse{ cm.reflectModule(companionSymbol) }
  companionMirror.instance
}


文章来源: Get the companion object instance of a inner modul with the Scala reflection API