Thursday, February 16, 2012

Defining and reading a cutom annotation


In this article we will create a custom annotation , and use that annotation in a class, and also we will write a program which reads the annotation value.
You then, can understand how the dependency injection happens , using annotations.
Following is the code which shows of creating annotations.

@Retention(RetentionPolicy.RUNTIME)

public @interface Argument {
String value() default "";
boolean required() default false;
String description() default "";
}

The @Retention annotations is of great use which help us to define, whether the annotation can be used in class level, field level, method level or the combinations.
Next we will have a class that use this annotation

public class AnnotationTester {
@Argument(value = "input", description = "This is the input file", required = true)
public String inputFilename;
@Argument(value = "output", description = "This is the output file", required = true)
public String outputFilename;
@Argument(description = "This flag can optionally be set")
public boolean someflag;
public AnnotationTester(){

}

@Argument(value="testValue",required=true,description = "This is method")
public void callMe(){
System.out.println("Input: " + inputFilename);
System.out.println("Output: " + outputFilename);
System.out.println("Someflag: " + someflag);
}
@Argument(description = "This is method")
public void method1(){
System.out.println("Hi All");
}
}

So we have used the custom annotation which we have created in the above shown code.
Next we will write a class which will read all the annotations that are defined in the above class.
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Hashtable;
public class AnnotationReader
{
private static Object name=new AnnotationTester();
public AnnotationReader(Object name) throws Exception{
System.out.println(""+name);
this.name=name;
getAnnotateValue();
}
public AnnotationReader(){}
public void getAnnotateValue() throws Exception
{
Class<AnnotationTester> classObject = AnnotationTester.class;
//readAnnotation(classObject);
Method method1 = classObject.getMethod("method1", new Class[]{});
//readAnnotation(method1);

Field[] method2=classObject.getDeclaredFields();
for(int i=0;i<method2.length;i++){
System.out.println("Thi si smethod"+method2[i]);
System.out.println("Thi si smethod"+method2[3].get(name));
method2[3].set(name, "Meiyappan");
System.out.println("Hi-> "+((AnnotationTester)name).hhh);

}

Field[] fs=classObject.getDeclaredFields();
for(int j=0;j<fs.length;j++){
readAnnotation(fs[j]);
}
Method[] method=classObject.getDeclaredMethods();
for(int i=0;i<method.length;i++){
readAnnotation(method[i]);
}
}

static void readAnnotation(AnnotatedElement element)
{
try
{
System.out.println("\nFinding annotations on " + element.getClass().getCanonicalName());
Annotation[] classAnnotations = element.getAnnotations();
for(Annotation annotation : classAnnotations)
{
if (annotation instanceof Argument)
{
Argument argument = (Argument)annotation;
System.out.println("DESC:" + argument.description());
System.out.println("VALUE:" + argument.value());
System.out.println("REQUIRED:" + argument.required());
}
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
new AnnotationReader().getAnnotateValue();
}
}