Annotations: metadata
· Annotations are compiled in the class files and can be retrieved in run time and used with some logical purpose by a consumer or processor.
· not available at runtime : also possible to create annotations that are.
· not available at compileTime : possible to create annotations that are only available in the source
Consumers:
annotations (Meta annotations) that come out of the box with the standard Java, the consumer is the Java Virtual Machine (JVM).
Junit, where the consumer is the Junit processor reading and analyzing the annotated test classes, and deciding what methods are going to be executed before and after every test.
consumers use reflection in Java in order to read and analyze the annotated source code. The main packages used for this purpose are java.lang and java.lang.reflect.
Built in annotations
Some of the following standard annotations are called Meta annotations; their targets are other annotations and contain information about them:
o @Depricated
· Built- in annotations applied to other annotations
· @Retention: annotates other annotations and informing about its nature(how the marked annotation is stored – code only, compiled into class or available at RunTime through reflection), Possible values are:
· SOURCE: ignored by compiler and JVM (no available at run time) and it is only retained in the source file.
· CLASS: retained by the compiler in .class file but ignored by the JVM so not going to be available at run time.
· RUNTIME: retained in .class and can be used in runtime via reflection.
· @Target: marks another annotation to restrict what kind of java elements the annotation may be applied to, possible values are:
· ANNOTATION_TYPE: the annotation can only be used to annotate other annotations
· CONSTRUCTOR
· FIELD
· LOCAL_VARIABLE
· METHOD
· PACKAGE
· PARAMETER
· TYPE: type is either a class, interface, enum or annotation, can be apply to any
This example shows a Java annotation that can only be used to annotate methods.
· @Inherited: by default annotations are not inherited to subclass, use it to make it inheritable
· @Documented: mark another annotation for inclusion in the documentation
How Annotations Work and How to Write Custom Annotations
Annotations are only metadata and they do not contain any business logic
à Annotations only provide some information about the attribute (class/method/package/field) on which it is defined. Consumer is a piece of code which reads this information and then performs necessary logic.
When we are talking about standard annotations like @Override – JVM is the consumer and it works at bytecode level
Write Custom Annotations: these are used to write CA
We have defined our custom annotation and applied it to some business logic methods. Now it’s time to write a consumer:
For that we will need to use Reflection which uses above annotation
Here, based on annotation param value you can apply some logic what a annotation should do like:
If(“Yeshwant”.equals(todoAnnotation.auther())){
// write your logic here, what to do on applying this annotation
------
}
· Annotation methods can’t have parameters.
· Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these
· Annotation methods can have default values.
Another Example to build Annotation based Test Framework: