Possible Duplicate:
How to create annotations and get them in scala
other examples get the annotation of a annotated class. What I would like to do is to get the annotation of a class field (version in this test), something like this:
import java.lang.annotation.{RetentionPolicy, Retention}
import annotation.meta.field
class AttribField() extends scala.annotation.StaticAnnotation
object MyTypes { type Attrib = AttribField @field }
import MyTypes._
case class TestClass(@Retention(RetentionPolicy.RUNTIME)
@Attrib version: Option[String])
object TestMe {
def main(args: Array[String]) {
val testObj = new TestClass(version = Some("1"))
testObj.getClass.getDeclaredFields.foreach(field => {
field.setAccessible(true)
println("field: name=" + field.getName + " value=" + field.get(testObj))
field.getAnnotations.foreach(x => println("x="+x)) // nothing in here
field.getDeclaredAnnotations.foreach(x => println("y="+x)) // nothing in here
// does not even compile
// field.getAnnotation(classOf[AttribField])
})
}
}
I want to test if there is a certain annotation, then branch. But I cannot pass go. There must be something fundamental that I missed, but what?