注释怎么可能是对自身的注释? [英] How is it possible that an annotation can be an annotation to itself?
问题描述
我在浏览 JDK 7 的文档时注意到包 java.lang.annotation
中有一个名为 @Target
的注释.该类的标题是
I was browsing through the documentation of JDK 7 when I noticed an annotation called @Target
in package java.lang.annotation
. The header of that class is
@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE)
public @interface Target
现在,@Target
用作对自身的注释.这怎么可能?@Target
甚至在声明之前就在标头中使用.我用我写的注释尝试了这个,它也有效.谁能解释一下这里发生了什么?
Now, @Target
is used as an annotation to itself. How is this possible? @Target
is used in the header even before it is declared. I tried this with annotations I had written, and it worked as well. Can anyone explain what's happening here?
推荐答案
JLS 在 第 9.6 节注释类型:
如果一个注解a
(§9.7)一个注解类型声明对应一个注解类型T
和 T
有一个(元)注解 m
对应于java.lang.annotation.Target
,那么 m
必须有一个元素其值为 java.lang.annotation.ElementType.ANNOTATION_TYPE
,或值为 java.lang.annotation.ElementType.TYPE
的元素,或发生编译时错误.
If an annotation
a
(§9.7) on an annotation type declaration corresponds to an annotation typeT
, andT
has a (meta-)annotationm
that corresponds tojava.lang.annotation.Target
, thenm
must have either an element whose value isjava.lang.annotation.ElementType.ANNOTATION_TYPE
, or an element whose value isjava.lang.annotation.ElementType.TYPE
, or a compile-time error occurs.
第 9.6 或 9.7 节的任何其他部分都没有说明使用对被声明的注释的引用来注释注释声明是非法的.
No other part of section 9.6 or 9.7 says anything about it being illegal for an annotation declaration to be annotated with a reference to the annotation being declared.
这篇关于注释怎么可能是对自身的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!