版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Goodbye_Youth/article/details/84314928
目前,Java中存在五个元注解,分别是 @Target、@Retention、@Documented、@Inherited、@Repeatable
一、元注解的定义
- 所谓元注解,其主要作用就是负责注解其他注解,为其他注解提供了相关的解释说明
二、元注解的使用
- @Target
- 用于指定注解的使用范围
- ElementType.TYPE:类、接口、注解、枚举
- ElementType.FIELD:字段、枚举常量
- ElementType.METHOD:方法
- ElementType.PARAMETER:形式参数
- ElementType.CONSTRUCTOR:构造方法
- ElementType.LOCAL_VARIABLE:局部变量
- ElementType.ANNOTATION_TYPE:注解
- ElementType.PACKAGE:包
- ElementType.TYPE_PARAMETER:类型参数
- ElementType.TYPE_USE:类型使用
- @Retention
- 用于指定注解的保留策略
- RetentionPolicy.SOURCE:注解只保留在源码中,在编译时会被编译器丢弃
- RetentionPolicy.CLASS:(默认的保留策略) 注解会被保留在Class文件中,但不会被加载到虚拟机中,运行时无法获得
- RetentionPolicy.RUNTIME:注解会被保留在Class文件中,且会被加载到虚拟机中,可以在运行时获得
- @Documented
- 用于将注解包含在javadoc中
- 默认情况下,javadoc是不包括注解的,但如果使用了@Documented注解,则相关注解类型信息会被包含在生成的文档中
- @Inherited
- 用于指明父类注解会被子类继承得到
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface ParentAnnotation {
String key();
String value();
}
@ParentAnnotation(key = "key", value = "value")
public class Parent {
}
public class Son extends Parent {
public static void main(String[] args) {
Annotation[] annotations = Son.class.getAnnotations();
// [@com.ParentAnnotation(key=key, value=value)]
System.out.println(Arrays.toString(annotations));
}
}
- @Repeatable
- 用于声明标记的注解为可重复类型注解,可以在同一个地方多次使用
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(RepeatableAnnotation.class)
public @interface TestAnnotation {
String key();
String value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface RepeatableAnnotation {
TestAnnotation[] value();
}
@TestAnnotation(key = "key1", value = "value1")
@TestAnnotation(key = "key2", value = "value2")
public class Test {
public static void main(String[] args) {
RepeatableAnnotation annotation = Test.class.getAnnotation(RepeatableAnnotation.class);
TestAnnotation[] testAnnotations = annotation.value();
for (TestAnnotation testAnnotation : testAnnotations) {
// @com.TestAnnotation(key=key1, value=value1)
// @com.TestAnnotation(key=key2, value=value2)
System.out.println(testAnnotation);
}
}
}