在Scala中访问注释值

Tra*_*kel 8 reflection scala

TL; DR:基本上,我正在寻找Java的Scala等价物:

(MyAnnotation) Thing.getClass().getAnnotations()[0]
Run Code Online (Sandbox Code Playgroud)

尽管我可以根据他们的类型愉快地发现注释和查询,但我似乎无法从scala.reflect.runtime.universe.Annotation我的实际类型中获取.

scala> // Declare an annotation (it seems StaticAnnotation means runtime
scala> // retention)
scala> case class MyAnnotation(x: Int, y: String) extends scala.annotation.StaticAnnotation
defined class MyAnnotation

scala> // Make a thing decorated with MyAnnotation
scala> @MyAnnotation(x=5, y="cool") case class Thing()
defined class Thing

scala> // Look at the annotation on the Thing...the runtime clearly
scala> // understands the values on it
scala> val annotation = scala.reflect.runtime.universe.typeOf[Thing].typeSymbol.asClass.annotations(0)
annotation: reflect.runtime.universe.Annotation = MyAnnotation(5, "cool")

scala> // I can sort of get at the values by index, which isn't terribly
scala> // safe
scala> annotation.scalaArgs(0)
res0: reflect.runtime.universe.Tree = 5

scala> // And what is a Tree here anyway? It certainly isn't a String (or
scala> // Int). I just want the value!
scala> annotation.scalaArgs(1)
res1: reflect.runtime.universe.Tree = "cool"

scala> // But how do I get at those values programatically?
scala> annotation.asInstanceOf[MyAnnotation]
java.lang.ClassCastException: scala.reflect.internal.AnnotationInfos$CompleteAnnotationInfo cannot be cast to MyAnnotation
        at .<init>(<console>:13)
        at .<clinit>(<console>)
        at .<init>(<console>:7)
        at .<clinit>(<console>)
        at $print(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:734)
        at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:983)
        at scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:573)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:604)
        at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:568)
        at scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:760)
        at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:805)
        at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:717)
        at scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:581)
        at scala.tools.nsc.interpreter.ILoop.innerLoop$1(ILoop.scala:588)
        at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:591)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:882)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
        at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:837)
        at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
        at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:837)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:83)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:105)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Run Code Online (Sandbox Code Playgroud)

关于这一切的欢乐部分是即使我想要也不能使用传统的Java方法,因为Scala不再费心去填充数组getAnnotations()了.

scala> Thing.getClass.getAnnotations.length
res2: Int = 0
Run Code Online (Sandbox Code Playgroud)

有关

我认为我想要的是"在运行时实例化类型"一节中的"反射概述",但我不明白为什么我必须跳过这么多箍才能获得注释中的值.在Java中,价值只是一个抛弃.

问题"查看Scala反射中的注释"似乎有关,但问题是从2011年开始,适用于Scala 2.9.我使用的是2.10,据我所知,从那时起,大量的反射工作方式发生了变化.

小智 6

不是java等价物,但是使用scala 2.11.6,这可以从注释中提取值:

case class MyAnnotationClass(id: String) extends scala.annotation.StaticAnnotation

val myAnnotatedClass: ClassSymbol = u.runtimeMirror(Thread.currentThread().getContextClassLoader).staticClass("MyAnnotatedClass")
val annotation: Option[Annotation] = myAnnotatedClass.annotations.find(_.tree.tpe =:= u.typeOf[MyAnnotationClass])
val result = annotation.flatMap { a =>
  a.tree.children.tail.collect({ case Literal(Constant(id: String)) => DoSomething(id) }).headOption
}
Run Code Online (Sandbox Code Playgroud)

我知道你可能已经做过,但以防万一它可以帮助别人:)


Eug*_*ako 5

在他们当前的形式中,Scala注释试图结合Java兼容性(这意味着只有常量参数和注释中允许的非常有限数量的语言结构)和最终的灵活性(这意味着允许在注释中可以想象的任何东西).

这表现在ClassfileAnnotation(兼容性)与StaticAnnotation(灵活性)的区别.根据这个想法,可以选择具有有限注释的Java风格反射作为对象和具有完全灵活注释的Scala风格反射仅作为抽象语法树可用(注意我们不能自动将静态注释转换为运行时对象) ,因为存储在此类注释中的代码可能包含任意Scala表达式,这使得评估它们非常困难).

不幸的是,这个理想化的图片被classfile注释不支持运行时保留这一事实打破了:https://issues.scala-lang.org/browse/SI-32,这意味着实际上并没有选择 -目前仅支持Scala风格的反射.希望有一天我们能够修复SI-32,但是我并没有意识到任何正在努力使它运转起来.

几个月前,我想实现类似的东西scala.reflect.Annotation.eval,如果可能的话,会采用Scala风格的注释并对其进行评估.然而,在我们的一次反思会议讨论之后,我们决定反对它,因为除了不幸的非一般性之外,这个API也会给编译时反射带来麻烦(在Scala中与运行时反射统一).

这已在https://issues.scala-lang.org/browse/SI-6423上记录为问题,并在https://groups.google.com/forum/#!topic/scala-internals/8v2UL上进行了讨论-LR9yY,但目前没有具体的改进计划.希望Project Palladium将在这里提供帮助,因为其核心组件之一是Scala解释器,它将提供必要的通用性以支持Annotation.eval.