@Documented @Retention(value=RUNTIME) @Target(value={CONSTRUCTOR,METHOD}) public @interface SafeVarargs
除了使用限制的@Target
元注释强加的,编译器都需要实现这个注释类型的附加使用限制;它是一个编译时错误,如果一个方法或构造函数的声明是一个@SafeVarargs
注释注释,要么:
static
也final
。当该注释类型被应用到一个方法或构造函数声明中时,鼓励编译器发出警告:
Object
,和String
。(unchecked警告该注释类型的抑制已经不发生一个泛型具体化元素类型。)走样导致运行时@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.