Custom annotations (Annontation)

Table of contents

1. Annotation definition

2. Meta annotation definition

3. Custom annotations (customized annotations with the same name will overwrite the original annotations)

4. Annotation architecture (introduction to meta-annotation parameters)


1. Annotation definition

        Annotations are used to associate any information or metadata (metadata) with program elements (classes, methods, member variables, etc.). It is a kind of explanation, configuration, and descriptive information, which has nothing to do with specific business and will not affect normal business logic. Annotations are used as tags in Java programs, and there are specific programs to discover tags

2. Meta annotation definition

        Meta-annotations are annotations specially used to annotate other annotations. It sounds a bit convoluted, but they are actually annotations specially provided for custom annotations. java.lang.annotation provides four meta-annotations:

  • @Documented – whether the annotation will be included in the JavaDoc
  • @Retention – when to use this annotation
  • @Target – where the annotation is used
  • @Inherited – Whether to allow subclasses to inherit the annotation
  • @Repeatable - Whether the annotation can be repeated, introduced by jdk1.8

3. Custom annotations (customized annotations with the same name will overwrite the original annotations)

Create a new annotation class 

@Documented//Describe that the annotation can appear in javadoc 

@Target(ElementType.TYPE)//Specify the type of Inherited as ElementType.TYPE. 

@Retention(RetentionPolicy.RUNTIME)//The policy specifying Inherited is RetentionPolicy.RUNTIME 

@Inherited//The Annotation marked by it will have inheritance. 
public @interface MyAnnotation1 { 
}

4. Annotation architecture (introduction to meta-annotation parameters)

 

 

        ①, (01) One Annotation is associated with one RetentionPolicy . It can be understood as: every Annotation object will have a unique RetentionPolicy attribute.

               (02) 1 Annotation is associated with 1~n ElementTypes . It can be understood as: For each Annotation object, there can be several ElementType attributes.

               (03) Annotation has many implementation classes, including: Deprecated, Documented, Inherited, Override and so on. Each implementation class of Annotation is "associated with 1 RetentionPolicy" and "associated with 1~n ElementTypes".

        ②, Annotation is an interface.

"Each Annotation" is associated with "1 RetentionPolicy" and associated with "1~n ElementType". It can be easily understood as: every Annotation object has a unique RetentionPolicy attribute; as for the ElementType attribute, there are 1~n.

        ③. ElementType is an Enum enumeration type, which is used to specify the type of Annotation.

"Each Annotation" is associated with "1~n ElementTypes". When Annotation is associated with an ElementType, it means: Annotation has a certain purpose.

  • ElementType.TYPE: class, interface, annotation, enum
  • ElementType.CONSTRUCTOR: Constructor
  • ElementType.FIELD: constants of member variables, objects, properties, enumerations
  • ElementType.LOCAL_VARIABLE: local variable
  • ElementType.METHOD: method
  • ElementType.PACKAGE: 包
  • ElementType.PARAMETER: parameter
  • ElementType.ANNOTATION_TYPE): Annotation
  • ElementType.TYPE_PARAMETER: type parameter, indicating that this annotation can be used before the declaration of Type, introduced by jdk1.8.
  • ElementType.TYPE_USE: A type annotation, indicating that this annotation can be used in all places where Type is used (such as: generics, type conversion, etc.), introduced by jdk1.8.

        ④. RetentionPolicy is an Enum enumeration type, which is used to specify the strategy (type) of Annotation. In layman's terms, the scope of Annotation of different RetentionPolicy types is different.

                a. If the type of the Annotation is SOURCE , it means: the Annotation only exists during the processing of the compiler, after the processing of the compiler, the Annotation is useless. For example, the "@Override" flag is an Annotation. When it modifies a method, it means that the method overrides the method of the parent class; and syntax checking will be performed during compilation! After the compiler has finished processing, "@Override" has no effect.

                b. If the type of Annotation is CLASS , it means: the compiler stores the Annotation in the .class file corresponding to the class, which is the default behavior of Annotation.
                c. If the type of Annotation is RUNTIME , it means: the compiler stores Annotation in class

Guess you like

Origin blog.csdn.net/qq_52240237/article/details/132183592