Original: http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html.
Meta-Annotations:
The role of meta annotations is to be responsible for annotating other annotations. Java5.0 defines 4 standard meta-annotation types, which are used to provide descriptions of other annotation types. Java5.0 defined meta-annotations:
[Email protected],
[Email protected],
[Email protected],
[Email protected]
These types and the classes they support can be found in the Java.lang.annotation package
@Target:
@Target illustrates the range of objects that annotation modifies: annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, constructor methods, member variables, enumeration values), Method parameters and local variables (such as loop variables, catch parameters). Target can be more clearly decorated by using target in the declaration of the annotation type.
Function: Used to describe the scope of use of annotations (i.e., where the annotations described can be used)
The values (ElementType) are:
1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations
@Retention:
@Retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "life cycle" of annotation.
Function: Indicates the level at which the annotation information needs to be saved to describe the life cycle of the annotation (i.e., the scope of the annotation being described is valid)
The values (Retentionpoicy) are:
1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)
The Retention meta-annotation type has a unique value as a member, and its value is derived from the enumeration type value of Java.lang.annotation.RetentionPolicy
@Documented:
@ Documented is used to describe other types of annotation that should be used as public APIs for annotated program members, so they can be documented by tools such as Javadoc. Documented is a markup annotation with no members.
@Inherited:
@Inherited meta-annotation is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for subclasses of that class.
Note: @Inherited the annotation type is inherited by subclasses of the class being labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads.
The reflection API enhances this inheritance when the retention of the @inherited annotation type callout annotation is retentionpolicy.runtime. If we use Java.lang.reflect to query the annotation of a @inherited annotation type, the reflection code check will work: Examine the class and its parent class until the specified annotation type is found, or reach the top level of the class inheritance structure.
Custom annotations:
When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be base type, Class, String, enum). You can declare the default value of a parameter through default.
To define the annotation format:
Public @interface annotation name {definition body}
Supported data types for annotation parameters:
1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. Arrays of all types above
How to set the parameters of the annotation type:
First, it can only be decorated with public or default access rights. For example, String value (); Here the method is set to Defaul default type;
Second, parameter members can only use basic types Byte,short,char,int,long,float,double,boolean eight basic data types and string,enum,class,annotations data types, As well as these types of arrays. For example, String value (), where the parameter member is string;
Thirdly, if there is only one parameter member, it is best to set the parameter name to "value", followed by parentheses. Example: The following example Fruitname annotation has only one parameter member.
Note Processor Class library (java.lang.reflect.AnnotatedElement):
Java uses the annotation interface to represent annotations in front of the program element, which is the parent interface for all annotation types. In addition, Java has added a annotatedelement interface under the Java.lang.reflect package, which represents a program element that can accept annotations in the program, which has the following implementation classes:
Class: Classes Definition
Constructor: Constructor definition
Field: Tired member variable definition
Method: Methods Definition of Class
Package: Packages definition for Class
The Java.lang.reflect package consists of a number of tool classes that implement the reflection function, and in fact, the Java.lang.reflect package provides the ability to read runtime annotation information with all the provided reflection APIs. When a annotation type is defined as a run-time annotation, the annotation is visible to the runtime, and the annotation stored in the class file is read by the virtual machine when the class file is loaded.
The Annotatedelement interface is the parent interface for all program elements (class, method, and constructor), so the program obtains the Annotatedelement object for a class by reflection. The program can call the following four methods of the object to access the annotation information:
Method 1:<t extends Annotation> T getannotation (class<t> annotationclass): Returns an annotation of the specified type that exists on the modified program element, if the type annotation does not exist, Then NULL is returned.
Method 2:annotation[] Getannotations (): Returns all annotations that exist on the program element.
Method 3:boolean is annotationpresent (class<?extends annotation> annotationclass): Determines whether the program element contains annotations of the specified type, and returns True if present. Otherwise, false is returned.
Method 4:annotation[] Getdeclaredannotations (): Returns all annotations that exist directly on this element. Unlike other methods in this interface, the method ignores inherited annotations. (if no comment exists directly on this element, an array of zero length is returned.) The caller of the method can arbitrarily modify the returned array, which does not have any effect on the array returned by other callers.