How is it possible that an annotation can be an annotation to itself?

后端 未结 1 564
梦毁少年i
梦毁少年i 2020-12-11 04:38

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 c

相关标签:
1条回答
  • 2020-12-11 05:27

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

    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.

    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.

    0 讨论(0)
提交回复
热议问题