Java Annotate(二)
@Inherited@Native@Repeatable
Java Annotate(二) @Inherited被注释的注解,拥有的了继承上注解的传递性
注解a使用在b类上,b的子类c也隐性的拥有了注解a(子类c通过反射可以获得注解b)
@Native@documented
//作用与接口、类、枚举
@Target(ElementType.FIELD)
//只保留在源码中
@Retention(RetentionPolicy.SOURCE)
public @interface Native {
//不存在参数,只是用于标识作用
}
@Repeatable
被注释的注解在一个位置可以重复使用
没使用Repeatable的注解
以过时 Deprecated为例:
@documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
使用兩次后
使用Repeatable的注解
org.junit.jupiter.api 下的 Tag
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
//使用了Repeatable
@Repeatable(Tags.class)
@API(status = STABLE, since = "5.0")
public @interface Tag {}
使用多次



