Java Annotations features and types

Java Annotations features and types

Annotations are a Java feature introduced in JDK5 (a similar mechanism, attributes, also exists in .NET). The main idea of java annotations is to give the programmer the ability to specify all necessary information in the same place. Annotation-based Java development is certainly one of the most notable recent development trends. Annotation-based development relieves Java developers from the pain of cumbersome configuration.

Java annotations are especially powerful as the basis for a DBMS interface when combined with Java?s reflection mechanism, which allows an application to get information about class format at runtime. Since Java works with objects, one requirement is to specify the format of objects (classes) to the database engine.

Java annotations are one of the main ease-of-development features introduced in JDK 5. Annotations are like meta-tags that you can add to your code and apply to package declarations, type declarations, constructors, methods, fields, parameters and variables. They provide a helpful way to indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes, and so on.

In Java, reflection can enable the DBMS to get format information about application classes at runtime, eliminating the need for either extra software (the specialized compiler or preprocessor) or extra work.

Creating an annotation is similar to creating an interface. But the annotation declaration is preceded by an @ symbol. The annotation declaration itself is annotated with the @Retention annotation. The @Retention annotation is used to specify the retention policy, which can be SOURCE, CLASS, or RUNTIME. The body of an annotation consists of method declarations without body. These methods work like fields. Once an annotation is created, it can be applied to classes, methods, fields, parameters, enums, and so on

Features of Java Annotations:

* Java Annotations are a special kind of Java interface.

* Annotations of java are represented with syntax in which the annotation?s name is preceded by the @ character.

* java Annotations can be associated with types, fields, methods, parameters and local variable declarations.

* Annotations parameters can be specified using keyword notation, and if a parameter is not specified, then a default value is used. The default “value” parameter can be set without specifying the name of the parameter.

*Annotations can be obtained using the reflection API at runtime Annotations do not alter the execution of a program but the information embedded using annotations can be used by various tools during development and deployment.

* An annotation cannot have the extends clause. However, annotations implicitly extend the Annotation interface.

Uses of Java Annotations:

Information for the compiler ? Annotations can be used by the compiler to detect errors or suppress warnings.

Compiler-time and deployment-time processing ? Software tools can process annotation information to generate code, XML files, and so forth.

Runtime processing ? Some annotations are available to be examined at runtime.

Java Annotation Types:

* Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.

* Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.

* Marker: Marker type annotations have no elements, except the annotation name itself.

* Single-Element: Single-element, or single-value type, annotations provide a single piece of data only. This can be represented with a data=value pair or, simply with the value (a shortcut syntax) only, within parenthesis.

* Full-value or multi-value: Full-value type annotations have multiple data members. Therefore, you must use a full data=value parameter syntax for each member.

The Java compiler conditionally stores annotation metadata in the class files if the annotation has a Retention Policy of CLASS or RUNTIME. Later, the JVM or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.