Java Annotation
Annotation Quick Tips
1. Annotations were introduced since java 1.5
2. Annotation are data about the code. They are tags that may be useful while compilation or execution.
3. @Override, @author etc are some defualt annotations.
4. We can provide dynamicity to our codes using annotations.
5. Annotation can be declared on class, methods, local variable, constructors, package, parameters etc.
6. Annotations are widely used in Aspect Oriented Programming, Doclets, IoC container, ORM etc
How to write your own annotations?
*. Annotation can be created by creating an interface with @ symbol
Eg. public @interface MyAnnotation
*We need to mention the scope of annotation for processing it using @Target and @Retention
@Target(ElementType.METHOD) //The place where we will be using the MyAnnotation in code
@Retention(RetentionPolicy.SOURCE) //The scope of the annotation for processing it.
* If we have only one attribute in annotation it should be named value.
eg.
public @interface MyAnnotation {
String value();
}
To use those annotations we need Reflection to get those annotations and the process accordingly.
Class sampleClass = SampleClass.class; for(Method method : sampleClass.getMethods()) { MyAnnotation myAnnotations = (MyAnnotation)method.getAnnotation(UsageClass.class); }
Best Open Source Business Intelligence Software Helical Insight is Here