【Java】@interface 元注解 实操详解 @Retention、@Target、@Documented 、@Inherited、@Repeatable

目录

元注解的含义

@Retention

@Target

@Documented

@Inherited

@Repeatable


@Retention        @Target        @Documented        @Inherited       @Repeatable


元注解的含义

        用来对注解的位置、作用范围、继承、文档抽取进行描述


@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

表示该注解可以给哪些元素使用

扫描二维码关注公众号,回复: 16729392 查看本文章

ElementType.ANNOTATION_TYPE:        注解

ElementType.CONSTRUCTOR:              构造方法

ElementType.FIELD:                                字段、枚举常量

ElementType.LOCAL_VARIABLE:           局部变量

ElementType.METHOD:                          方法

ElementType.PACKAGE:                         包

ElementType.PARAMETER:                   形参

ElementType.TYPE:                                类、接口、注解、枚举

ElementType.TYPE_PARAMETER:        类型参数

ElementType.TYPE_USE:                       类型使用 

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

@Documented

注解信息可以被 javadoc 工具提取到API文档中

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

IDE路径:Tools -- Generate JavaDoc


@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

声明的注解可以重复注解在对应的元素上,可以通过另一个注解的值来包含这个可重复的注解。

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

猜你喜欢

转载自blog.csdn.net/xudahai513/article/details/126898673