Java Annotation Learning Informal essay

First:  Annotation  introduction   

Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.

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.

Annotations can be applied to a program's declarations of classes, fields, methods, and other program elements.

Second: Predefined by the language specification itself

@Override 

@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override{
/**
* Indicates that a method declaration is intended to override a method
* declaration in a superclass. If a method is annotated with this
* annotation type but does not override a superclass method,
* compilers are required to generate an error message. *
*/ }

 

@SuppressWarnings

@Target(value={TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE})
@Retention(value=SOURCE)
public @interface SuppressWarnings
{
/**
* Indicates that the named compiler warnings should be 
* suppressed in the annotated element (and in all program
* elements contained in the annotated element). Note that the
* set of warnings suppressed in a given element is a superset of
* the warnings suppressed in all containing elements. For example,
* if you annotate a class to suppress one warning and
* annotate a method to suppress another, both warnings will be suppressed in the method. * As a matter of style, programmers should always use this
* annotation on the most deeply nested element where it is effective.
* If you want to suppress a warning in a particular *method, you should annotate that method rather than its *class. *
*/
String [] value(); }

@Deprecated  it's outdated,no suggest that use it.

Third: Annotation Processing

@Target

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Target{
/**
* Indicates the kinds of program element to which an 
* annotation type is applicable. If a Target meta-annotation is
* not present on an annotation type declaration, the declared
* type may be used on any program element. If such a meta-annotation is present,
* the compiler will enforce the specified *usage restriction. * For example: * @Target(ElementType.ANNOTATION_TYPE) * public @interface MetaAnnotationType {
* ... * } *
*/ ElementType [] value(); } Enum ElementType{ ANNOTATION_TYPE // Annotation type declaration CONSTRUCTOR //Constructor declaration FIELD // Field declaration (includes enum constants) LOCAL_VARIABLE //Local variable declaration METHOD //Method declaration PACKAGE //Package declaration PARAMETER // Parameter declaration TYPE //Class, interface (including annotation type), or enum declaration }

@Retention

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Retention{
/**
* Indicates how long annotations with the annotated type are
* to be retained. If no Retention annotation is present on an 
* annotation type declaration, the retention policy defaults to *RetentionPolicy.CLASS. 
* A Target meta-annotation has effect only if the meta-
* annotated type is use directly for annotation. It has no effect 
* if the meta-annotated type is used as a member type in *another annotation type. 
**/
 RetentionPolicy value();
}
Enum RetentionPolicy{
CLASS     //Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. 
RUNTIME   //Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively. 
SOURCE    //Annotations are to be discarded by the compiler. 
}

@Documented

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Documented{
/**
* Indicates that annotations with a type are to be documented 
* by javadoc and similar tools by default. This type should be
* used to annotate the declarations of types whose
* annotations affect the use of annotated elements by their clients.
* If a type declaration is annotated with Documented,
* its annotations become part of the public API of the *annotated elements. *
*/ }

@inherited

@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Inherited
{
/**
* Indicates that an annotation type is automatically inherited. 
* If an Inherited meta-annotation is present on an annotation
* type declaration, and the user queries the annotation type on
* a class declaration, and the class declaration has no
* annotation for this type, then the class's superclass will
* automatically be queried for the annotation type. This process
* will be repeated until an annotation for this type is found, or
* the top of the class hierarchy (Object) is reached.
* If no superclass has an annotation for this type, then the query will
* indicate that the class in question has no such annotation. * Note that this meta-annotation type has no effect if the
* annotated type is used to annotate anything other than a class.
* Note also that this meta-annotation only causes annotations
* to be inherited from superclasses; annotations *on implemented interfaces have no effect. *
*/ }

 

 In a word ,The annotation is important in the furture.For example ,the properties of struts is using it !

posted @ 2012-08-17 16:18  zhangxp  阅读(226)  评论(0编辑  收藏  举报