Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? I have a library with some annotations I would like to use in Java

like image 449
Maciej Laskowski Avatar asked Nov 24 '25 04:11

Maciej Laskowski


1 Answers

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

like image 169
Dmytro Mitin Avatar answered Nov 26 '25 19:11

Dmytro Mitin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!