简体   繁体   English

如何在Java代码中使用Scala注释

[英]How to use Scala annotations in Java code

Is there any way to use annotations defined in Scala so they can work with Java code? 有什么方法可以使用Scala中定义的注释,以便它们可以与Java代码一起使用? I have a library with some annotations I would like to use in Java 我有一个带有一些注释的库,我想在Java中使用

Quotes from scaladocs: 引用scaladocs:

A base class for annotations. 注释的基类。 Annotations extending this class directly are not preserved in the classfile. 直接扩展此类的注释未保留在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]]. 为了能够将注释存储在类文件的Scala签名中并使其可用于Scala反射和其他工具,注释需要从[[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. Scala中定义的注释类不会以Java兼容的方式存储在类文件中,因此在Java反射中不可见。 In order to achieve this, the annotation has to be written in Java. 为了实现这一点,注释必须用Java编写。

abstract class Annotation

https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/Annotation.scala 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. 这些可用于Scala类型检查器或Scala反射,即使在不同的编译单元中也是如此。 Annotation classes defined in Scala are not stored in classfiles in a Java-compatible manner and therefore not visible in Java reflection. Scala中定义的注释类不会以Java兼容的方式存储在类文件中,因此在Java反射中不可见。 In order to achieve this, the annotation has to be written in Java. 为了实现这一点,注释必须用Java编写。

trait StaticAnnotation extends Annotation

https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/StaticAnnotation.scala https://github.com/scala/scala/blob/2.13.x/src/library/scala/annotation/StaticAnnotation.scala

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM