简体   繁体   中英

Recovering a singleton instance via reflection from sealed super trait, when type is nested

I am having trouble recovering a singleton instance via reflection from its sealed super trait, when it is a nested type:

import reflect.runtime.universe._
import reflect.runtime.{currentMirror => cm}

object Foo {
  case object Bar extends Foo
}
sealed trait Foo

def getModule(tpe: Type): Any = {
  val classSymbol  = tpe.typeSymbol.asClass
  val compSymbol   = classSymbol.companionSymbol  // gives <none> !
  val moduleSymbol = compSymbol.asModule
  val moduleMirror = cm.reflectModule(moduleSymbol)
  moduleMirror.instance
}

val subs     = typeOf[Foo].typeSymbol.asClass.knownDirectSubclasses
val tpeOther = subs.last.asType.toType

val res = getModule(tpeOther)
println(res)

The call to companionSymbol yields "none", and asModule fails with

scala.ScalaReflectionException: <none> is not a module

(I had taken the body from this Gist by Eugene Burmako.)


If you change to

sealed trait Foo
case object Bar extends Foo  // not nested in object Foo

it works...

This is not an answer yet, but maybe a work-around, in case it helps anyone.

It worked for me the first time but not the second time, because isn't science all about reproducing the results?:

scala> res22.head
res24: reflect.runtime.universe.Symbol = object Bar

scala> .asClass
res25: reflect.runtime.universe.ClassSymbol = object Bar

scala> .isModule
isModule        isModuleClass   

scala> .isModuleClass
res26: Boolean = true

scala> res25.companionSymbol
res27: reflect.runtime.universe.Symbol = object Bar

scala> .isModule
res28: Boolean = true

And let me just check:

scala> cm reflectModule res27.asModule
res30: reflect.runtime.universe.ModuleMirror = module mirror for Foo.Bar (bound to null)

scala> .instance
res31: Any = Bar

Copying every command exactly works, so not a race condition:

scala> typeOf[Foo].typeSymbol.asClass.knownDirectSubclasses.head
res29: reflect.runtime.universe.Symbol = object Bar

scala> .asClass
res30: reflect.runtime.universe.ClassSymbol = object Bar

scala> .companionSymbol
res31: reflect.runtime.universe.Symbol = object Bar

scala> .asModule
res32: reflect.runtime.universe.ModuleSymbol = object Bar

Something got initialized, but I don't see yet what. I noticed I did this:

scala> cm reflect Foo
res13: reflect.runtime.universe.InstanceMirror = instance mirror for Foo$@3a854a76

Well, here is a transcript calling typeSig on everything as you go:

scala> import reflect.runtime.universe._
import reflect.runtime.universe._

scala> import reflect.runtime.{currentMirror => cm}
import reflect.runtime.{currentMirror=>cm}

scala> :pa
// Entering paste mode (ctrl-D to finish)

sealed trait Foo
object Foo {
  case object Bar extends Foo
}

// Exiting paste mode, now interpreting.

defined trait Foo
defined object Foo

scala> typeOf[Foo].typeSymbol
res0: reflect.runtime.universe.Symbol = trait Foo

scala> res0.typeSignature
res1: reflect.runtime.universe.Type = scala.AnyRef

scala> res0.asClass
res2: reflect.runtime.universe.ClassSymbol = trait Foo

scala> .companionSymbol
res3: reflect.runtime.universe.Symbol = object Foo

scala> .typeSignature
res4: reflect.runtime.universe.Type = Foo.type

scala> res2.knownDirectSubclasses.head.asClass
res5: reflect.runtime.universe.ClassSymbol = object Bar  

This one isn't necessary in this case:

scala> res5.typeSignature
res6: reflect.runtime.universe.Type = 
scala.AnyRef
        with Foo
        with scala.Product
        with scala.Serializable {
  def <init>(): Foo.Bar.type
  override def productPrefix: java.lang.String
  def productArity: scala.Int
  def productElement(x$1: scala.Int): scala.Any
  override def productIterator: Iterator[scala.Any]
  def canEqual(x$1: scala.Any): scala.Boolean
  override def hashCode(): scala.Int
  override def toString(): java.lang.String
  private def readResolve(): java.lang.Object
}

scala> res5.companionSymbol
res7: reflect.runtime.universe.Symbol = object Bar

scala> res7.isModule
res8: Boolean = true

scala> cm reflectModule res7.asModule
res9: reflect.runtime.universe.ModuleMirror = module mirror for Foo.Bar (bound to null)

scala> .instance
res10: Any = Bar

Maybe we're old enough to remember this bug.

For completeness but not closure:

object Driller extends App {
  def getModule(sym: Symbol): Any = {
    val classSymbol = sym.asClass
    val compSymbol = classSymbol.companionSymbol // gives <none> !
    val moduleSymbol = compSymbol.asModule
    val moduleMirror = cm.reflectModule(moduleSymbol)
    moduleMirror.instance
  }
  val foo = typeOf[Foo].typeSymbol.asClass
  foo.typeSignature
  foo.companionSymbol.typeSignature
  val bar = foo.knownDirectSubclasses.head.asClass
  val res = getModule(bar)
  println(res)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM