Saturday, August 16, 2014

Java Annotation

Annotations are like markers which supply metadata. Before JavaSE5 there was a trend of using XML's to provide the meta data of a program. However that resulted in reading a configuration file and the source code to understand the program. At runtime also, the tools have to read both the XML file and the class file to execute it correctly. However with annotations an attempt is made to bring the metadata inside the source code itself. It increases the readability of the program also.Annotations in simple terms are markers which provide extra information to compiler or run time environments. We can see them as an extension of keywords provided by Java and which is extendable. For example when you provide a file to a compiler how does it knows that it has a class defined in it. It will start parsing the file and look for a keyword class in it. Now let's see how annotations work. Now suppose in EJB you write a session bean. How does your EJB container knows which class represent Session beans. It will start inspecting the class files and will look for a marker/keyword in the class file @Stateless or @Stateful. In EJB2.1 the same thing was done by reading ejb xml. What annotations has done here is to move the configuration from XML file to the class file itself.
Earlier there was a tool called Xdoclet which use to do the same thing. The annotations were kept in the javadoc section and the xdoclet tool used to read it. With annotation this feature has been brought inside the language itself. Annotaions also bring compile time checking.
In JavaSE5 the standard annotations are:
  • @Override - Tells that the method has been overridden from the base class. Gives compile error of the signature do not matches.
  • @Deprecated - gives compiler warning if this method is used.
  • @SuppressWarning - To turn off compiler warnings.
So are annotaions good or bad. I am not sure about the outcome of debate but we are moving to annotations. Some goods and bad:
Good:
  • Compile time checking
  • One less XML to maintain as everything sits in java file.
Bad
  • You can have only one configuration. If you want to switch you will have to provide a separate class file.
  • In some frameworks like JSF,Spring if you give name to beans and you want to have multiple instances of beans than you have to use XML. 

Annotation Example

Let's write a simple annotation called author annotation. We can use this annotation over any class to specify who is the author of the class.
  • AuthorAnnotation - See that it is like any interface definition
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthorAnnotation{
      public String authorName() default "No Author";
}

Meaning of elements:
  • @Target - Tells where the annotation need to be applied.
  • @Retention - Duration for which the annotation element is kept. Choices are SOURCE, CLASS and RUNTIME
  • @Documented - Annotation to be included in javadoc
  • @Inherited - Annotations will be inherited by the subclass
Let's write a class where we apply the annotation:
@AuthorAnnotation(authorName="Lalit Bhatt")
public class AnnotatedClass {
}
Let's write a method to process the annotation. It can be a main method:
Class clazz = Class.forName("com.oyejava.AnnotatedClass");
Annotation[] annotations = clazz.getAnnotations();
for(Annotation a: annotations){
    AuthorAnnotation aa = (AuthorAnnotation)a;
    System.out.println("Class author is " +aa.authorName());
}

If you have annotation which are retained at source level than you will have to use apt tool for processing those annotations.

No comments:

Post a Comment