这怎么可能,一个注释可以是一个注释本身? [英] How is it possible that an annotation can be an annotation to itself?

查看:207
本文介绍了这怎么可能,一个注释可以是一个注释本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览通过JDK 7的文档时,我注意到 @Target 在呼包注释 java.lang.annotation中的。这个类的标题是

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注释类型的:

The JLS specifically anticipates this, in section 9.6 Annotation Types:

如果一个注解 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 type T, and T has a (meta-)annotation m that corresponds to java.lang.annotation.Target, then m must have either an element whose value is java.lang.annotation.ElementType.ANNOTATION_TYPE, or an element whose value is java.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆