jeudi 2 mars 2017

Accessing Java Field annotation from scala

I have the following code (most of it is just from another SE question, Scala returns no annotations for a field):

val cls = peripheral.getClass
import scala.reflect.runtime.universe._
val mirror = runtimeMirror(cls.getClassLoader)
val clsSymbol = mirror.staticClass(cls.getCanonicalName)
val decls = clsSymbol.toType.decls.filter(s => s"$s".startsWith("variable "))
for (d <- decls) {
  val classname = classOf[InjectExt].getCanonicalName
  val list = d.annotations.filter(v => v.toString equals classname)
  if (list.nonEmpty) {
    val annotation = list.head
    // what now?
  }
}

where InjectExt is the following Java annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface InjectExt {
    String id() default "auto";
}

This works, but now I get this "annotation" that only has one method: tree(). What can I do to get my InjectExt.id()?
And also, the filtering by toString() really seems like a workaround, but it doesn't look like there's any isVariable or similar that I can use.





Aucun commentaire:

Enregistrer un commentaire