問題は、タイプがパス依存の場合、TypeTag はパスをエンコードするかどうかです。
インスタンスからの作業は簡単なので:
scala> trait Foo { case class Inner(i: Int) }
defined trait Foo
scala> object Bar extends Foo
defined object Bar
scala> import reflect.runtime._
import reflect.runtime._
scala> import universe._
import universe._
scala> currentMirror reflect Bar.Inner
res0: reflect.runtime.universe.InstanceMirror = instance mirror for Inner
scala> res0.symbol.typeSignature.member(newTermName("apply")).asMethod
res1: reflect.runtime.universe.MethodSymbol = method apply
scala> res0 reflectMethod res1
res3: reflect.runtime.universe.MethodMirror = method mirror for Foo.Inner.apply(i: scala.Int): Foo.this.Inner (bound to Inner)
scala> res3(7)
res4: Any = Inner(7)
タイプから囲んでいるオブジェクトを手動で取得します。
scala> typeOf[Bar.Inner]
res0: reflect.runtime.universe.Type = Bar.Inner
scala> val TypeRef(pre, sym, args) = res0
pre: reflect.runtime.universe.Type = Bar.type
sym: reflect.runtime.universe.Symbol = class Inner
args: List[reflect.runtime.universe.Type] = List()
scala> pre.typeSymbol.asClass.companionSymbol.asModule
res1: reflect.runtime.universe.ModuleSymbol = object Bar
scala> currentMirror reflectModule res1
res2: reflect.runtime.universe.ModuleMirror = module mirror for Bar (bound to null)
scala> res2.instance
res3: Any = Bar$@22a71ac
scala> currentMirror reflect res3
res4: reflect.runtime.universe.InstanceMirror = instance mirror for Bar$@22a71ac
scala> res4.symbol
res5: reflect.runtime.universe.ClassSymbol = object Bar
次に、ドリルダウンして次の場所に戻りInner
ます。
scala> res5.typeSignature.member(newTermName("Inner"))
res7: reflect.runtime.universe.Symbol = object Inner
scala> res7.asModule
res9: reflect.runtime.universe.ModuleSymbol = object Inner
scala> res9.moduleClass
res10: reflect.runtime.universe.Symbol = object Inner
scala> res10.typeSignature
res11: reflect.runtime.universe.Type =
scala.runtime.AbstractFunction1[scala.Int,Foo.this.Inner]
with scala.Serializable {
def <init>(): Foo.this.Inner.type
final override def toString(): java.lang.String
case def apply(i: scala.Int): Foo.this.Inner
case def unapply(x$0: Foo.this.Inner): scala.Option[scala.Int]
private def readResolve(): java.lang.Object
}
scala> res11.member(newTermName("apply"))
res12: reflect.runtime.universe.Symbol = method apply
またはを使用してsym
:
scala> res5.typeSignature.member(sym.name)
res16: reflect.runtime.universe.Symbol = class Inner
scala> res16.asClass.companionSymbol
res17: reflect.runtime.universe.Symbol = object Inner
scala> res17.typeSignature.member(newTermName("apply"))
res18: reflect.runtime.universe.Symbol = method apply
それを使用して:
scala> res4 reflectModule res9
res20: reflect.runtime.universe.ModuleMirror = module mirror for Foo.Inner (bound to Bar$@22a71ac)
scala> res20.instance
res22: Any = Inner
scala> currentMirror reflect res22
res23: reflect.runtime.universe.InstanceMirror = instance mirror for Inner
scala> res23 reflectMethod res18.asMethod
res24: reflect.runtime.universe.MethodMirror = method mirror for Foo.Inner.apply(i: scala.Int): Foo.this.Inner (bound to Inner)
scala> res24(7)
res25: Any = Inner(7)