Jump to content

Java annotation

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 71.252.101.51 (talk) at 20:05, 8 March 2008 (Undid revision 195980143 by 80.237.187.34 - nothig remover is hearsay - rather an expert point of view). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

An annotation, in the Java computer programming language, is a special syntax that adds metadata to Java source code. They can be added to program elements such as classes, methods, fields, parameters, local variables, and packages. Unlike Javadoc tags, Java annotations are reflective in that they may be retained by the Java VM and made retrievable at run-time.

 @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) Item item = new Carrot();
 @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 {
   private Integer id;
   private String name;
   
   @Id                                             // Map this to the primary key column.
   @GeneratedValue(strategy = GenerationType.AUTO) // Database will generate new primary keys, not us.
   public Integer getId() { 
     return id; 
   }
   
   public void setId(Integer id) { 
     this.id = id;
   }
   
   @Column(length = 32)                            // Truncate column values to 32 characters.
   public String getName() {
     return body;
   }
   
   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. 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.

Reception

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.

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 (in 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

References