Java Annotations Annotation (i)--Introduction
This chapter first briefly introduces the annotations, and the next chapter gives a demo of the application of annotations.
1. Meta-annotations
The role of meta-annotations is to be responsible for annotating other annotations.
In JDK1.5, 4 standard meta-annotation annotation types are defined, and they are used to provide descriptions of other annotation annotation types.
- @Target
- @Retention
- @Documented
- @Inherited
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, etc.), 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, that is, where the annotations described can be used.
Value (ElementType):
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 methods
5.PACKAGE: Used to describe the package
6. PARAMETER: Used to describe parameters
7. Type: Used to describe types such as classes, interfaces, or enumerations
@Retention
@Retention defines the time that the annotation annotations are retained sweat your high end, some annotation only appear 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).
Function: Indicates the level at which the annotation information needs to be saved to describe the life cycle of the annotation, that is, the scope in which the annotation described is valid.
Value (Retentionpoicy):
1. Source: Valid in source file
2. Class: Valid in class file
3. Runtime: Valid at run time
The attribute value of the annotation's retentionpolicy is runtime, so that the annotation processor can obtain the attribute value of the annotation by reflection, thus doing some logic processing of the runtime.
@Documented
@Documented 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
The @Inherited meta-annotation is a markup annotation @Inherited illustrates that a typed type is inherited and the annotation type is automatically inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for that class's subclass
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 Annotatiaon from the method it overloads.
2. Custom annotations
When using @interface custom annotations, the Java.lang.annotation.Annotation interface is automatically integrated 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, and none of the methods 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. You can declare the default value of a parameter through default.
Format of custom annotations:
@Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented Public @interfaces Demo { publicdefault "Demo";}
The above code shows that @demo this custom annotation, when used, can only annotate field, that is, the properties of the class, which only works at run time. The method in the annotations returns the string "demo" by default.
Supported data types for the parameters:
- All basic data types
- String type
- Class type
- Enum type
- Annotation type
- Arrays of all types above
The parameters in the annotation type are set:
- can only be modified with public or default access rights
- The type of the parameter member can only be a few of the above.
- If there is only one parameter member, it is best to set the parameter name to "value", as in the preceding code.
Default values for annotation elements:
The annotation element must have a definite value, either specified in the default value of the definition annotation, or specified when the annotation is used, and the value of a non-basic type of annotation element cannot be null. Therefore, using an empty string or 0 as the default value is a common practice. This constraint makes it difficult for the processor to represent the existence of an element or a missing ungtai, because in each annotation declaration, all elements are present and have corresponding values, in order to circumvent this constraint, we can only define some special values, such as an empty string or a negative number, once to indicate that an element does not exist, This has become a customary usage when defining annotations.
3. Annotation processor
In declaring the annotation Hiro, we are going to handle the method of annotation annotation.
Note Processor Class Library:
Java.lang.reflect.AnnotatedElement
Java Annotations Annotation (i)