7

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
...
Community
  • 1
  • 1
leo
  • 3,677
  • 7
  • 34
  • 46
  • This question http://stackoverflow.com/questions/11084408/scala-reflection-error-this-is-an-inner-module-use-reflectmodule-on-an-instanc might be related – leo May 08 '13 at 12:38

1 Answers1

3

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
}
senia
  • 37,745
  • 4
  • 88
  • 129
  • Thanks a lot! Unfortunately I do have (in the general case) the reference to SPL not at hand. Respectively the parameter caseclassinstance (which might not be a inner class of SPL) need to be enough in my case to get the companion object. – leo May 13 '13 at 12:33
  • You can't achieve it without mirror for instance of `SPL`. – senia May 13 '13 at 12:38
  • thank you so much! I completely agree that this magic is ugly. please let me know if you find another way to find the spl object. – leo May 13 '13 at 13:35
  • @leo: it's a little bit better with field "$outer". At least we know exactly name. – senia May 13 '13 at 15:52