I defined an annotation class with a List field in Scala:
class TestAnnotation(a: String, b: List[String]) extends StaticAnnotation
And some classes using the annotation, e.g.:
@TestAnnotation("test", List("b1", "b2"))
class TestA
Now I want to get the class annotation values, a and b, by reflection
import scala.reflect.runtime.universe
universe
  .typeOf[TestA]
  .typeSymbol
  .annotations
  .find(_.tree.tpe =:= universe.typeOf[TestAnnotation])
  .map(_.tree).foreach(t => {
    // Match Exceptions happen there! How to match the List fields?
    val universe.Apply(_, universe.Literal(universe.Constant(a: String)) :: universe.Literal(universe.Constant(b: List[String])) :: Nil) = t 
    println(a)
    println(b)
  })
How to get the List value from deconstructing t by matching it against universe.Apply?
Aucun commentaire:
Enregistrer un commentaire