1.1 interpretation:

Also called metadata, a code-level specification. With packages, classes, methods, fields, local variables, method parameters, etc. Used to illustrate these elements.

1.2 Classification of effects:

  1. Document: Use the Javadoc tool to generate doc documents.
  2. Code analysis: Use annotations through reflection.
  3. Compile checks: Let the compiler implement basic compile checks. Such as Override

1.3 Predefined annotations in the JDK:

  1. Override: Indicates whether the method inherits its parent class.
  2. Deprecated: Indicates that some elements are Deprecated.
  3. @suppresswarnings: SuppressWarnings. You need to pass a parameter, for example@SuppressWarnings("all")

1.4 Custom annotations

Format:

Public @interface Xxxx {... }Copy the code

Nature:

An Annotation is essentially an interface that inherits the Annotation interface by default.

Properties:

Member methods that can be defined in an interface. Requirements:

  1. Property return value type: basic data type, String, enumeration, annotation, array of the above types.
  2. Each attribute needs to be assigned:
> - An annotation that defines an attribute that requires an assignment or a value specified by default within the attribute. > - If there is only one attribute and the attribute name is value, define the value directly, such as @suppressWarnings above. > - Array assignments are wrapped with {}.Copy the code

1.5 yuan notes

Explanation: The annotation that describes the annotation when defining the annotation.

@target: Describes where the annotation can be used.

  • ElementType values:
    • TYPE: applies to the class
    • METHOD: applies to the METHOD
    • FIELD: applied to a variable

@Retention: Describes the phase in which annotations are retained.

  • RetentionPolicy values
    • SOURCE: This element is not retained in the class bytecode file.
    • CLASS: This element is retained in the CLASS bytecode file.
    • RUNTIME: This element remains in the class bytecode file and is read by the JVM.

Document: Describes whether the annotation is extracted into the API Document. Inherited: Describes whether an annotation is Inherited by a child class.

1.6 Parsing Annotations

  1. Gets the object where the annotation is defined (the bytecode class file object of this class; Method Object, filed object)
  2. Get the annotation object via getAnnotation.
    • Essentially, a subclass implementation object of the annotation interface is generated in memory.
  3. Call the abstract method in the annotation object to get the return value.