Chapter 6. Annotations
Using Standard Annotation Types
Annotating an Overriding Method
Annotating a Deprecated Method
Suppressing Warnings
Creating Custom Annotation Types
Annotating Annotations
Defining an Annotation Type's Target
Setting the Retention of an Annotation Type
Documenting Annotation Types
Setting Up Inheritance in Annotations
Reflecting on Annotations
One of the more popular terms being tossed around in programming
these days is metadata. Metadata is simply information about information.
It resides somewhere in the spectrum between Java source code,
which is raw information for a compiler, and JavadocJavadoc, which is
pure documentation. Metadata typically makes statements about source
code that is interpreted at some point, usually by a code or data analysis
tool.Your first thought might be, "Well, Javadoc takes care of that, right?"
Consider thishow many ways are there to say, "This variable should
not be null." In Javadoc, you might say "non-null," you might say "This
variable shouldn't be null," you might say "Don't assign null to this." All
are valid in terms of documentation, but there is no consistency among
them. There isn't any tool that could analyze and account for all the variances
in how you might state this simple condition. Annotations, new to
Tiger, seek to solve that problem by providing a well-defined metadata
mechanism.In a nutshell, annotations are modifiers that can be applied to package
and type declarations, constructors, methods, fields, parameters, and
even variables. They take the form of
name=value
pairs, and you can use
Java's built-in types, as well as define custom annotation types of your
own.