About Java Annotation



Mainly java annotation are meta data, You can take some decision on the method and class at runtime using java reflection to read those defined annotation.

@Retention


This annotation is used to specify whether the newly created annotation is limited to source code only (RetentionPolicy.SOURCE), 

will be embedded into generated class file (RetentionPolicy.CLASS),
or it will be available at runtime through the Reflection mechanishm (RetentionPolicy.RUNTIME).

@Retention (RetentionPolicy.RUNTIME)

public @interface Test {
}

@Documented

This annotation is used to indicate the newly created annoation should be included into the
generated java doc.

@Retention (RetentionPolicy.RUNTIME)

@Documented
public @interface MyDoc {
}

@Target

This annotation is used to specify the usage scope for the newly created annotation.
Whether the newly created annotation will be used at method or construtor or class or parameter level.

import java.lang.annotation.ElementType;

import java.lang.annotation.Target;

@Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR,
                 ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.LOCAL_VARIABLE,
                 ElementType.PACKAGE, ElementType.PARAMETER})
public @interface MyAnno {
}

@Inherited

Applying this annotation at any newly created annotation, and then when you use that newly created annotation
on any class then for any child class of that class the newly created annotation will be available.

@Override

This annotation check whether the created method is overriden method or not. It will create compile time error
if it doesnt find the method in any super class.

Example for class level Annotation

package anno;


import java.lang.annotation.ElementType;

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

@Retention (RetentionPolicy.RUNTIME)

@Target ( ElementType.TYPE) // TYPE is for class level annotations
public @interface Owner {

public enum Priority {

LOW, MEDIUM, HIGH;
}

String createdBy() default "raj";
String createdDate() default "1/1/2011";
String[] tags() default "";
Priority priority() default Priority.LOW;
}

====================



package anno;
import anno.Owner.Priority;

@Owner ( 
createdBy = "raj", 
createdDate = "13-OCT-2017", 
priority = Priority.HIGH, 
tags = {"RED", "WHITE", "BLUE", "GREEN", "PINK", "BLACK"}
)
public class Colors {
}

=====================

import java.lang.annotation.Annotation;

import java.lang.reflect.Method;

import anno.Colors;

import anno.Owner;

public class AnnotationFlight {
public static void main(String[] galaxies){
//CLASS LEVEL ANNOTATION


Class<Colors> colorObj = Colors.class;

if(colorObj.isAnnotationPresent(Owner.class)){

Annotation annotation = colorObj.getAnnotation(Owner.class);
Owner ownerAnnotation = (Owner)annotation;
System.out.println("Colors class is CreatedBy: "+ownerAnnotation.createdBy());
System.out.println("Colors class is createdDate: "+ownerAnnotation.createdDate());
System.out.println("Colors class priority is: "+ownerAnnotation.priority().name());
System.out.println("Colors class tags are : ");
for(String tag : ownerAnnotation.tags()){
System.out.println("Tag: "+tag);
}
}
}

}

Example about Method level Annotation

package anno;

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

@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.METHOD)

public @interface Test {
public boolean ignore() default false;
}

=======================

package anno;

public class Flowers {

@Test ( ignore = false)
public void checkFlowerColor(){
System.out.println("Checking Flower colors");
}
@Test (ignore = true)
public void checkFlowerAroma(){
System.out.println("Checking flowr Aroma");
}
@Test
public void nascentFlower(){
System.out.println("Flower is nascenting");
}
}

=====================

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import anno.Colors;
import anno.Flowers;
import anno.Owner;
import anno.Test;

public class AnnotationFlight {
public static void main(String[] galaxies){
// METHOD LEVEL ANNOTATION
Class<Flowers>  infoObj = Flowers.class;
for(Method method : infoObj.getDeclaredMethods()){
if(method.isAnnotationPresent(Test.class)){
Annotation annotation = method.getAnnotation(Test.class);
Test test = (Test) annotation;
if(test.ignore()==false){
try{
System.out.println("Method called '"+method.getName()+"'.");
method.invoke(infoObj.newInstance());
System.out.println("Method '"+method.getName()+"' executed successfully");
}catch(Exception ex){
System.out.println("Error while executing method '"+method.getName()+"'");
}
}else{
System.out.println("Method '"+method.getName()+"' ignored.");
}
}

}
    }

}