scala - 如何在 Scala 中定义 @interface?

标签 scala annotations

如何在 Scala 中创建@interface?老实说,我觉得问这个问题很愚蠢,但我在任何地方都找不到这个语法。我知道您可以使用它们,但是如何在 Scala 中实际定义新的?

Java:

public @interface MyAnnotation { }

斯卡拉:

???

最佳答案

此答案基于 Scala 2.8。

// Will be visible to the Scala compiler, but not in the class file nor at runtime.
// Like RetentionPolicy.SOURCE
final class MyAnnotation extends StaticAnnotation

// Will be visible stored in the annotated class, but not retained at runtime.
// This is an implementation restriction, the design is supposed to support
// RetentionPolicy.RUNTIME
final class MyAnnotation extends ClassfileAnnotation

有关完整详细信息,请参阅 Scala Reference 中的第 11 节“用户定义的注释” 例如,参见:@tailrec .

更新编译器警告说明了这一点:

>cat test.scala
final class MyAnnotation extends scala.ClassfileAnnotation

@MyAnnotation
class Bar

>scalac test.scala
test.scala:1: warning: implementation restriction: subclassing Classfile does not
make your annotation visible at runtime.  If that is what
you want, you must write the annotation class in Java.
final class MyAnnotation extends scala.ClassfileAnnotation

Discussion

关于scala - 如何在 Scala 中定义 @interface?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2265773/

相关文章:

scala - 猫中的右分离

scala - Akka-Http Websockets : How to Send consumers the same stream of data

java - XML dateTime 解码为 NULL

java - java 1.8 上的 maven-enunciate-plugin 作为 apt 现在已从最新的 JDK(即 java 注释处理工具)中删除

java - 如何使用@XmlIDREF和@XmlID

model-view-controller - 用scala功能风格包装spring mvc会很容易吗?

eclipse - 如何在eclipse中添加sbteclipse插件

mysql - tomcat connectionpool mysql配置避免空闲连接超时

ios - iPhone map 套件 : Accessibility of Annotations

groovy - 无法使用 groovy 注释中的类中定义的变量