Custom Annotation in Java 5.0
Overview
Previous versions of Java provided a limited and ad-hoc mechanism for annotating code through JavaDoc comments
Java 1.5 allows both runtime and compile-time processing of annotation data. Annotations are said to annotate a Java element. A good example is the @deprecated JavaDoc tag. The @deprecated tag is used for documentation purposes. This tag has no effect on the code it describes, but causes the compiler to produce warnings if any other code references the tagged element.
Annotations will also be used for compile-time checking such as to produce warnings and errors for different failure scenarios. An example of an annotation that is used at compile time is the new @Deprecated annotation, which acts the same as the old @deprecated JavaDoc tag.
How to define an Annotation?
Annotations are defined in following way:
public @interface MyFirstAnnotation
{
// define the properties.
// define the properties.
}
MyFirstAnnotation is the name of the annotation.
Annotation may have metadata defined for itself also. Following are the Meta data that can be defined for an Annotation.
Target Annotation: The element (Packages, Classes, variables & methods) on which an Annotation has been defined is also needs to be specified. It needs to be specified in the following way:
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation
{
// define the properties.
// define the properties.
}
Here ‘@Target’ is a meta data for the annotation ‘MyFirstAnnotation’ and it symbolizes that the annotation can only be used with a method. ‘ElementType’ is an already defined Enum in ‘java.lang.annotation’ package.
ElementType can have the following usages <strong><a href="../wp-content/uploads/2010/06/Untitled-1-copy.jpg"><a href="http://www.skill-guru.com/blog/wp-content/uploads/2010/06/Untitled-1-copy1.jpg"><img class="aligncenter size-full wp-image-2375" src="http://www.skill-guru.com/blog/wp-content/uploads/2010/06/Untitled-1-copy1.jpg" alt="Custom Annatation" width="250" height="119" /></a> </a></strong>
Rentention Annotaion: This is another metadata for an Annotation. It tells to what extent an Annotation should be retained.
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation
{
// define the properties.
// define the properties.
}
An annotation can be retained at Code level or at class file level or at runtime JVM level. In the above example, the retention is at class file level. ‘RetentionPolicy’ is an already defined Enum in ‘java.lang.annotation’ package.
RententionPolicy can have following levels. <a href="http://www.skill-guru.com/blog/wp-content/uploads/2010/06/Untitled-2-copy1.jpg"><img class="aligncenter size-full wp-image-2374" src="http://www.skill-guru.com/blog/wp-content/uploads/2010/06/Untitled-2-copy1.jpg" alt="Custom Annatation" width="256" height="50" /></a>
Types of Annotation
Annotation can be of 3 types
Marker annotations: This kind of annotation has no variables. They are identified by only their name.
They are defined as follows:
public @interface MarkerAnnotation {
}
Used as @ MarkerAnnotation
Single Value Annotations: This kind of annotations is provided with a single value.
They are defined as follows:
public @interface SingleValueAnnotation {
String name();
}
Used as @SingleValueAnnotation(“singlevalue”)
Full annotations: This kind of annotations has multiple data members. This need to use fuller syntax.
They are defined as follows:
public @interface FullAnnotation {
String[] names();
}
Used as @FullAnnotation(value1=”val1”,value2=”val2”)
<strong> </strong>
Here is an example full annotation:
/**
* Describes the Request-For-Enhancement(RFE) that led
* to the presence of the annotated API element.
*/
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default “[unassigned]“;
String date() default “[unimplemented]“;
}
Once an annotation type is defined, you can use it to annotate declarations. An annotation is a special kind of modifier, and can be used anywhere that other modifiers (such as public, static, or final) can be used. By convention, annotations precede other modifiers. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:
@RequestForEnhancement(
id = 2868724,
synopsis = “Enable time-travel”,
engineer = “Mr. Peabody”,
date = “4/1/2007″
)
public static void travelThroughTime(Date destination) { … }
Conclusion
Before the advent of Annotations, Applications had to define their meta-data in some configuration files and usually these meta-data were externalized from the Application. Now, they can directly define this meta-data information in the source code










Hi
For annotation processing please use the APT (Annotation Processing Tool)
Please refer the following link for this.
http://java.sun.com/j2se/1.5.0/docs/guide/apt/GettingStarted.html
Thanks
Frederic Anand
@Vinay That would be really helpful.
Hi there,
It ll be really helpfull if you provide a complete example of how to use annotations in java programs and furthermore how to process them ?
thanks
@Karthikeyan, Anand can explain briefly on how to process these annotations.
It will also be helpful if you mention how to process these custom annotations.