Java custom annotation
1. Annotation definition
- Annotations are metadata that can be added to java code. Classes, methods, variables, parameters and packages can be modified with annotations. Annotations have no direct impact on the code they decorate.
2. Scope of use
- Annotations have many uses
- Provide information for the compiler
- Annotations can be used by the compiler to detect errors or suppress warnings
- Compile time and deployment time processing
- Software tools can process annotation information to generate code, XML files, etc
- Runtime processing
- Some annotations can be detected at run time
3. User defined annotation
3.1 basic grammar
-
public @interface MyAnnotation { /** * @Author: Hjx * @Date: 2021/8/16 16:57 */ public @interface MyAnnotation { /** * Constructs, attributes, methods, etc. will be defined in the class, but only annotation type elements can be defined in the user-defined annotation */ /** * Access modifiers can only be defined as public and default, and the default public is not written */ public String name(); /** * default Set the default value. If the default value is not set, it needs to be added manually */ int num() default 1; }
3.2 precautions
- Constructs, attributes, methods, etc. will be defined in the class, but only annotation type elements can be defined in the user-defined annotation
- Access modifiers can only be defined as public and default, and the default public is not written
- Default sets the default value. If the default value is not set, it needs to be added manually
- Member parameters can only use eight basic types (byte, short, char, int, long, float, double, boolean) and data types such as String, Enum, Class, annotations, and their arrays.
- The definition of annotations is represented by @ interface, and all annotations will automatically inherit Java Lang. annotation interface, and cannot inherit other classes or interfaces.
- To obtain the Annotation information of class methods and fields, you can only obtain the Annotation object through Java reflection technology.
- Annotations can have no defined members and only be used for identification.
- () is not a place to define method parameters, nor can any parameters be defined in parentheses. It is just a special syntax;
3.3 common meta notes
- Meta annotations are annotations specifically used to annotate other annotations.
@Target
-
It is used to limit where annotations are used, for example, only in classes, methods, attributes or construction methods
-
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Target { /** * Returns an array of the kinds of elements an annotation type * can be applied to. * @return an array of the kinds of elements an annotation type * can be applied to */ ElementType[] value(); }
-
Property is an enumerated array of ElementType type
-
public enum ElementType { /** Class, interface (including annotation type), or enum declaration */ /** Class, interface, annotation type, enumeration */ TYPE, /** Field declaration (includes enum constants) */ /** attribute */ FIELD, /** Method declaration */ /** method */ METHOD, /** Formal parameter declaration */ /** Method formal parameters */ PARAMETER, /** Constructor declaration */ /** Construction method */ CONSTRUCTOR, /** Local variable declaration */ /** local variable */ LOCAL_VARIABLE, /** Annotation type declaration */ /** annotation type */ ANNOTATION_TYPE, /** Package declaration */ /** package */ PACKAGE, /** * Type parameter declaration * * @since 1.8 */ /** Used to dimension type parameters */ TYPE_PARAMETER, /** * Use of a type * * @since 1.8 */ /** Used to label any type */ TYPE_USE }
@Retention
-
The lifecycle used to decorate custom annotations
-
There are three stages in the annotation life cycle: java source file, compiled class file, and runtime
-
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.ANNOTATION_TYPE) public @interface Retention { /** * Returns the retention policy. * @return the retention policy */ RetentionPolicy value(); }
-
Property is an enumeration of type RetentionPolicy
-
public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. */ /** Annotations can only appear in the source code and are ignored by the compiler */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior. */ /** Annotations will be compiled into the class file by the compiler and will not be retained by the virtual machine at run time, */ CLASS, /** * 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. * * @see java.lang.reflect.AnnotatedElement */ /** It will be compiled into the class file at compile time, and will be reserved by the virtual machine at run time, which can be obtained through reflection */ RUNTIME }
@Documented
- Indicates whether to add information about this annotation to the javadoc document
@Inherited
- If the annotation is defined on the parent class, its child classes can inherit automatically. Note that the annotation can only take effect on the class and is not valid for methods and properties.
3.4 special grammar
- If the annotation has no annotation type element, it can be directly written as @ annotation name when used, and () can be omitted
- If there is only one annotation type element, it can be named value. When used, you can directly @ annotation name (annotation value)
- If the annotation type element is an array and only one is added during use, {} can be omitted and written as @ annotation name (type name = type value)