Friday, July 22
Annotate-tate-tate
Annotation - extra information associated with a particular point in a documentAs of Java release 5.0(Tiger) , the platform has a general purpose annotation (also known as metadata) facility that permits you to define and use your own annotation types. The facility consists of a
>> syntax for declaring annotation types - declare an annotation type by preceding @ to normal interface declaration
>> syntax for annotating declarations - annotation is a special kind of modifier and so can be used anywhere that a modifier is used.
It generally preceds any declaration (class, method, variable, parameter etc) and consists of the @ tag + annotation name + element , value in ()
>> APIs for reading annotations - Class, Method api has annotation information which can be read with help of reflection.
>> a class file representation for annotations - generate support documents based on annotations
>> and an annotation processing tool.
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.
Annotation types :
>> Marker Annotation : An annotation type with no elements :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface Test { }
Declaration :
@Test public class TimeTravel { ... }
>> Single member annotation :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface Copyright {
int value(); // this variable name must be value
}
Declaration :
@Copyright("My Copyright")public class MyClass{...}
>> Custom annotation :
Definition :
@Retention(RetentionPolicy.RUNTIME)
@interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date(); default "[unimplemented]";
}
Declaration :
@RequestForEnhancement(id = 2868724,synopsis = "Manage Entitlements", engineer = "Mr. X", date = "4/1/3007" )
public static void manage() { ... }
Note that the annotation type declaration is itself annotated. Such annotations are called meta-annotations. The first (@Retention(RetentionPolicy.RUNTIME)) indicates that annotations with this type are to be retained by the VM so they can be read reflectively at run-time. The second (@Target(ElementType.METHOD)) indicates that this annotation type can be used to annotate only method declarations.
The defined annotations can be read, processed through the api (for simple processing) or by APT.
Possible usages for annotations :
>> Generate config files from annotated data in the code , so as to keep data at one place and avoid maitainence hassles.
>> Generate XML representation of data objects
>> Generate automatic boilerplate code using annotations.
Boilerplate - In information technology, a boilerplate is a unit of writing that can be reused over and over without change.
By extension, the idea is sometimes applied to reusable programming as in "boilerplate code." .. like a text template.
Useful Links :
http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html
http://www.java2s.com/ExampleCode/Language-Basics/Annotation.htm
http://www.javaspecialists.co.za/archive/Issue097.html
http://javaboutique.internet.com/tutorials/annotation/index-2.html
Dips at 3:30 PM