Sunday 15 February 2015

Custom Annotation in java


1. source - discard compile time.
2. compile time - annotation exist in class file but not in vm
3. run time - annotation exist in run time vm knows these types of annotation(Hibernate,Spring use these annotation for understanding the class at run time). below example is run time annotation


package test;

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

@Retention(RetentionPolicy.RUNTIME)
@interface Test1{
public String name() default "vimal";
}
@Test1(name="vimalesan")
public class CustomAnnotationPro {
public static void main(String arg[]) {
Class<CustomAnnotationPro> c=CustomAnnotationPro.class;
if(c.isAnnotationPresent(Test1.class)){
Annotation annotation=c.getAnnotation(Test1.class);
Test1 t=(Test1)annotation;
if(t.name().equals("vimal")){
System.out.println("No Change");
}
else{
System.out.print("New value "+t.name());
}
}

}
}


No comments:

Post a Comment