- 如果你对 Java Annotation (就是形如 @Xxx, 但并不写在注释块里的语法结构) 熟悉的话, 也许你已经知道, Java 标准库内置了四种用于修饰自定义的 Annotation 的 Meta Annotation:
(
2016-04-24 补丁:
原先写的时候不知道. 其实从 Java 8 开始, java.lang.annotation 包中加入了 @Native 和 @Repeatable.
- @Repeatable 是个 "Meta Annotation" (即 @Target(value=ANNOTATION_TYPE)).
- @Native 不是. 它 @Target(value=FIELD).
)
@Documented (功能 = "若 @Documented 修饰 Annotation A, 则 @A 将在生成的 Javadoc 中出现, 就像 Javadoc 内置的 @return 什么的.")
@Inherited (功能 = "若 @Inherited 修饰 Annotation A; 而 @A 又修饰类 C1 或修饰{类 C2 的方法 M}; 且 CC1 继承 C1, CC2 继承 C2, 则 @A 将影响 CC1 或 {CC2 中未被 override 的方法 M}. 否则 @A 不影响子类或其中的方法.")
( ref http://wangyu.iteye.com/blog/210818 )@Retention (功能 = "决定 @Retention 所修饰的 Annotation 是仅存在于代码中 (但不参与编译), 还是保持到编译后的 class 文件 (但不被 JVM 解释), 还是保持到运行时 (被 JVM 解释; 能被反射获取)."
@Target (功能 = "约束 @Target 所修饰的 Annotation 能修饰哪些语法结构, 如 PACKAGE, ANNOTATION_TYPE, etc.")
它们被定义在 java.lang.annotation 包里. 前几天学 Annotation 的时候, 我的 Meta-欲勃发, 想到 "Meta annotation 也是 annotation, 也会被 meta annotation 修饰"...
于是看了一下它们的定义. 四个写在一起是这样的:
package java.lang.annotation;
# @Target
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
# @Retention
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
# @Documented
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
# @Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
OK! 得到一个结论...
四个 Meta Annotation 都被修饰成:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
用简写 (譬如 R 表示 @Retention(RetentionPolicy.RUNTIME)) 和箭头表示四个 Meta Annotation 的依赖关系, 形如下图:
↙↖
↘↗
D
↗ ↑ ↖
↗ │ ↘ ↖ ↙↖
I←----┼----→R |
↘ ↓ ↗ ↙ ↘↗
↘│ ↙
T
↙↖
↘↗
#ASCII Crap
第一节完...
To be continued...