b station learning to meet crazy God -- Note 2

Meta annotation

Meta annotation is used to interpret other annotations. Four standard mate annotation types are defined in Java, which are used to explain other annotation types
These types and the classes they support can be found in the java.lang.annotation package. They are (@ Target,@Retention,@Documented,@Inherited)
1.@Target: used to describe the scope of use of annotations, that is, where the described annotations can be used

2.@Retention: indicates the level at which the annotation information needs to be saved, which is used to describe the annotation life cycle. (source < class() < runtime))

3.@Documented: indicates that the annotation will be included in javadoc, that is, whether to generate document annotations

4. @ inherited (inherited): indicates that the subclass can inherit the annotation of the parent class

Well, with all that said, let's define a meta annotation

package com.ffyc.kuang.annotation;

import java.lang.annotation.*;

//Customize a meta annotation

public class Annotationtext2 {
    //Here we see that the annotation we defined is OK, and the program does not report an error
    @MyAnnotation
    public void test() {

    }


}

//First, let's define an annotation:
//@Target indicates where our annotations can be used
//The values in value have been defined. We only need to select what we want. Here, we select method (method), which means that the annotation can only be run in the method. When we use the annotation in other places, the program will report an error
//The ElementType here is an array. When we need to pass multiple values, we can use value = [ElementType.METHOD,ElementType. (parameter value 1),,ElementType. (parameter value 2)];
@Target(value = ElementType.METHOD)
//Retention indicates where our annotation is still valid
//The RetentionPolicy here is an array with only three values: source, class and runtime. Where source < class < runtime
@Retention(value = RetentionPolicy.RUNTIME)
//Documented: indicates whether the annotation will be included in the javadoc, that is, whether to generate a document annotation
@Documented
//Inherited: indicates that the subclass can inherit the annotation of the parent class
@Inherited
@interface MyAnnotation {

}

Finally, let's talk about the parameters of @ target and value in @ retention. First, let's look at the source code:
1. Let's take a look at @ Target source code:

@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();
}

Here we can see that the value we want to pass in @ Target is an ElementType array
Let's look at the source code of ElementType []:

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
}

Here, we can know that there are 10 elements defined in the ElementType array, and their meaning can also be easily understood according to their names, so we can pass in the corresponding elements according to the values we need at that time

2. Let's take a look at the @ Retention source code:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

Here we can see that the value we want to pass in @ Retention is a RetentionPolicy array
Let's look at the source code of RetentionPolicy []:

public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

There are only three elements defined in the RetentionPolicy array. They are source, class and runtime. Here, I need to explain their relationship source < class < runtime,
When the value we pass in is RUNTIME, they also include source and class by default. Therefore, the value here can only pass one number
When we know this, we just pass in the corresponding element

Tags: Java

Posted on Fri, 19 Nov 2021 14:56:25 -0500 by plehmurof