3

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

1
  • It depends. Most probably no. What library, what annotations, how to "work"? Commented Aug 21, 2019 at 12:49

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.