Annotation custom annotation

 

Meta annotations:

    1.@Target,
    2.@Retention,
    3.@Documented,
    4.@Inherited
  These types and the classes they support can be found in the java.lang.annotation package.

@Target:

  The values ​​(ElementType) are:

 

    1.CONSTRUCTOR: used to describe the constructor
    2.FIELD: used to describe the domain
    3.LOCAL_VARIABLE: used to describe the 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

@Retention:

  The values ​​(RetentionPoicy) are:

    1.SOURCE: valid in the source file (that is, the source file is retained)
    2.CLASS: valid in the class file (that is, the class is retained)
    3.RUNTIME: valid at runtime (that is, the runtime is retained)

 

Custom annotations:

  When using @interface to customize the annotation, the java.lang.annotation.Annotation interface is automatically inherited, and other details are automatically completed by the compiler. When defining an annotation, other annotations or interfaces cannot be inherited. @interface is used to declare an annotation where 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, enum). The default value of a parameter can be declared by default.

  Definition 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. Array of all the above types

 

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 using the annotation. The value of the annotation element of a non-primitive type cannot be null. Therefore, it is a common practice to use an empty string or 0 as the default value. This constraint makes it difficult for the processor to express the presence or absence of an element, because in the declaration of each annotation, all elements are present and have corresponding values. In order to circumvent this constraint, we can only define some special Values, such as the empty string or a negative number, indicate that an element does not exist at a time, which has become an idiom when defining annotations.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996417&siteId=291194637