|
Many APIs require some information about the data which is to be sent to JVM to work with them. Most of the API's uses the XML files commonly known as deployment descriptor like EJB, JAX-RPC web service etc... Java 5.0 gives an easy way to give that meta data into a java file and JVM then reads that meta data at runtime to do the processing. This mechanism is also same as XDoclet.
The Java platform has always had various ad hoc annotation mechanisms. For example the transient modifier is an ad hoc annotation indicating that a field should be ignored by the serialization subsystem, and the @deprecated javadoc tag is an ad hoc annotation indicating that the method should no longer be used. As of release 5.0, the platform has a general purpose annotation (also known as metadata ) facility that permits you to define and use your own annotation types. The facility consists of a syntax for declaring annotation types, a syntax for annotating declarations, APIs for reading annotations, a class file representation for annotations, and an annotation processing tool .
Annotations have a number of uses, among them:
- 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.
Note:- There are three annotation types that are predefined by the language specification itself: @Deprecated , @Override , and @SuppressWarnings
The below example will demonstrate the use of the annotations.
package com.visualbuilder;
public class AnnotationExample {
@Deprecated public void hello(){ System.out.println("hello"); } // use a deprecated method and tell compiler not to generate a warning @SuppressWarnings("deprecation") public static void main(String ar[]){ new AnnotationExample().hello(); } }
Output:-
The program will print the "hello" without any warnings of deprecated methods being used. |