Show Mobile Navigation

Webring

Powered by WebRing.

How to use annotations in Java.

Sachin R Kukale - 20:34
Annotations (Metadata)
Annotation provides the special way of specifying Metadata inside the java class instead of writing the metadata inside the XML file.
While developing applications using the frameworks like  Hibernate, Struts, Spring, EJB you need to write many XML documents which are specific to corresponding framework or technology.
If you use the XML file to specify the metadata inside the XML file then internally SAX parser will be used to read the information from XML file and to store in main memory, which is not good for performance.
From Java 5 you can avoid these XML file Meta-Data related to Struts, Hibernate, Spring inside the class itself with the help of annotation.
Main tasks in annotations implementations and uses are:
·        Define the annotation.
·        Provide the annotation processing tool (APT).
·        Use the annotation.
Normally technology or framework providers are responsible for defining the Annotation and providing the Annotation processing tools.
All annotations are subtype of interface java.lang.annotation.Annotation.
Public interface java.lang.annotation.Annotation{
boolean equals(Object);
int hashCode();
String toString();
Class annotationType();
}
As a developer you are only responsible to use the Annotations.
Java vendor has also provided some annotations which are used by default.
Two types of annotations are provided by the Java vendor:
       i.            Simple Annotations:
a)     @Deprecated:
It can be used on field, method, constructor or class and it indicates that this element is outdated and should not be used anymore. Adding deprecated to the class does not deprecate automatically all its fields and methods.
b)    @Override:
It can be applied for method. It is used to check if the annotated method really overrides a method of an interface or the extended class, by Java compiler. Target for this annotation is method and it won’t be used in class file.
c)     @SupressWarning:
It used to merely to tell the compiler to ignore specific warnings they produce, its retention policy is SOURCE. For ex. using raw types in generics.
    ii.            Meta Annotations.
a)     @Retention:
It will be used to specify that what will be scope of custom annotation i.e. to indicate the visibility of custom annotation.
To specify the scope you need to provide the value by using the constant from RetentionPolicyEnum.
Java.lang.annotation.RetentionPolicy enum
CLASS
When annotation value is given as ‘class’ then the annotation will be compiled and included in the class file.
RUNTIME
When the retention value is ‘Runtime’ this annotation will be available in JVM at runtime. We can write the custom code using reflection package and parse the annotation.
SOURCE
This annotation will be removed at compile time and will not be available at compiled class.
b)    @Target:
c)      Specifies the target member of custom annotation. If not specified then custom annotation will be used with any member. To specify the target you need to use the following constant from java.lang.annotation.ElementType enum.
d)      CONSTRUCTOR           FIELD               LOCAL_VARIABLE
e)      METHOD                     PACKAGE         PARAMETER
f)       TYPE etc.
g)     @Inherited:
Specifies that custom annotation will be inherited by the subclass or not. By default will not be inherited. If you want to specify that annotation used for superclass should be inherited by its subclasses then you need to use @inherited meta annotation.
With custom annotation:
Syntax to use marker annotation:
@
Defining:
@Target(ElementType.TYPE)
@Retention (RetentionPolicy.RUNTIME)
@interface JlcCloneable{}
Using:
@JlcCloneable
@Student{
. . . . . .
}
Custom Annotations
You can also define your own annotations using the reflection for APT. If you are creating your own annotations then you need to define annotation processing tool.
Following methods from java.lang.Class can be used to define the APT:
public  Boolean isAnnotation();
public annotation getAnnotation(class);
public Boolean isAnnotationPresent(class);
public Annotation[] getAnnotation();
Syntax to define the annotation:
@interface{
//some member.
}
You can not define constructor, initialization blocks or methods inside the annotation. You can define class, interface or annotation inside the annotation syntax.
You can use following method to verify that annotation is present with the member or not:
public Boolean isAnnotationPresent(class<! Extends Annotation>annotationclass)
If annotation is present then use the following method to get the annotation object:
public Annotation getAnnotation(class annotationclass)
You need to typecast the result in custom annotation type.
With annotation reference you can use the defined attribute to access the value.
Marker Annotations
The custom annotations defined without any members are called as marker annotations.
If you want define the APT while defining the custom annotations then the information should be accessed at runtime using the reflection.
You need to use the built-in Meta Annotation to provide this information.
Single Value Annotations
The annotation may have only one element sometimes. In such case the value should be named value.
If name of attribute is value then while using ‘this’ you can ignore attribute name.
Ex:
@interfacae Table{
String value();
}
@Table(“stud_table”)
class st1{}
@Table(value=”stud_table”)
class st2
If attribute is other than value then while specifying the data you need to use the attribute name.
Ex:
@interfacae Table{
String value();
}
@Table(value=“stud_table”)
class st1{}
//Invalid
@Table(value=”stud_table”)
class st2{}
Multiple Value Annotations
Sometimes annotation can have multiple elements.
If the default value is available then while using the annotation you can ignore or you can provide the value. If you are ignoring the attribute then the default value will be used.
Ex:
@interfacae Table{
String value() default “mytable”;
}
@Table()
class st1{}
@Table
class st2{}
@Table(value=”stud_table”)

class st3{}

0 comments:

Post a Comment