Several meta-annotations that Java needs to use to create custom annotations

@Target: This annotation is used to identify where the current annotation can be placed.

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    
    
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

The required property is ElementTypean array of a type that is an enumeration class.

public enum ElementType {
    
    
    TYPE,  // 可以放在类、接口

    FIELD, // 成员变量

    METHOD, // 方法

    PARAMETER, // 入参

    CONSTRUCTOR, // 构造函数

    LOCAL_VARIABLE, // 本地变量

    ANNOTATION_TYPE, 

    PACKAGE,
    
    TYPE_PARAMETER,

    TYPE_USE
}

@Retention: Used to identify the annotation life cycle scope, the parameter is RetentionPolicy

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    
    
    RetentionPolicy value();
}
RetentionPolicy
/**
 * Annotation retention policy.  The constants of this enumerated type
 * describe the various policies for retaining annotations.  They are used
 * in conjunction with the {@link Retention} meta-annotation type to specify
 * how long annotations are to be retained.
 *
 * @author  Joshua Bloch
 * @since 1.5
 */
public enum RetentionPolicy {
    
    
    /**
     * 指的是该注解将在编译期被丢弃,注解只保留在源文件,当Java文件编译成class文件的时候,注解被遗弃;
     */
    SOURCE,

    /**
     * 注解信息将会在编译期被记录、但是不在代码在VM运行时维持,注解被保留到class文件,但jvm加载class文件时候被遗弃,这是默认的生命周期;
     */
    CLASS,

    /**
     * 注解将会在VM执行时维持注解信息,贯穿整个生命周期,注解不仅被保存到class文件中,jvm加载class文件之后,仍然存在;需要用到反射都需要使用这个
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

Life cycle SOURCE < CLASS < RUNTIME

@Documented: The Javadoc tool will include the annotation information of this annotation tag element in the javadoc. By default, annotation information is not included in Javadoc

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
    
    
}

Example:

package org.springmorning.demo.javabase.annotation.meta;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;

@Documented
@Inherited
public @interface Book {
    
    
    //书名
    String name();
    //出版日期
    String publishedDate();
    //作者
    String author();
}

Use the @Book annotation to mark the class DocumentAnnotation, and the content of the Book mark element is as follows:

package org.springmorning.demo.javabase.annotation.meta;

@Book(
        name = "Spring in Action",
        author = "Craig Walls",
        publishedDate = "2008-10-1"
)
public class DocumentAnnotation {
    
    }

Open cmd and enter the javadoc command:

javadoc -d D:\doc org.springmorning.demo.javabase.annotation.meta -encoding utf-8 -charset utf-8

illustrate:

-d D:\docIndicates: the doc file input directory is the doc folder of the D drive;

org.springmorning.demo.javabase.annotation.metaIndicates that all classes in this package need to generate java doc html files;

-encoding utf-8Indicates: the java code is written in utf-8 character encoding;

-charset utf-8Means: java doc html file is UTF-8 character encoding.

Running result:
insert image description here
The java doc document generated by the javadoc command:

insert image description here
Open index.html in the browser to view the content inside, and you can see the document description of the @Book annotation:
insert image description here
click "DocumentAnnotation" in the left column to open the document description of the DocumentAnnotation class, and you can see the annotation content of the @Book mark displayed :
insert image description here
If the @Book annotation is not marked by @Document, then the annotation information of the DocumentAnnotation class marked by the @Book annotation will not be included in the java doc html document: as shown in the figure below: java8 supports in the same insert image description here
@Repeatableplace (a certain method or a certain method) classes, etc.) plus the same annotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
    
    
    /**
     * Indicates the <em>containing annotation type</em> for the
     * repeatable annotation type.
     * @return the containing annotation type
     */
    Class<? extends Annotation> value();
}

Let's create a custom annotation first:

@Retention(RetentionPolicy.RUNTIME)
@Target({
    
    ElementType.METHOD, ElementType.TYPE})
@Repeatable(CustomAnnotations.class)
public @interface CustomAnnotation {
    
    
    int type();
}

The attribute of this annotation is Class<? extends Annotation>, that is to say, a class (annotation class) that is inherited is needed here Annotation. Another custom annotation is created here, which is called the parent annotation later, and the previous one is called the sub-annotation. The sample code is as follows:

@Retention(RetentionPolicy.RUNTIME)
@Target({
    
    ElementType.METHOD, ElementType.TYPE})
public @interface CustomAnnotations {
    
    
    CustomAnnotation[]  value();
}

The parent annotation must have the following characteristics:
1: The scope must be greater than or equal to the child annotation
2: The period of the parent annotation is smaller or the same as that of the child annotation (Note: SOURCE (source code) < CLASS (byte code) < RUNTIME (run ))
3: The value type of the parent annotation is an array of sub-annotation types

@Inherited: Annotation mark Other annotations are used to indicate that the marked annotations can be automatically inherited.

Note: This annotation is only valid for the superclass marked by the annotation, not for the interface.

Guess you like

Origin blog.csdn.net/qq_16733389/article/details/126745015