Thursday 29 November 2012

Java Annotations



What are Annotations?


Annotations was introduced in J2SE 5. These are tags that help us insert metadata into the source code so that they can be processed by tools.
In the Java programming language, an annotation is used like a modifier, and it is placed before the annotated item, without a semicolon.

 

 

Types of Annotations

Standard Annotation : Are applied to regular java classes, methods and statements
Meta Annotations:  Applied to annotation definition to describe the behavior of the annotation being declared and how it can be used.

Standard Annotations

These are the Annotations that are provided by Java to help us when writing our classes

 Meta Annotations

These are annotations used when defining an annotation
@Target : Elements of the Java class to which annotation is applicable
       @Retention :   When are the annotations loaded into the JVM
         @Inherited : meta-annotation applies only to annotations for classes.  
If an Inherited meta-annotation is pre sent on an annotation type declaration, and the user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class's superclass will automatically be queried for the annotation type

This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached. If no superclass has an annotation for this type, then the query will indicate that the class in question has no such annotation.

       @Inherited Example:
@Target(ElementType.METHOD, ElementType.CLASS)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface  changelog
{
    String author() default "author" ;    // Annotation member
    String date();      // Annotation member
}
  
@changelog(author="vidhya", date="30/11/2012")
public  class superclass
{
}
  
public class subclass extends superclass
{
}

When subclass is queried for ChangeLog annotation, it will be successful as the changelog annotation is @inherited.
@Documented: If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.

 How to Define an Annotation and read it


Each annotation must be defined by an annotation interface. The methods of the interface  correspond to the elements of the annotation.

@Target(ElementType.METHOD, ElementType.CLASS)
@Retention(RetentionPolicy.RUNTIME)
Public @interface  ChangeLog
{
    String author();    // Annotation member
    String date();      // Annotation member
}

3 Types of annotation Definition

1. Marker - No Parameters, just used to denote a type. @Test in Junit 1.4 onwards
2. Single Value Annotation  - This contains just one value @name("Single Value")
3. Multiple parameter annotation - has many parameters @user(id="1234", name="user1")

       Example:

//Defined Annotation 
@ChangeLog(author="user1", date="10/10/2012") 
public class MyTest
{
 @ChangeLog(author="user2", date="20/08/2012")
 @TestCase 
 public void test1()
}


// The Annotation Reader
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class AnnotationTest
{
  public static void main( String[] args ) throws Exception
  {
    //access the class annotation
    Class clazz = MyTest.class;
    System.out.println( clazz.getAnnotation(ChangeLog.class ) );
   
    //access the method annotation
    Method method = clazz.getMethod( "test1" );
    ChangeLog changelog = (ChangeLog) method.getAnnotation(ChangeLog.class);
    System.out.println("--- Changelog Author:"+changelog.author()");
  }
}

1 comment: