For example, javax.annotation.Generated is meant to mark generated
code, but it's rarely useful. As there are AFAIK more tools working on
the bytecode than tools working with the source, the information
disappears just before it could be used.
Take a look at source code editor, Android Studio derived from JetBrains and many other IDE, need to work on source code and it provides all great editing experience just because of compile time annotations.
While the class is being edited (not yet compiled), editor can store and process annotations.
Example:
@SuppressWarnings
let's you suppress warnings, how else could you do it? C# allows you to define #PRAGMA
, #IF
, some sort of conditional compilation. None of conditional compilation information is stored in the compiled output.
@Override
allows Java compiler to check if base class has a method to override or not, if you define a new method with an erroneous parameters, java compiler will compile class with new method with overload, but in presence of @Override
java compiler will give you an error that signature does not match correctly to override the method.
@GeneratedCode
allows IDE to skip classes and members to be displayed when you search using "Find and Replace", and it lets you operate the IDE only on your code and not the generated one. Have you seen R.*
for resources in Android, these generated classes are hidden in Android Studio but they do provide useful code completion lists.
Similarly many such annotations allow you to do code analysis, write unit tests etc and do more work with it before compilation.
Here is more
Many ORM frameworks use compile time annotations and generates useful extra classes used for typed queries and other helper classes to create tables and maintain schema.
Conclusion
In example shown above, it is clear that all three and many such annotations will unnecessary add so many bytes which are totally useless at runtime.
Java had two options, one was adding some sort of compile time annotation using #IF
etc directives used in c based language. Which would require, new syntax and new editing experience etc, and another was to create Retention
. It was nice move to create Retention
without breaking syntax.