2

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?

Community
  • 1
  • 1
user2036540
  • 61
  • 2
  • 3
  • 1) The question is not about how to annotate a class as in How to create annotations and get them in scala. 2) Danyel answer is getting closer, I think. But I must admit for a basic coder like me I would never have guessed that to get the annotation would be so complicated. As I understand they are meant to help "getAnnotation" would do me, but to incant the universe.type#Annotation, well come on... Still cannot determine if version has the annotation or not. – user2036540 Feb 03 '13 at 10:47
  • Did you actually read it? The question I linked to explains why it does not work and how you can fix it by declaring the annotation in java, which was the only solution pre 2.10, and is still probably what many people do in 2.10. If you are using java reflection, it is also the **only** solution. – Régis Jean-Gilles Feb 03 '13 at 10:50

1 Answers1

1

With this snippet, you'll get very close to what your goal is, I guess.

import scala.reflect.runtime.universe
def annotationsOf[T: universe.TypeTag](obj: T) = {
    universe.typeOf[T].members.foldLeft(Nil: List[universe.type#Annotation]) {
        (xs, x) => x.annotations ::: xs
    }
}

You would call that in your example like this:

val testObj = TestClass(version = Some("1"))
println(annotationsOf(testObj))

If you want to know more, you should have a look into TypeTag and, specifically, what the members in my snippet does and what methods it provides.

Regards,
Danyel

Danyel
  • 2,180
  • 18
  • 32