Search code examples
scalaannotationsscala-reflectscala-3

Access to annotation value in Scala 3.0


I created annotation in scala and used it as follows:

object Main extends App {
  println(classOf[Annotated].getAnnotations.length)

  import scala.reflect.runtime.universe._
  val mirror = runtimeMirror(cls.getClassLoader)

}


final class TestAnnotation extends StaticAnnotation

@TestAnnotation
class Annotated

As it's a Scala annotation it can not be read using getAnnotations on the other hand, scala-reflect dependency isn't available anymore for scala 3.0, so we have no access to runtimeMirror

Is there any alternative solution to read an annotation value in scala?


Solution

  • You don't need runtime reflection (Java or Scala) since information about annotations exists at compile time (even in Scala 2).

    In Scala 3 you can write a macro and use TASTy reflection

    import scala.quoted.*
    
    inline def getAnnotations[A]: List[String] = ${getAnnotationsImpl[A]}
    
    def getAnnotationsImpl[A: Type](using Quotes): Expr[List[String]] = {
      import quotes.reflect.*
      val annotations = TypeRepr.of[A].typeSymbol.annotations.map(_.tpe.show)
      Expr.ofList(annotations.map(Expr(_)))
    }
    

    Usage:

    @main def test = println(getAnnotations[Annotated]) // List(TestAnnotation)
    

    Tested in 3.0.0-RC2-bin-20210217-83cb8ff-NIGHTLY