是否可以检索匿名内部类型的注释?
我在下面有一个示例,可以通过2条路由向下钻取匿名内部类型的字段,而在这两种情况下,在字段field
上都找不到注释
object ReflectionBug {
@native
val outer:Int = 5
val nested = new {
@native
val field:Int = 4
}
def main(args:Array[String]) = {
val mirror = scala.reflect.runtime.universe.runtimeMirror(ReflectionBug.getClass.getClassLoader)
val t = mirror.classSymbol(ReflectionBug.getClass)
val members = t.toType.members
val annotatedMembers = members.filter(_.annotations.nonEmpty)
//Outer is incuded with annotation native
val subs = members.filter(_.typeSignature.typeSymbol.isType).map(s => s -> s.typeSignature.members)
//'field' members under nested member has no annotations
val mirror2 = scala.reflect.runtime.universe.runtimeMirror(ReflectionBug.nested .getClass.getClassLoader)
val t2 = mirror2.classSymbol(ReflectionBug.nested .getClass)
val members2 = t2.toType.members
val annotates2 = members2.filter(_.annotations.nonEmpty)
//annotates2 does not contain field
}
}
答案 0 :(得分:4)
val nested
是什么类型?结构类型为AnyRef { val field: Int }
val nested: { val field: Int } = new {
@native
val field:Int = 4
}
这就是typeSignature
失去注释的原因。
您必须找到成员nested
,握住它的右手并使用它。