vendredi 12 juin 2020

[Scala][Reflection] calling a method from Annotation using reflection

I have Sample class with Size annotation

case class Sample(
  attr: SomeTypeA
  @Size(value = 50)
  name: SomeTypeB)

This Size annotation is a class that implements AnnotationInterface

trait AnnotationInterface[T] {
  def getValue: T
}

class Size(value: Int) extends StaticAnnotation with AnnotationInterface[Int] {
    override def getValue: Int = value
}

And I have Extractor which is responsible for extracting class members using reflection

class Extractor[A](implicit
    tt: TypeTag[A],
    ct: ClassTag[A]
) { ...extract class members using reflection... } 

Then I would instantiate the extractor like:

val extractor: Extractor[Sample] =
      new Extractor 

Question : How can I call the method getValue: T inside the class Extractor ?





Aucun commentaire:

Enregistrer un commentaire