What are the major areas that we can use Annotations? Is the feature a replacement for XML based configuration?
Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object for its meta-object (e.g. anObj.getClass()
) is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g. aClass.getAnnotations
). Introspection and annotations belong to what is called reflection and meta-programming.
An annotation needs to be interpreted in one way or another to be useful. Annotations can be interpreted at development-time by the IDE or the compiler, or at run-time by a framework.
Annotation processing is a very powerful mechanism and can be used in a lot of different ways:
@Deprecated, @Override
, or @NotNull
@Entity, @TestCase, @WebService
@Statefull, @Transaction
@Column, @XmlElement
In all cases, an annotation is used to describe the element and clarify its meaning.
Prior to JDK5, information that is now expressed with annotations needed to be stored somewhere else, and XML files were frequently used. But it is more convenient to use annotations because they will belong to the Java code itself, and are hence much easier to manipulate than XML.
Usage of annotations:
...have a look for instance at the project Lombok, which uses annotations to define how to generate equals
or hashCode
methods.