To learn annotations in depth, we must be able to define our own annotations and use them. Before defining our own annotations, we must understand the syntax of meta-annotations and related definition annotations provided by Java.
-------------------------------------------------- ----------------------------------
Meta annotation:
The role of meta-annotations is to annotate other annotations. Java5.0 defines four standard meta-annotation types, which are used to provide descriptions of other annotation types. Meta-annotations defined by Java5.0:
1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited
These types and the classes they support are found in the java.lang.annotation package. Let's take a look at the role of each meta-annotation and the instructions for using the corresponding sub-parameters.
-------------------------------------------------- ----------------------------------
@Target:
@Target explains the scope of objects modified by Annotation: Annotation can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumeration values), method parameters and Local variables (such as loop variables, catch parameters). Using target in the declaration of the Annotation type can make the modified target clearer.
Function: Used to describe the scope of use of annotations (ie: where the described annotations 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 the parameters 7.TYPE: used to describe classes, interfaces (including annotation types) or enum declarations
Usage examples:
@Target(ElementType.FIELD)
public @interface NoDBColumn {
}
-------------------------------------------------- ----------------------------------
@Retention:
@Retention defines the length of time the Annotation is retained: some Annotations only appear in the source code and are discarded by the compiler; while others are compiled in the class file; Annotations compiled in the class file may be virtualized The machine ignores them, while others will be read when the class is loaded (please note that it does not affect the execution of the class, because Annotation and class are used separately). Use this meta-Annotation to limit the "life cycle" of Annotation.
Function: Indicates at what level the annotation information needs to be saved, and is used to describe the life cycle of the annotation (ie: within what scope the described annotation is valid)
The values (RetentionPoicy) are:
1.SOURCE: Valid in the source file (that is, the source file is retained)
2.CLASS: Valid in class files (i.e. class reserved)
3.RUNTIME: valid at runtime (that is, retained at runtime)
The Retention meta-annotation type has a unique value as a member, and its value comes from the enumeration type value of java.lang.annotation.RetentionPolicy. Specific examples are as follows:
-------------------------------------------------- ----------------------------------
@Documented:
@Documented is used to describe other types of annotation that should be used as the public API of the annotated program members and therefore can be documented by tools such as javadoc. Documented is a markup annotation and has no members.
The @Inherited meta-annotation is a markup annotation. @Inherited states that a certain annotated type is inherited. If an annotation type modified with @Inherited is used for a class, this annotation will be used for subclasses of that class.
Note: @Inherited annotation types are inherited by subclasses of the annotated class. A class does not inherit annotation from the interface it implements, and a method does not inherit annotation from the method it overloads.
When the Retention of the annotation annotated with the @Inherited annotation type is RetentionPolicy.RUNTIME, the reflection API enhances this inheritance. If we use java.lang.reflect to query annotation of an @Inherited annotation type, the reflective code inspection will start working: checking the class and its parent class until the specified annotation type is found, or the top level of the class inheritance structure is reached.
Example code:
-------------------------------------------------- ----------------------------------
Custom annotation:
When using @interface to customize annotations, the java.lang.annotation.Annotation interface is automatically inherited, and other details are automatically completed by the compiler. When defining annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, and 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 basic types, Class, String, or enum). You can declare the default value of a parameter through default.
Define annotation format:
public @interface annotation name {definition body}
Supported data types of 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 the above types
How to set the parameters in the Annotation type:
First, it can only be modified with the two access rights of public or default. For example, String value(); here the method is set to default type;
Second, parameter members can only use the eight basic data types of byte, short, char, int, long, float, double, boolean and data types such as String, Enum, Class, annotations, and arrays of these types. For example ,String value();The parameter member here is String;
Third, if there is only one parameter member, it is best to set the parameter name to "value" followed by parentheses. Example: The FruitName annotation in the following example has only one parameter member.
Simple custom annotations and examples of using annotations:
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Notes on fruit names
* @author peida
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
String value() default "";
}
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Fruit color annotation
* @author peida
*
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
/**
* Color enumeration
* @author peida
*
*/
public enum Color{BULE,RED,GREEN};
/**
* Color attributes
* @return
*/
Color fruitColor() default Color.GREEN;
}
import annotation.FruitColor.Color;
public class Apple {
@FruitName("Apple")
private String appleName;
@FruitColor(fruitColor=Color.RED)
private String appleColor;
public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleColor() {
return appleColor;
}
public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleName() {
return appleName;
}
public void displayName(){
System.out.println("The name of the fruit is: Apple");
}
}