如何在 Java 代码中使用 Scala 注释
How to use Scala annotations in Java code
是否有任何方法可以使用 Scala 中定义的注释,以便它们可以与 Java 代码一起使用?我有一个带有一些注释的库,我想在 Java
中使用
来自 scaladocs 的引述:
A base class for annotations.
Annotations extending this class directly are not preserved in the classfile. To enable storing
annotations in the classfile's Scala signature and make it available to Scala reflection and
other tools, the annotation needs to inherit from [[scala.annotation.StaticAnnotation]].
Annotation classes defined in Scala are not stored in classfiles in a Java-compatible manner
and therefore not visible in Java reflection. In order to achieve this, the annotation has to
be written in Java.
abstract class Annotation
https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/Annotation.scala
A base class for static annotations. These are available to the Scala
type checker or Scala reflection, even across different compilation
units. Annotation classes defined in Scala are not stored in
classfiles in a Java-compatible manner and therefore not visible in
Java reflection. In order to achieve this, the annotation has to be
written in Java.
trait StaticAnnotation extends Annotation
https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/StaticAnnotation.scala
是否有任何方法可以使用 Scala 中定义的注释,以便它们可以与 Java 代码一起使用?我有一个带有一些注释的库,我想在 Java
中使用来自 scaladocs 的引述:
A base class for annotations. Annotations extending this class directly are not preserved in the classfile. To enable storing annotations in the classfile's Scala signature and make it available to Scala reflection and other tools, the annotation needs to inherit from [[scala.annotation.StaticAnnotation]]. Annotation classes defined in Scala are not stored in classfiles in a Java-compatible manner and therefore not visible in Java reflection. In order to achieve this, the annotation has to be written in Java.
abstract class Annotation
https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/Annotation.scala
A base class for static annotations. These are available to the Scala type checker or Scala reflection, even across different compilation units. Annotation classes defined in Scala are not stored in classfiles in a Java-compatible manner and therefore not visible in Java reflection. In order to achieve this, the annotation has to be written in Java.
trait StaticAnnotation extends Annotation
https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/StaticAnnotation.scala