如何在 Java 代码中使用 Scala 注释 [英] How to use Scala annotations in Java code

查看:36
本文介绍了如何在 Java 代码中使用 Scala 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以使用 Scala 中定义的注释,以便它们可以与 Java 代码一起使用?我有一个带有一些注释的库,我想在 Java 中使用

Is there any way to use annotations defined in Scala so they can work with Java code? I have a library with some annotations I would like to use in Java

推荐答案

引用自 scaladocs:

Quotes from scaladocs:

注解的基类.直接扩展此类的注释不会保留在类文件中.启用存储类文件的 Scala 签名中的注释并使其可用于 Scala 反射和其他工具,注解需要继承自[[scala.annotation.StaticAnnotation]].Scala 中定义的注解类没有以 Java 兼容的方式存储在类文件中因此在 Java 反射中不可见. 为了实现这一点,注释必须用 Java 编写.

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.

抽象类注解

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

静态注解的基类.这些可用于 Scala类型检查器或 Scala 反射,即使跨不同的编译单位.Scala 中定义的注解类不存储在类文件以 Java 兼容的方式,因此在Java 反射. 为了实现这一点,注解必须是用 Java 编写.

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 扩展 Annotation

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

这篇关于如何在 Java 代码中使用 Scala 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆