mardi 9 juillet 2019

Scala reflection, annotations on anonymous inner type

Is it possible to retrieve annotations on anonymous inner type? I have an example below with which I could drill down to the field in the anonymous inner type via 2 routes and with both, no annotations were found on the field 'field'

object ReflectionBug {


  @native
  val outer:Int = 5

  val nested = new {
    @native
    val field:Int = 4
  }

  def main(args:Array[String]) = {
    val mirror = scala.reflect.runtime.universe.runtimeMirror(ReflectionBug.getClass.getClassLoader)
    val t = mirror.classSymbol(ReflectionBug.getClass)
    val members = t.toType.members
    val annotatedMembers = members.filter(_.annotations.nonEmpty)
    //Outer is incuded with annotation native
    val subs = members.filter(_.typeSignature.typeSymbol.isType).map(s => s -> s.typeSignature.members)
    //'field' members under nested member has no annotations


    val mirror2 = scala.reflect.runtime.universe.runtimeMirror(ReflectionBug.nested .getClass.getClassLoader)
    val t2 = mirror2.classSymbol(ReflectionBug.nested .getClass)
    val members2 = t2.toType.members
    val annotates2 = members2.filter(_.annotations.nonEmpty)
    //annotates2 does not contain field
  }
}





Aucun commentaire:

Enregistrer un commentaire