Java annotation

Java annotation

An annotation, in the Java computer programming language, is a special form of syntactic metadata that can be added to Java source code. [cite web|url = http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html|title = JDK 5.0 Developer's Guide: Annotations|date = 2007-12-18|accessdate = 2008-03-05|publisher = Sun Microsystems.] Classes, methods, variables, parameters and packages may be annotated. Unlike Javadoc tags, Java annotations are reflective in that they are embedded in class files generated by the compiler and may be retained by the Java VM to be made retrievable at run-time. [citation|title = Java(TM) Language Specification|edition = 3rd|publisher = Prentice Hall|year = 2005|isbn = 0321246780|url = http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html|author = Sun Microsystems.]

Examples

@Twizzle public void toggle() { } "// @Twizzle is an annotation to method toggle()." public @interface Twizzle { } "// Declares the annotation Twizzle."

Annotations may include an optional list of key-value pairs:

"// Same as: @Edible(value = true)" @Edible(true) Item item = new Carrot(); public @interface Edible { boolean value() default false; } @Author(first = "Oompah", last = "Loompah") Book book = new Book(); public @interface Author { String first(); String last(); }

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

@Retention(RetentionPolicy.RUNTIME) "// Retain this annotation in the VM for use at runtime." @Target({ElementType.METHOD}) "// This annotation can only be applied to class methods." public @interface Tweezable { }

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 associating behaviors with user-defined classes and methods that must otherwise be declared in some external source (like an XML configuration file) or programmatically (with API calls). The following, for example, is an annotated EJB 3.0 data class: @Entity "// Declares this an entity bean" @Table(name = "people") "// Maps the bean to SQL table "people" class Person implements Serializable { @Id "// Map this to the primary key column." @GeneratedValue(strategy = GenerationType.AUTO) "// Database will generate new primary keys, not us." private Integer id; @Column(length = 32) "// Truncate column values to 32 characters." private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

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

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. [cite web|url = http://www.jcp.org/en/jsr/detail?id=175|title = JSR 175: A Metadata Facility for the JavaTM Programming Language|date = 2006-11-02|accessdate = 2008-03-05|first = Danny|last = Coward|publisher = Java Community Process.] 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.

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.

ee also

* Java programming
* Model-driven architecture
* Java virtual machine

References

External links

* [http://www.javabeat.net/articles/java-5-0/2007/08/annotations/ Introduction to Java 5.0 Annotations by Joy Christy]
* [http://www.regdeveloper.co.uk/2006/02/24/java_annotations/ Of Java Annotations by John Hunt]
* [http://www.developer.com/java/other/article.php/3556176 An Introduction to Java Annotations by M. M. Islam Chisty]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Annotation — An annotation is a note that is made while reading any form of text. This may be as simple as underlining or highlighting passages. Annotated bibliographies give descriptions about how each source is useful to an author in constructing a paper or …   Wikipedia

  • Java Platform, Standard Edition — or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use.In practical terms, Java SE consists of a virtual machine, which must be used to run Java… …   Wikipedia

  • Java Management Extensions — (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (e. g. printers) and service oriented networks. Those resources are represented by objects called MBeans (for Managed Bean). In… …   Wikipedia

  • Java (Framework) — Java Objektorientierte Programmiersprache Basisdaten Paradigmen: Objektorientierte Programmiersprache Aktuelle  …   Deutsch Wikipedia

  • Java (Softwaretechnologie) — Java Objektorientierte Programmiersprache Basisdaten Paradigmen: Objektorientierte Programmiersprache Aktuelle  …   Deutsch Wikipedia

  • Java (Technologie) — Java Logo Java Technik (englisch Java Technology) ist eine hauptsächlich von Sun Microsystems entwickelte Sammlung von Spezifikationen, die einerseits die Programmiersprache Java und andererseits verschiedene Laufzeitumgebungen für… …   Deutsch Wikipedia

  • Java 2 Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java 2 Platform Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java EE — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

  • Java Platform Enterprise Edition — Java Platform, Enterprise Edition, abgekürzt Java EE oder früher J2EE, ist die Spezifikation einer Softwarearchitektur für die transaktionsbasierte Ausführung von in Java programmierten Anwendungen und insbesondere Web Anwendungen. Sie ist eine… …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”