Java元注解
元注解是指注解的注解,包括 @Retention @Target @Document @Inherited 四种。
1.@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) // 注解仅存在于源码中,在 class 字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在 class 字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在 class 字节码文件中存在,在运行时可以通过反射获取到
首
先要明确生命周期长度 SOURCE < CLASS < RUNTIME
,所以前者能作用的地方后者一定也能作用。一般如果需要在运行时去动态获取注解信息,那只能用 RUNTIME
注解;如果要在编译时进行一些预处理操作,比如生成一些辅助代码(如 ButterKnife),就用
CLASS 注解;如果只是做一些检查性的操作,比如 @Override 和 @SuppressWarnings,则可选用 SOURCE 注解。
2.@Target:定义注解的作用目标
源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) // 接口、类、枚举、注解
@Target(ElementType.FIELD) // 字段、枚举的常量
@Target(ElementType.METHOD) // 方法
@Target(ElementType.PARAMETER) // 方法参数
@Target(ElementType.CONSTRUCTOR) // 构造函数
@Target(ElementType.LOCAL_VARIABLE)// 局部变量
@Target(ElementType.ANNOTATION_TYPE)// 注解
@Target(ElementType.PACKAGE) /// 包
3.@Document:说明该注解将被包含在 javadoc 中
4.@Inherited:说明子类可以继承父类中的该注解
举例:
// 适用类、接口(包括注解类型)或枚举 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ClassInfo {String value(); }// 适用 field 属性,也包括 enum 常量
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldInfo {
int[] value();
}
// 适用方法
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodInfo {
String name() default "long";
String data();
int age() default 27;
}
这 3 个注解分别适用于不同的元素,并都带有不同的属性,在使用注解是需要设置这些属性值。
再定义一个测试类来使用这些注解:
@ClassInfo("Test Class") public class TestRuntimeAnnotation {@FieldInfo(value </span>= {1, 2<span style="color: rgba(0, 0, 0, 1)">}) </span><span style="color: rgba(0, 0, 255, 1)">public</span> String fieldInfo = "FiledInfo"<span style="color: rgba(0, 0, 0, 1)">; @FieldInfo(value </span>= {10086<span style="color: rgba(0, 0, 0, 1)">}) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span> i = 100<span style="color: rgba(0, 0, 0, 1)">; @MethodInfo(name </span>= "BlueBird", data = "Big"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> String getMethodInfo() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> TestRuntimeAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getSimpleName(); }
}
在代码中获取注解信息:
private void _testRuntimeAnnotation() {
StringBuffer sb = new StringBuffer();
Class<?> cls = TestRuntimeAnnotation.class;
Constructor<?>[] constructors = cls.getConstructors();
// 获取指定类型的注解
sb.append("Class 注解:").append("\n");
ClassInfo classInfo = cls.getAnnotation(ClassInfo.class);
if (classInfo != null) {
sb.append(Modifier.toString(cls.getModifiers())).append(" ")
.append(cls.getSimpleName()).append("\n");
sb.append("注解值:").append(classInfo.value()).append("\n\n");
}sb.append(</span>"Field注解:").append("\n"<span style="color: rgba(0, 0, 0, 1)">); Field[] fields </span>=<span style="color: rgba(0, 0, 0, 1)"> cls.getDeclaredFields(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Field field : fields) { FieldInfo fieldInfo </span>= field.getAnnotation(FieldInfo.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (fieldInfo != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { sb.append(Modifier.toString(field.getModifiers())).append(</span>" "<span style="color: rgba(0, 0, 0, 1)">) .append(field.getType().getSimpleName()).append(</span>" "<span style="color: rgba(0, 0, 0, 1)">) .append(field.getName()).append(</span>"\n"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"注解值: ").append(Arrays.toString(fieldInfo.value())).append("\n\n"<span style="color: rgba(0, 0, 0, 1)">); } } sb.append(</span>"Method注解:").append("\n"<span style="color: rgba(0, 0, 0, 1)">); Method[] methods </span>=<span style="color: rgba(0, 0, 0, 1)"> cls.getDeclaredMethods(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Method method : methods) { MethodInfo methodInfo </span>= method.getAnnotation(MethodInfo.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (methodInfo != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) { sb.append(Modifier.toString(method.getModifiers())).append(</span>" "<span style="color: rgba(0, 0, 0, 1)">) .append(method.getReturnType().getSimpleName()).append(</span>" "<span style="color: rgba(0, 0, 0, 1)">) .append(method.getName()).append(</span>"\n"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"注解值: ").append("\n"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"name: ").append(methodInfo.name()).append("\n"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"data: ").append(methodInfo.data()).append("\n"<span style="color: rgba(0, 0, 0, 1)">); sb.append(</span>"age: ").append(methodInfo.age()).append("\n"<span style="color: rgba(0, 0, 0, 1)">); } } System.out.print(sb.toString());
}