如何在运行时使用Scala的反射列出带有自定义注释的所有字段?

Pet*_*lák 6 reflection annotations scala scala-2.10

我有一个自定义注释

class MyProperty(val name: String)
  extends annotation.StaticAnnotation; // or should I extend something else?
Run Code Online (Sandbox Code Playgroud)

对于给定的类,如何列出具有此批注的所有字段?我正在寻找像(只是猜测)的东西:

def listProperties[T: ClassTag]: List[(SomeClassRepresentingFields,MyProperty)];
Run Code Online (Sandbox Code Playgroud)

gou*_*ama 12

这可以TypeTag通过过滤members输入类型来完成:

import reflect.runtime.universe._

def listProperties[T: TypeTag]: List[(TermSymbol, Annotation)] = {
  // a field is a Term that is a Var or a Val
  val fields = typeOf[T].members.collect{ case s: TermSymbol => s }.
    filter(s => s.isVal || s.isVar)

  // then only keep the ones with a MyProperty annotation
  fields.flatMap(f => f.annotations.find(_.tpe =:= typeOf[MyProperty]).
    map((f, _))).toList
}
Run Code Online (Sandbox Code Playgroud)

然后:

scala> class A { @MyProperty("") val a = 1 ; @MyProperty("a") var b = 2 ; 
  var c: Long = 1L }
defined class A

scala> listProperties[A]
res15: List[(reflect.runtime.universe.TermSymbol, reflect.runtime.universe.Annotation)]
  = List((variable b,MyProperty("a")), (value a,MyProperty("")))
Run Code Online (Sandbox Code Playgroud)

这不会直接给你一个MyProperty但是a universe.Annotation.它有一个scalaArgs方法,如果你需要用它做什么,你可以访问它作为树的参数.