[Java] @interface meta-annotation practical explanation @Retention, @Target, @Documented, @Inherited, @Repeatable

Table of contents

The meaning of meta-annotations

@Retention

@Target

@Documented

@Inherited

@Repeatable


@Retention        @Target        @Documented        @Inherited       @Repeatable


The meaning of meta-annotations

        Used to describe the location, scope, inheritance, and document extraction of annotations.


@Retention

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

//  @Retention(RetentionPolicy.SOURCE) //  MyAnno注解在源代码中有效
//  @Retention(RetentionPolicy.CLASS) //  MyAnno注解在类文件中有效
@Retention(RetentionPolicy.RUNTIME) //   MyAnno注解在运行时有效

public @interface MyAnno {
    public String value() default "";
}
@MyAnno("我是一个注解")
public class AnnoTest {
    public static void main(String[] args) {
        AnnoTest  at= new AnnoTest();
        MyAnno annotate = at.getClass().getAnnotation(MyAnno.class);
        System.out.println(annotate.value());
    }
}

输出:我是一个注解

@Target

Indicates which elements this annotation can be used on

ElementType.ANNOTATION_TYPE: Annotation

ElementType.CONSTRUCTOR: Constructor

ElementType.FIELD: fields, enumeration constants

ElementType.LOCAL_VARIABLE: local variable

ElementType.METHOD: Method

ElementType.PACKAGE:                         包

ElementType.PARAMETER: formal parameters

ElementType.TYPE: Class, interface, annotation, enumeration

ElementType.TYPE_PARAMETER: Type parameter

ElementType.TYPE_USE: Type use 

@Target({ElementType.METHOD,ElementType.FIELD})
public @interface MyAnno {
    public String value() default "";
}

@Documented

Annotation information can be extracted into API documentation by the javadoc tool

@Documented
public @interface MyAnno {
    public String value() default "";
}

IDE path: Tools -- Generate JavaDoc


@Inherited

Modified annotations are inherited

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String value() default "";
}
@MyAnnotation("父亲")
public class Father {}
public class Child extends Father{
	public static void main(String[] args) {
		Class<Child> child=Child.class;
		MyAnnotation anno = child.getAnnotation(MyAnnotation.class);
		System.out.println(anno.value());
	}
}
输出:父亲

@Repeatable

The declared annotation can be repeatedly annotated on the corresponding element, and this repeatable annotation can be included through the value of another annotation.

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME) //
@Target(ElementType.TYPE)
public @interface MyAnnos {
    MyAnno[] value() ;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(MyAnnos.class)
public @interface MyAnno {
    String value() default "";
}
import com.prv.anno.MyAnno;

@MyAnno("anno")
@MyAnno("lala")
public class AnnoTest {
    public static void main(String[] args) {
        MyAnno[] fileTypes = new AnnoTest().getClass().getAnnotationsByType(MyAnno.class);
        for (MyAnno myAnno : fileTypes) {
            System.out.println(myAnno.value());
        }
    }
}
输出:
      anno
      lala

Guess you like

Origin blog.csdn.net/xudahai513/article/details/126898673