Java annotation learning notes basic concept understanding

Concept of annotation

Annotation, also known as Metadata, is a new feature of Java 5. JDK5 introduces Metadata and can easily call annotations. Annotations are at the same level as classes, interfaces and enumerations, and can be applied to the declarations of packages, types, construction methods, methods, member variables, parameters and local variables to explain and annotate these elements.

Syntax and definition form of annotation

  1. Defined with @ interface keyword
  2. Annotations contain members that are declared as parameterless methods. Its method name and return value define the name and type of the member.
  3. Member assignment is in the form of @ Annotation(name=value).
  4. Annotation needs to indicate the life cycle of annotation, the modification goal of annotation and other information, which is realized through meta annotation.

Example analysis of single annotation

In Java The Target annotation defined in lang.annotation is used to explain:
@Target: defines the target (SCOPE) of the annotation

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = { ElementType.ANNOTATION_TYPE } )
public @interface Target {
    ElementType[] value();
}

The source code analysis is as follows:
First: meta annotation @ Retention. The value of member value is retentionpolicy RUNTIME.
Second: meta annotation @ Target, member value is an array, which is assigned in the form of {}, and the value is ElementType ANNOTATION_ TYPE
Third: the member name is value and the type is ElementType []
In addition, it should be noted that if the member name is value, it can be abbreviated in the assignment process. If the member type is array, but only one element is assigned, it can also be abbreviated. For example, the abbreviation above is:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}

@Target annotation scope enumeration class:

@Target(ElementType.TYPE)
package java.lang.annotation;

public enum ElementType {
    /** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

They are:

  • TYPE: refers to the annotation used on the class, interface (including annotation) or enum.
  • field: refers to the field attribute, including the annotation used by enum constant.
  • METHOD: refers to the annotation used on the METHOD declaration.
  • PARAMETER: refers to the annotation used on the PARAMETER.
  • CONSTRUCTOR: refers to the annotation used in the CONSTRUCTOR.
  • LOCAL_VARIABLE: refers to the annotation used on local variables.
  • ANNOTATION_TYPE: refers to the meta annotation used on the annotation.
  • PACKAGE: refers to the annotation used on the PACKAGE.

Classification of annotations

There are two classifications of annotations:

The first method is defined according to how the annotation is created

  • Basic built-in annotations refer to several annotations provided by Java, such as @ Override, Deprecated, @ SuppressWarnings, etc;

  • Meta annotation refers to the annotation responsible for annotating other annotations. JDK 1.5 and later versions define four standard meta annotation types, as follows:

    @Target: indicates the modification target of the annotation. The scope value range can be referred to 👆 Described in the previous chapter Chapter content

    @Retention: refers to the length of time an Annotation is retained, indicating the life cycle of the Annotation. There are three values through retention policy, as follows:

    • SOURCE: valid in SOURCE file (i.e. SOURCE file retention)
    • Class: valid in class file (i.e. class reserved)
    • RUNTIME: valid at RUNTIME (i.e. reserved at RUNTIME)

    @Documented: tag annotations, used to describe that other types of annotations should be used as the public API of the marked program members, so they can be documented by tools such as javadoc

    @Inherited: tag annotation, which allows subclasses to inherit the annotation of the parent class

  1. User defined annotations can be customized as needed. The above meta annotation is required for user-defined annotations

The second method is defined according to the annotation life cycle

  1. Source code annotation (RetentionPolicy.SOURCE)
  2. Compile time annotation (RetentionPolicy.CLASS)
  3. Runtime annotation (RetentionPolicy.RUNTIME)

The annotation needs to indicate the annotation life cycle. This information is realized through the meta annotation @ Retention. The annotation value is the Retention policy of enum type, including the following situations

public enum RetentionPolicy {
    /**
     * Annotations are only kept in the source file. When Java files are compiled into class files, annotations are discarded
     * This means that an Annotation exists only during compiler processing and is useless after the compiler processes it
     */
    SOURCE,
 
    /**
     * Annotations are retained in the class file, but are discarded when the jvm loads the class file, which is the default life cycle
     */
    CLASS,
 
    /**
     * The annotation is not only saved in the class file, but also exists after the jvm loads the class file,
     * Save to the class object, which can be obtained through reflection
     */
    RUNTIME
}

Keywords: Java Back-end

Added by billynastie on Fri, 18 Feb 2022 17:28:12 +0200