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

查看:54
本文介绍了如何在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:

注释的基类.直接扩展此类的注释未保留在classfile中.启用存储类文件的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.

特征StaticAnnotation扩展了Annotation

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

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

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