问题
I have come across a statement in scalatest documentation:
It must be defined in Java instead of Scala so it will be accessible at runtime.
This is quite weird as I was under the impression that Scala annotation is stronger than its Java counterpart. Why this feature is missing? Is there a plan to add it back?
回答1:
There is no known plan to have Scala support annotations retained at runtime (the issue has been known for a while but as this page says, it won't be fixed).
Lukas Rytz, a Scala committer, explained on a post on the scala-language
mailing list:
It's definitely non-trivial. Java annotations have specific representation in bytecode [...], which would require changes to Scala's bytecode generator.
Also the language of java annotations is quite special, different from the rest of the Java language [...], so one would need to define how this can be represented in a Scala class.
来源:https://stackoverflow.com/questions/38647213/why-annotations-written-in-scala-are-not-accessible-at-runtime