Java annotation
Encyclopedia
An annotation, in the Java computer programming language
, is a special form of syntactic metadata
that can be added to Java
source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc
tags, Java annotations can be reflective
in that they can be embedded in class files
generated by the compiler and may be retained by the Java VM to be made retrievable at run-time.
Annotations may include an optional list of key-value pairs:
Annotations themselves may be annotated to indicate where and when they can be used:
The compiler reserves a set of special annotations (including @Deprecated, @Override and @SuppressWarnings) 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 some external source (such as an XML configuration file) or programmatically (with API calls). The following, for example, is an annotated EJB 3.0 data class:
A complete example is given below:
The annotations are not method calls and will not, by themselves, do anything. Rather, the class object is passed to EJB implementation at run-time, which then extracts the annotations to generate an ORM
.
as JSR-175 in 2002 and approved in September 2004. Annotations became available in the language itself beginning with version 1.5 of the JDK. 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.
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
, is a special form of syntactic metadata
Metadata
The term metadata is an ambiguous term which is used for two fundamentally different concepts . Although the expression "data about data" is often used, it does not apply to both in the same way. Structural metadata, the design and specification of data structures, cannot be about data, because at...
that can be added to Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
source code. Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc
Javadoc
Javadoc is a documentation generator from Sun Microsystems for generating API documentation in HTML format from Java source code.The "doc comments" format used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, such as Netbeans and Eclipse automatically generate...
tags, Java annotations can be reflective
Reflection (computer science)
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime....
in that they can be embedded in class files
Class (file format)
In the Java programming language, source files are compiled into machine-readable class files which have a .class extension. Since Java is a platform-independent language, source code is compiled into an output file known as bytecode, which it stores in a .class file. If a source file has more...
generated by the compiler and may be retained by the Java VM to be made retrievable at run-time.
Examples
Annotations may include an optional list of key-value pairs:
Annotations themselves may be annotated to indicate where and when they can be used:
The compiler reserves a set of special annotations (including @Deprecated, @Override and @SuppressWarnings) for syntactic purposes.
Annotations are often used by frameworks
Software framework
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by user code, thus providing application specific software...
as a way of conveniently applying behaviours to user-defined classes and methods that must otherwise be declared in some external source (such as an XML configuration file) or programmatically (with API calls). The following, for example, is an annotated EJB 3.0 data class:
A complete example is given below:
The annotations are not method calls and will not, by themselves, do anything. Rather, the class object is passed to EJB implementation at run-time, which then extracts the annotations to generate an ORM
Object-relational mapping
Object-relational mapping in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages. This creates, in effect, a "virtual object database" that can be used from within the programming language...
.
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, and also 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.History
The Java platform has 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 ProcessJava Community Process
The Java Community Process or JCP, established in 1998, is a formalized process that allows interested parties to get involved in the definition of future versions and features of the Java platform....
as JSR-175 in 2002 and approved in September 2004. Annotations became available in the language itself beginning with version 1.5 of the JDK. 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
Javac
javac is the primary Java compiler, included in the Java Development Kit from Oracle Corporation.The compiler accepts source code conforming to the Java language specification and produces bytecode conforming to the Java Virtual Machine Specification .javac is itself written in Java...
compiler in version 1.6.
See also
- Java programming
- Model-driven architectureModel-driven architectureModel-driven architecture is a software design approach for the development of software systems. It provides a set of guidelines for the structuring of specifications, which are expressed as models. Model-driven architecture is a kind of domain engineering, and supports model-driven engineering of...
- Java virtual machineJava Virtual MachineA Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...
- .NET Attributes