Annotations
As we mentioned in Chapter 4, Java for a
long time has supported a limited kind of metadata in Java source code
through the use of Javadoc comment tags. With Javadoc tags like @deprecated or @author, we can add some
information to a class, method, or field by sticking it into comments
above the item. In this case, the information is mainly useful to the
Javadoc documentation generator, because comments exist only in Java
source code. However, developers have long wanted a way to generalize
metadata for other purposes. And in fact, some tools have been developed
over the years that read extended Javadoc-style tags in comments and do
all sorts of things with them, including code generation and
documentation. In Java 5.0, a formal, extensible metadata system called
annotations was added to the language that provides this kind of
source-level functionality as well as new possibilities for using metadata
at runtime.
Annotations allow you to add metadata to Java packages, classes, methods, and fields. This metadata can be utilized by tools at compile time and optionally retained in the compiled Java classes for use at runtime as well. The availability of annotation data to the running program opens up new uses for metadata. For example, annotations cannot only be used at compile time to generate auxiliary classes or resources, but also could be used by a server to provide special services to classes such as importing or exporting of values, security, or monitoring. Annotations ...