How to define and use custom annotations in Scala

前端 未结 2 1631
萌比男神i
萌比男神i 2021-02-20 04:28

I am trying use a custom annotation in Scala. In this example, I create a string that I want to annotate with metadata (in this case, another string). Then, given an instance of

2条回答
  •  天涯浪人
    2021-02-20 05:19

    There are different kinds of annotations in Scala:

    Java Annotations that you can access using the Java Reflection API, annotations that are just in the source code, static annotations that are available to the type checker across different compilation units (so they should be somewhere in a class file but not where normal reflections go) and classfile annotations which are stored like java annotations, but cannot be read using the java reflection api.

    I have described how to access static and classfile annotations here: What is the (current) state of scala reflection capabilities, especially wrt annotations, as of version 2.11?

    If you just need a annotation containing a string using a Java annotation that is loaded by the JVM for you might be the simpler alternative.

提交回复
热议问题