gpt4 book ai didi

java - 如何在 Java 代码中使用 Scala 注解

转载 作者:行者123 更新时间:2023-11-30 07:40:35 26 4
gpt4 key购买 nike

有没有什么方法可以使用在 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

关于java - 如何在 Java 代码中使用 Scala 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57589952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com