In computer programming, annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
Annotations have a number of uses; the most visible among them are:
- Information for the compiler — used by the compiler to detect errors or suppress warnings.
- Compiler-time and deployment-time processing — annotation processor can process annotation information to produce new source code and other files.
- Runtime processing — some annotations are available to be examined at runtime.
Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements. Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.
History
The Java platform has always had various ad hoc annotation mechanisms - for example, the transient modifier, or the @deprecated javadoc tag. The general purpose annotation (also known as metadata) facility was introduced to the Java Community Process as JSR-175 in 2002 and approved in September 2004. This type of annotation became available with the JDK version 1.5. A provisional interface for compile-time annotation processing was provided by the apt tool in JDK version 1.5, and was formalized through JSR-269 and integrated into the javac compiler in version 1.6.
Processing
When Java source code is compiled, annotations can be processed by compiler plug-ins called annotation processors. Processors can produce informational messages or create additional Java source files or resources, which in turn may be compiled and processed, but processors cannot modify the annotated code itself. The Java compiler conditionally stores annotation metadata in the class files if the annotation has a RetentionPolicy of CLASS or RUNTIME. Later, the JVM or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.
Syntax
Declaring an annotation is a variation on tags that have been added to comment sections in the past.
Annotations take the form of an interface declaration with an @ preceding it and optionally marked with meta-annotations, as shown below:
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD})
In the above example both Retention and Target are examples of annotations.
Impact and Perception
The main advantage of the annotations is seen in their declarativeness: Annotations allow the programmer to declare in their source code how the software should behave. It is an example of how declarative programming constructs can be added to an object oriented language.
Robin Sharp, in his article Annotations: Don't Mess with Java, says: Annotations are a major kludge on the landscape. They don't fit and we don't need them; but you just know that they'll be picked up by framework junkies and abused horribly.
On the other hand - other authors disagreed:
Andrey Grebnev says in Java developers don't want Java annotations?that Java annotations is the future. In the same article, Jesper Joergensen of BEA told us that "Apache Beehive stores the wiring metadata and other metadata as JDK 5 annotations in the code"
Anil Saldhana of JBoss continues (the same article): "Combined with JDK 1.5 Annotations, aspects (AOP) also is a great way to expand the Java language in a clean pluggable way rather than using annotations solely for code generation.
See also
- Java programming
- Model-driven architecture
- Java virtual machine
- .Net CLI (Uses 'Attribute' constructs to achieve the same effect).