Several common annotations in javase

Several common annotations in javase

Insert image description here

@retention

The Chinese meaning is to maintain, to retain, ****

Specify the scope of the annotation

illustrate:

Only one annotation can be modified to specify how long the annotation can be retained. @retention contains a member variable of the retentionpolicy type. When using @retention, you must specify a value for the value member variable:

Three values ​​of @retention

@Retention(RetentionPolicy.RUNTIME)
@Retention(RetentionPolicy.SOURCE)
@Retention(RetentionPolicy.CLASS)
以上三个就是我们的三种取值
    1.SOURCE编译器使用后,会直接丢弃这个策略的注释
    2.CLASS编译器会把注释记录存在class文件种种那个,当运行Java程序时,jvm不会保留注释,
    3.RUNTIME这个编译器可以把注释记录存在class文件中,当运行Java程序的时候,jvm会保留注释,程序可以通过反射获取该注释。

@target

Specify where this annotation will be used

Let’s take a look at the source code of target

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-QnOHbXF9-1643505693475) (C:\Users\86189\AppData\Roaming\Typora\typora-user-images\ image-20220130090031609.png)]

    /** 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

The above is the range of values ​​that target can take.

Let’s take a look at where @deprecated can be written

Let’s just look at the value of his target.

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-7FLyCg7x-1643505693476) (C:\Users\86189\AppData\Roaming\Typora\typora-user-images\ image-20220130090352773.png)]

@documented

Used to specify the class modified by this meta-annotation, which is extracted as a document by the javadoc tool, that is, when the document is generated, the annotation can be seen.

Note: When we set the documented annotation, we need to set the retention value to runtime.

This will ensure that the annotation will not disappear when you see it when generating the help document.

Insert image description here

@inherited

Subclasses will inherit the annotations of the parent class

Suppose there is an annotation a

modified by inherited

Then a class A has an annotation a, and then class b inherits class a

Then b also has an annotation of a

Annotation class

Insert image description here

@deprecated

The English translation is obsolete, which means that our class is a system reminder that this class is not recommended for use, but it can still be used.

Adding this annotation to our class can indicate whether this class is outdated.

hvg6-1643505693477)]

@deprecated

The English translation is obsolete, which means that our class is a system reminder that this class is not recommended for use, but it can still be used.

Adding this annotation to our class can indicate whether this class is outdated.

Guess you like

Origin blog.csdn.net/SpongeBob_shouse/article/details/122750252