I have a case class like:
case class Foo (@Annotation bar: String)
I'd like to be able to get access to that annotation and any of its information that is stored
I can get the case accessors using scala reflection (using 2.11.8) with
val caseAccessors =
universe.typeTag[T].
tpe.
decls.
filter(_.isMethod).
map(_.asMethod).
filter(_.isCaseAccessor)
But when I try and access .annotations
on them there is nothing. I realize that the annotation is technically on the constructor parameter, but how do I get to it?
Your
@Annotation
will be on both constructor parameter and companion objectapply
method, and you can find those by name. I don't think there exist specific methods to filter out constructor / primary constructor / companion object factory method. Both these should work:(although I'm on Scala 2.11.8 and there's no
decls
but there'sdeclarations
)If you want to put your annotations on fields or getters, use
scala.annotation.meta
package:In this case your code will work (if you change
T
toFoo
intypeTag[T]
)See the doc for
scala.annotation.meta