Tuesday 15 September 2015

Scala 2.11 reflection and annotations (Java) with parameters -



Scala 2.11 reflection and annotations (Java) with parameters -

i have simple class-level annotation written in java:

@target({elementtype.type}) @retention(retentionpolicy.runtime) public @interface collection { string name(); }

used like:

@collection(name="mytable") case class foo(...)

i need introspect classes in scala 2.11 obtain value of name parameter. how can info? i'm here:

val sym = currentmirror.classsymbol(class.forname(fullname)) val anno = sym.annotations.head val annotype = anno.tree.tpe // can this...works println(anno.tree.children.tail) // prints list(name = "mytable")

i'm close! can see name parameter , value doesn't seem accessible map or friendly. how can value of annotation's parameter?

the tree api implements product elements out, kind of hacky demonstration, can out element:

println(anno.tree.children.last.productelement(1)) // prints "mytable"

scala reflection annotations

No comments:

Post a Comment