JDK1.5 begins with the addition of the annotation feature, which can be used in declarations of classes, construction methods, member variables, methods, parameters, and so on. This feature does not affect the program's operation, but it can have an impact on accessibility tools such as compiler warnings.
First, define a annotation
public @interface username{}
Define an annotation using the @interface keyword
Second, add members
public @Interface username{ String value (); }
Member types support: String\class\primitive\enumerated\annotation, and Arrays of the listed types;
Third, add the default value
public @Interface username{ default "default value"; }
Iv. specifying the types that can be annotated
public @interface username{}
Applies to annotations on the method, for example:
Class car{ = "Laye") publicvoid Drive () {} }
The types of ElementType are:
- Annotation_type: indicated for ANNOTATION type;
- Type: Represents the class, interface, and annotation and enumeration types;
- CONSTRUCTOR: expressed for construction methods;
- FIELD: Represents the use of member variables and enumeration constants;
- Method: means used;
- PARAMETER: indicated for the parameter;
- Local_variable: expressed for local variables;
- Package: Indicates the use of packages;
V. Specify the valid range of annotations
@Retention (retentionpolicy.runtime) public @interface username{}
Indicates valid at run time, retentionpolicy optional type is as follows:
- SOURCE: Indicates that it is not compiled into the class file;
- Class: Represents the compilation to the class file, but the runtime does not load;
- Represents the load at run time;
Vi. Access Annotations
The Java.lang.reflect package has a class: Accesibleobject, which method\field\constructor the parent class at the same time, and it implements the Annotatedelement interface. Therefore, Method\field\constructor contains an interface method for accessing annotations;
Specific examples are as follows:
@Target (Elementtype.field)//annotation type is variable@Retention (Retentionpolicy.runtime)//valid range is run-time Public@inteface username{//Statement AnnotationsString value () default "no Body";//Add Member}classcar{@UserName (value= "Lay")//annotations on the variable PublicString owner;//Default Value Public Static voidMain (string[] args) throws exception{Class C= Car.class;//Get class typeField field = C.getfield ("owner");//get the corresponding variableUserName UserName = (UserName) field.getannotation (Username.class);//Get AnnotationsSystem.out.println (Username.value ());//get member variables for annotationsCar car =NewCar ();//Get InstanceField.set (Car, Username.value ());//set the value of the instance's owner variable }}
The above is an example of accessing field, such as method and constructor;
Common methods:
- Isannotationpresent (class Class): Whether to add annotations of the specified type;
- Getannotation (class Class): Gets the annotations of the specified type;
- Getannotations (): Get all types of annotations;
- Getparameterannotations (): Get annotations for all parameters;
Annotation features of Java