tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Annotations > Retention Annotation

Retention Annotation 

The Retention Policy of Annotation specifies its durability. Some Annotation are available only in source code, some in Class byte code format and others available at Runtime. We can specify the Retention Policy with @Retention annotation.

The possible Retention Policies are listed below,

  • RetentionPolicy.SOURCE
    Available in source code only.
  • RetentionPolicy.CLASS
    Available in source code and class byte code.
  • RetentionPolicy.RUNTIME
    Available in source code, class byte code and at Runtime.

Two Annotations with Retention Policy RUNTIME are shown below,

File Name  :  
com/bethecoder/tutorials/annotations/retention/Author.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.retention;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Permitted types for annotation attributes
 
 * 1. primitive type
 * 2. String
 * 3. Class
 * 4. annotation 
 * 5. enumeration 
 * 6. 1-dimensional arrays
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Author {
  String name() default "admin";
  String creationDate();
}
   

File Name  :  
com/bethecoder/tutorials/annotations/retention/VersionHistory.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.retention;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Permitted types for annotation attributes
 
 * 1. primitive type
 * 2. String
 * 3. Class
 * 4. annotation 
 * 5. enumeration 
 * 6. 1-dimensional arrays
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface VersionHistory {
  String [] value();
}
   

Its usage is shown below,

File Name  :  
com/bethecoder/tutorials/annotations/retention/Source.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.retention;

@Author(creationDate="01/01/2500")
@VersionHistory({ "1.0""2.0""3.0" })
public class Source {

  private int fieldOne;
  private String fieldTwo;
  
  public Source(int fieldOne, String fieldTwo) {
    super();
    this.fieldOne = fieldOne;
    this.fieldTwo = fieldTwo;
  }

  public int getFieldOne() {
    return fieldOne;
  }
  
  public void setFieldOne(int fieldOne) {
    this.fieldOne = fieldOne;
  }
  
  public String getFieldTwo() {
    return fieldTwo;
  }
  
  public void setFieldTwo(String fieldTwo) {
    this.fieldTwo = fieldTwo;
  }
  
}
   

Only the Annotations with Retention Policy RUNTIME are accessible using Reflection API. The following example shows how to access Annotations at runtime.

File Name  :  
com/bethecoder/tutorials/annotations/retention/Test.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.retention;

import java.lang.annotation.Annotation;
import java.util.Arrays;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {

    Source src = new Source(1"Java");
    Annotation [] annotations = src.getClass().getAnnotations();
    System.out.println("Annotation count : " + annotations.length);
    
    for (Annotation an : annotations) {
      System.out.println(an);
    }
    
    Author author = src.getClass().getAnnotation(Author.class);
    
    if (author != null) {
      System.out.println("Author Name : " + author.name());
      System.out.println("Creation Date : " + author.creationDate());
    }
    
    VersionHistory versionHistory = src.getClass().getAnnotation(VersionHistory.class);
    
    if (versionHistory != null) {
      System.out.println("Version History : " + Arrays.toString(versionHistory.value()));
    }
    
  }

}
   

It gives the following output,
Annotation count : 2
@com.bethecoder.tutorials.annotations.retention.VersionHistory(value=[1.0, 2.0, 3.0])
@com.bethecoder.tutorials.annotations.retention.Author(name=admin, creationDate=01/01/2500)

Author Name : admin
Creation Date : 01/01/2500

Version History : [1.0, 2.0, 3.0]



 
  


  
bl  br