How to use Scala annotations in Java code

Maciej Laskowski :

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

Dmytro Mitin :

Quotes from 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=145120&siteId=1
Recommended