2
case class FieldName(field: String) extends scala.annotation.StaticAnnotation
@FieldName("foo") trait Foo
import scala.reflect.runtime.universe._

symbolOf[Foo].annotations.head
// ann: Annotation = FieldName("type")

如何将注释作为FieldName对象访问?文档提到了 tree.children.tail,但是没有类型。

4

1 回答 1

3

如果你真的想要FieldName我能想到的最好的实例是使用ToolBox

scala> case class FieldName(field: String) extends scala.annotation.StaticAnnotation
defined class FieldName

scala> @FieldName("foo") trait Foo
defined trait Foo

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

scala> val annotation = symbolOf[Foo].annotations.head
annotation: reflect.runtime.universe.Annotation = FieldName("foo")

scala> import scala.tools.reflect.ToolBox
import scala.tools.reflect.ToolBox

scala> val tb = runtimeMirror(getClass.getClassLoader).mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@1b26a499

scala> tb.eval(tb.untypecheck(annotation.tree)).asInstanceOf[FieldName]
res10: FieldName = FieldName(foo)

.tree.children.tail您可以访问传递给的参数,而FieldName无需创建实际实例。

scala> annotation.tree.children.tail.map{ case Literal(Constant(field)) => field } 
res11: List[Any] = List(foo)

如果您只想要所有FieldName注释并提取它们的值,您可以这样做:

scala> val fields = symbolOf[Foo].annotations.withFilter( 
     |   a => a.tree.tpe <:< typeOf[FieldName] 
     | ).flatMap( 
     |   a => a.tree.children.tail.map{ case Literal(Constant(field)) => field } 
     | )
fields: List[Any] = List(foo)
于 2017-03-09T15:44:02.640 回答