当它是一个嵌套类型时,我无法通过其密封超级特征的反射来恢复单例实例:
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)
对companionSymbol
的调用产生“无”,asModule
失败并带有
scala.ScalaReflectionException: <none> is not a module
(我从Eugene Burmako的this Gist身上取走了身体。)
如果您更改为
sealed trait Foo
case object Bar extends Foo // not nested in object Foo
它有效......
答案 0 :(得分:4)
这还不是答案,但也许是一种解决方法,以防它对任何人都有帮助。
它第一次对我有用,但不是第二次,因为复制结果不是科学吗?:
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
让我查一下:
scala> cm reflectModule res27.asModule
res30: reflect.runtime.universe.ModuleMirror = module mirror for Foo.Bar (bound to null)
scala> .instance
res31: Any = Bar
复制每个命令确实有效,所以不是竞争条件:
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
有些东西被初始化了,但我还没看到什么。我注意到我这样做了:
scala> cm reflect Foo
res13: reflect.runtime.universe.InstanceMirror = instance mirror for Foo$@3a854a76
嗯,这是一个成绩单,在你去的时候调用typeSig:
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
在这种情况下不需要这个:
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
也许我们已经够大了,难以记住this bug.
为了完整性而非关闭:
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)
}