Section 9.6.4.1 of the Java Language Specification version 19列出了Java语言中的所有声明上下文.其中一个声明上下文是"Type parameter declarations of generic classes, interfaces, methods, and constructors".

Java语言规范版本19说"If an annotation of type java.lang.annotation.Target is not present on the declaration of an annotation interface A, then A is applicable in all declaration contexts and in no type contexts"

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> {}

…我认为这是合法的:

javac@A等于not applicable in this type context.

这是个窃听器吗?或者,我是否误读了Java语言规范,版本19?

推荐答案

更改为使@Target个注释"适用于所有声明上下文"发生在JDK-8261610年.

我同意你对JLS 19 §9.6.4.1的理解,它说‘声明上下文’包括 "泛型类、接口、方法和构造函数的类型参数声明",以及

如果在注释接口A的声明中不存在类型为java.lang.annotation.Target的注释,则A适用于所有声明上下文,而不适用于任何类型上下文.

我认为这是个错误:https://bugs.openjdk.org/browse/JDK-8303784

Java相关问答推荐

那么比较似乎不是词典学的,尽管doctor 这么说

如何为具有多对多关系的实体的给定SQL查询构建JPA规范?

Java Streams在矩阵遍历中的性能影响

Oracle DUAL表上使用DDL时jOOQ问题的解析'

错误:在Liferay7.4中找不到符号导入com.liferay.portal.kernel.uuid.PortalUUID;";

为什么不应用类型推断?

无法初始化JPA实体管理器工厂:无法确定为Java类型<;类>;推荐的JdbcType

放气总是压缩整个街区吗?

使用Jackson库反序列化json

如何在EL处理器中定义带有命名空间的变量?

将stringBuilder + forloop转换为stream + map

如何在字节数组中反转UTF-8编码?

Java HashMap保留所有时间复杂性

如何正确使用java.time类?

URI构造函数错误?

关于正则表达式的一个特定问题,该问题与固定宽度负向后看有关

Jackson YAML:支持锚点扩展/覆盖

如何调查进程列表中不可见的活跃 MySQL 事务?

人们在 IntelliJ 上将-Dhttp.proxyHost=your.proxy.net -Dhttp.proxyPort=8080放在哪里?

Xml Reader 将 BMP 外部的字符解析为代理项对,这会导致无效的 xml