我在 JLS section 8.4 中看到过省略号前可以有注释:
class X {
void method(String @Annotation ... x) {}
}
我的问题很简单:这是什么意思?
特别是,有什么区别:
class X {
void method(@Annotation String ... x) {}
}
最佳答案
From the JLS on Where Annotations May Appear
It is possible for an annotation to appear at a syntactic location in a program where it could plausibly apply to a declaration, or a type, or both.
和
Whether an annotation applies to a declaration or to the type of the declared entity - and thus, whether the annotation is a declaration annotation or a type annotation - depends on the applicability of the annotation's type: [...]
因此,
中的注解void method(String @Annotation ... x) {}
是一个TYPE_USE
注解。
和注解在
void method(@Annotation String ... x) {}
既是TYPE_USE
又是PARAMETER
注释。
你可以验证这一点。
@Target(value = ElementType.TYPE_USE)
@interface Annot {}
public static void method(String @Annot... arg) {}
public static void method2(@Annot String... arg) {}
和
@Target(value = ElementType.PARAMETER)
@interface Annot {}
public static void method(String @Annot... arg) {} // DOES NOT COMPILE
public static void method2(@Annot String... arg) {}
关于java - 在省略号前输入注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944980/