Java 注释@Target(ElementType.TYPE_USE) - 还包括声明上下文?
Java Annotation @Target(ElementType.TYPE_USE) - Also covers declaration contexts?
即使以下注释不应该用作声明注释,也可以完美编译。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// Foo annotation
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@interface Foo {}
@Foo
class Person {}
@Foo
interface Emittable() {}
在上面的示例中,Foo
注释类型用于 Person
和 Emittable
类型的声明上下文,即使它仅针对类型上下文,并且有趣的是代码编译得很好并且运行完美 这是否意味着 ElementType.TYPE_USE
也涵盖了声明上下文?根据 Java 14 语言规范,ElementType.TYPE_USE
不涵盖任何声明上下文。我错过了什么?
当前接受的答案不正确。
相关文档位于 https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/ElementType.html 并说
The constant TYPE_USE
corresponds to the 15 type contexts in JLS 4.11, as well as to two declaration contexts: type declarations (including annotation type declarations) and type parameter declarations.
换句话说,TYPE_USE
是一个特例。的理由是,在定义类型限定符时,程序员可能经常希望将其写在声明和使用上。
这是在 https://docs.oracle.com/javase/specs/jls/se14/html/jls-9.html#jls-9.7.4 中列出的,它说:
two of the nine clauses - for class, interface, enum, and annotation type declarations, and for type parameter declarations - mention "... or type contexts" because it may be convenient to apply an annotation whose type is meta-annotated with @Target(ElementType.TYPE_USE)
(thus, applicable in type contexts) to a type declaration.
当前接受的答案引用了关于另一件事的文本:如何解释
中的注释
class C {
@MyDeclAnno @MyTypeAnno Object myField;
}
其中 MyDeclAnno
是声明注释,MyTypeAnno
是类型注释。
JLS 语法 解析声明中的两个注释。但是,当前接受的答案中引用的文本指出 @MyTypeAnno
实际上应用于类型 Object
而 @MyDeclAnno
实际上应用于字段的声明 myField
.
即使以下注释不应该用作声明注释,也可以完美编译。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// Foo annotation
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@interface Foo {}
@Foo
class Person {}
@Foo
interface Emittable() {}
在上面的示例中,Foo
注释类型用于 Person
和 Emittable
类型的声明上下文,即使它仅针对类型上下文,并且有趣的是代码编译得很好并且运行完美 这是否意味着 ElementType.TYPE_USE
也涵盖了声明上下文?根据 Java 14 语言规范,ElementType.TYPE_USE
不涵盖任何声明上下文。我错过了什么?
当前接受的答案不正确。
相关文档位于 https://docs.oracle.com/javase/8/docs/api/java/lang/annotation/ElementType.html 并说
The constant
TYPE_USE
corresponds to the 15 type contexts in JLS 4.11, as well as to two declaration contexts: type declarations (including annotation type declarations) and type parameter declarations.
换句话说,TYPE_USE
是一个特例。的理由是,在定义类型限定符时,程序员可能经常希望将其写在声明和使用上。
这是在 https://docs.oracle.com/javase/specs/jls/se14/html/jls-9.html#jls-9.7.4 中列出的,它说:
two of the nine clauses - for class, interface, enum, and annotation type declarations, and for type parameter declarations - mention "... or type contexts" because it may be convenient to apply an annotation whose type is meta-annotated with
@Target(ElementType.TYPE_USE)
(thus, applicable in type contexts) to a type declaration.
当前接受的答案引用了关于另一件事的文本:如何解释
中的注释class C {
@MyDeclAnno @MyTypeAnno Object myField;
}
其中 MyDeclAnno
是声明注释,MyTypeAnno
是类型注释。
JLS 语法 解析声明中的两个注释。但是,当前接受的答案中引用的文本指出 @MyTypeAnno
实际上应用于类型 Object
而 @MyDeclAnno
实际上应用于字段的声明 myField
.