Zaimplementowałem kod wspomniany w Get companion object instance with new Scala reflection API (kod stąd: https://gist.github.com/xeno-by/4985929).Pobierz instancję obiektu towarzyszącego modułu wewnętrznego za pomocą interfejsu API do refleksji Scala
Działa to dobrze dla każdej standardowej klasy klas przypadków. Niestety w niektórych klasach projektu otrzymuję wyjątek: scala.ScalaReflectionException: object Tensor is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror
Wyjątkiem jest całkiem jasne, więc starałem się zmienić swój kod w następujący sposób:
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
}
}
Ale teraz dostaję scala.ScalaReflectionException: expected a member of class Tensor, you provided object Prototype2.SPL.SPL_Exp.Tensor
a ja nie dowiedzieć się, jak zmień kod, aby to naprawić. Każda pomoc jest bardzo cenna!
Aktualizacja: I dostarczyć niektóre kodu dla lepszej powtarzalności:
scala> trait SPL {
| case class Tensor()
| }
defined trait SPL
scala> val s = new SPL {}
s: SPL = [email protected]
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
...
To pytanie http://stackoverflow.com/questions/11084408/scala-reflection-error-this-is-an-inner-module-use-reflectmodule-on-an-instanc może być związane – leo