Java - 如何创建仅适用于类型上下文的注释类型?(PURE 类型注解) [英] Java - How do I create an annotation type that is only applicable in type contexts? (PURE type annotation)

查看:34
本文介绍了Java - 如何创建仅适用于类型上下文的注释类型?(PURE 类型注解)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要创建适用于类型上下文的类型注释,除了使用 @Target(ElementType.TYPE_USE) 对注释类型进行元注释之外别无他法.然而,由于 Java 设计者的错误决定,这个注解也适用于声明上下文.

To create a type annotation that is applicable in type contexts, there is no way other than meta-annotating the annotation type with @Target(ElementType.TYPE_USE). However, this annotation also becomes applicable in declaration contexts due to the bad decision of the Java designers.

根据 Java SE 文档,它是这样写的:

According to the Java SE documentation, it reads:

常量 TYPE_USE 对应于 JLS 4.11 中的类型上下文,以及两个声明上下文:类型声明(包括注解类型声明)和类型参数声明.

The constant TYPE_USE corresponds to the type contexts in JLS 4.11, as well as to two declaration contexts: type declarations (including annotation type declarations) and type parameter declarations.

例如,一个注解,其类型是元注解 @Target(ElementType.TYPE_USE) 可以写在一个字段的类型上(或者在该字段的类型内,如果它是嵌套的、参数化的或数组类型),也可以作为修饰符出现,比如类声明.

For example, an annotation whose type is meta-annotated with @Target(ElementType.TYPE_USE) may be written on the type of a field (or within the type of the field, if it is a nested, parameterized, or array type), and may also appear as a modifier for, say, a class declaration.

TYPE_USE 常量包括类型声明和类型参数声明,以方便类型检查器的设计者为注解类型提供语义.例如,如果注解类型 NonNull 是用 @Target(ElementType.TYPE_USE) 元注解的,那么 @NonNull class C {...} 可以通过类型检查器作为指示C类的所有变量为非空,同时仍然允许其他类的变量为非空或非非空基于是否@NonNull 出现在变量的声明处.

The TYPE_USE constant includes type declarations and type parameter declarations as a convenience for designers of type checkers which give semantics to annotation types. For example, if the annotation type NonNull is meta-annotated with @Target(ElementType.TYPE_USE), then @NonNull class C {...} could be treated by a type checker as indicating that all variables of class C are non-null, while still allowing variables of other classes to be non-null or not non-null based on whether @NonNull appears at the variable's declaration.

尽管如此,我相信 TYPE_USE 最初打算在 JLS 4.11.但是 Java 设计者决定将其用途扩展到声明上下文.

Nonetheless, I believe TYPE_USE was originally intended to be used in 16 type contexts as described in JLS 4.11. But the Java designers made a decision to extend its usage to declaration contexts.

我想:

  • 当您可以通过向 @Target 注释添加更多元素来扩展注释的适用性时,他们到底为什么会做出这个决定.除了方便"之外还有什么具体原因?
  • 如何创建仅适用于类型上下文的注释类型?(又名纯类型注解)
  • WHY on earth did they make this decision when you can expand an annotation's applicability simply by adding more elements to the @Target annotation. Any specific reason other than "convenience"?
  • HOW can I create an annotation type that is ONLY applicable in type contexts? (aka. Pure type annotation)

推荐答案

关于你的第一个问题,@Sweeper 给出了原因.按照惯例,写在类型声明上的类型注释被视为适用于该类型的每次使用.这是一个很常见的习惯用法,要求每个注解设计者都扩展@Target注解会很麻烦.此外,编写 @Target({TYPE_USE, TYPE})误导,因为它声明了一个既是类型注释又是声明注释的注释,这不是设计师的目标,与设计师的目标有着不同的含义.

Regarding your first question, @Sweeper gave the reason. Conventionally a type annotation written on a type declaration is treated as applying to every use of the type. This is such a common idiom that it would be troublesome to require every annotation designer to expand the @Target annotation. Furthermore, writing @Target({TYPE_USE, TYPE}) would be misleading, because it declares an annotation that is both a type annotation and a declaration annotation, which is not the designer's goal and has a different meaning than the designer's goal.

这是 Java 设计的另一个优势.在字段或方法声明中,Java 将声明上的声明注释与字段类型或方法返回类型上的类型注释区分开来.在类声明上编写 TYPE_USE 批注的能力提供了一种类似的方式来区分编写在类声明上的批注的意图和目的.

Here is another advantage to the Java design. In a field or method declaration, Java distinguishes declaration annotations on the declaration from type annotations on the field type or method return type. The ability to write a TYPE_USE annotation on a class declaration gives a similar way to distinguish the intent and purpose of annotations written on class declarations.

关于您的第二个问题,您可以编写一个注释处理器,当在您希望禁止的位置写入注释时,该处理器会发出错误.您已经在使用注解处理器来强制执行类型注解的语义,因此您只需要对其进行调整即可.

Regarding your second question, you can write an annotation processor that issues an error when an annotation in written at a location that you wish to prohibit. You are already using an annotation processor to enforce the semantics of type annotations, so you just need to tweak it.

这篇关于Java - 如何创建仅适用于类型上下文的注释类型?(PURE 类型注解)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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