Section 9.6.4.1 of the Java Language Specification version 19列出了Java语言中的所有声明上下文.其中一个声明上下文是"Type parameter declarations of generic classes, interfaces, methods, and constructors".
javadocs for @Target
in Java 19 say如果我有一个批注类没有用@Target
批注,我的批注可能使用"as a modifier for any declaration"(句号).
在Java语言规范版本13中,但在版本14和更高版本中为not,文本改为:"If an annotation of type java.lang.annotation.Target
is not present on the declaration of an annotation type T
, then T
is applicable in all declaration contexts except type parameter declarations, and in no type contexts".
Java 13(但不是14及更高版本)中@Target
的javadoc读作(类似地):"If an annotation of type java.lang.annotation.Target
is not present on the declaration of an annotation type T
, then T
is applicable in all declaration contexts except type parameter declarations, and in no type contexts".
Java 17中@Target
的javadoc最终更改(迟来的?)以匹配规范(在14中更改),读作(就像今天在Java 19中所做的那样):"If an [sic] @Target
meta-annotation is not present on an annotation interface T
, then an annotation of type T
may be written as a modifier for any declaration".
因此,考虑到Java 19中的这一点:
@Retention(RetentionPolicy.RUNTIME)
public @interface A {}
public class B<@A C> {}
…我认为这是合法的:
-
C
是类型参数声明,因此 -
@A
,比@Target
少,应该能够适用于它,并且 - 这不是(看起来)在Java Language Specification version 19 section 4.11中列出的类型上下文.
但javac
说@A
等于not applicable in this type context
.
这是个窃听器吗?或者,我是否误读了Java语言规范,版本19?