获取带有Scala反射API的内部模块的配套对象实例



我用新的Scala反射API实现了Get伴侣对象实例中提到的代码(代码来自这里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
...

你应该有一个module的实例。只有从Spl的镜像才能得到Tensor符号的镜像。

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
}

相关内容

  • 没有找到相关文章