@Documented @Retention(value=RUNTIME) @Target(value={CONSTRUCTOR,METHOD}) public @interface SafeVarargs
除了@Target元注释的使用限制外,还需要编译器对此注释类型实施其他使用限制; 如果方法或构造函数声明使用@SafeVarargs注释注释,则是编译时错误,并且:
static也不是final的可变方法。 当这种注释类型应用于方法或构造函数声明时,鼓励编译器发出警告:
Object和String 。 (可注释元素类型不会发生此注释类型禁止的未选中的警告。)
@SafeVarargs // Not actually safe!
static void m(List<String>... stringLists) {
Object[] array = stringLists;
List<Integer> tmpList = Arrays.asList(42);
array[0] = tmpList; // Semantically invalid, but compiles without warnings
String s = stringLists[0].get(0); // Oh no, ClassCastException at runtime!
}
在运行时导致ClassCastException 。 该平台的未来版本可能要求对这种不安全操作的编译器错误。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.