I implemented the code mentioned in Get companion object instance with new Scala reflection API (code from here 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
}
}
This works fine for any standard class of case classes. Unfortunately in some classes of the project I get an Exception: scala.ScalaReflectionException: object Tensor is an inner module, use reflectModule on an InstanceMirror to obtain its ModuleMirror
The exception is pretty clear, so I tried to change my code as follows:
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
}
}
But now I get a scala.ScalaReflectionException: expected a member of class Tensor, you provided object Prototype2.SPL.SPL_Exp.Tensor
and I did not find out how to change the code to fix this. Any help is greatly appreciated!
Update: I provide some code for better reproducibility:
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
...
You should have an instance of module. You can get mirror for Tensor
symbol only from mirror of 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
You could get an instance of Spl
using a bit of ugly magic:
val outer =
t.
getClass.
getFields.
find(_.getName == """$outer""").
get.
get(t)
You should not do so until you can.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With