Java annotation

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

History
The Java platform has various ad-hoc annotation mechanisms—for example, the   modifier, or the   javadoc tag. The Java Specification Request JSR-175 introduced the general-purpose annotation (also known as metadata) facility to the Java Community Process in 2002; it gained approval in September 2004.

Annotations became available in the language itself beginning with version 1.5 of the Java Development Kit (JDK). The  tool provided a provisional interface for compile-time annotation processing in JDK version 1.5; JSR-269 formalized this, and it became integrated into the javac compiler in version 1.6.

Built-in annotations
Java defines a set of annotations that are built into the language. Of the seven standard annotations, three are part of java.lang, and the remaining four are imported from java.lang.annotation.

Annotations applied to Java code:
 * - Checks that the method is an override. Causes a compilation error if the method is not found in one of the parent classes or implemented interfaces.
 * - Marks the method as obsolete. Causes a compile warning if the method is used.
 * - Instructs the compiler to suppress the compile time warnings specified in the annotation parameters.

Annotations applied to other annotations (also known as "Meta Annotations"):


 * - Specifies how the marked annotation is stored, whether in code only, compiled into the class, or available at runtime through reflection.
 * - Marks another annotation for inclusion in the documentation.
 * - Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
 * - Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited by subclasses).

Since Java 7, three additional annotations have been added to the language.


 * - Suppress warnings for all callers of a method or constructor with a generics varargs parameter, since Java 7.
 * - Specifies that the type declaration is intended to be a functional interface, since Java 8.
 * - Specifies that the annotation can be applied more than once to the same declaration, since Java 8.

Built-in annotations
This example demonstrates the use of the  annotation. It instructs the compiler to check parent classes for matching methods. In this case, an error is generated because the  method of class Cat doesn't in fact override   of class Animal like is desired, because of the mismatching case. If the  annotation were absent, a new method of name   would be created in class Cat.

Custom annotations
Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the keyword "interface".

Annotations may include a set of key-value pairs, which are modeled as methods of the annotation type. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types. Methods can have default values.

Annotations themselves may be annotated to indicate where and when they can be used:

The compiler reserves a set of special annotations (including,   and  ) for syntactic purposes.

Annotations are often used by frameworks as a way of conveniently applying behaviours to user-defined classes and methods that must otherwise be declared in an external source (such as an XML configuration file) or programmatically (with API calls). The following, for example, is an annotated JPA data class:

The annotations are not method calls and will not, by themselves, do anything. Rather, the class object is passed to the JPA implementation at run-time, which then extracts the annotations to generate an object–relational mapping.

A complete example is given below:

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. However, annotation processors cannot modify the annotated code itself. (Code modifications may be implemented using methods beyond the Java Language Specification.) The Java compiler conditionally stores annotation metadata in the class files, if the annotation has a  of   or. Later, the JVM or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.

In addition to processing an annotation using an annotation processor, a Java programmer can write their own code that uses reflection to process the annotation. Java SE 5 supports a new interface that is defined in the  package. This package contains the interface called  that is implemented by the Java reflection classes including ,  ,  ,  , and. The implementations of this interface are used to represent an annotated element of the program currently running in the Java Virtual Machine. This interface allows annotations to be read reflectively.

The  interface provides access to annotations having   retention. This access is provided by the,  , and   methods. Because annotation types are compiled and stored in byte code files just like classes, the annotations returned by these methods can be queried just like any regular Java object. A complete example of processing an annotation is provided below: