public abstract @interface SafeVarargs
implements Annotation
| java.lang.SafeVarargs |
程序员断言注解方法或构造函数的主体不对其varargs参数执行潜在的不安全操作。 将此注释应用于方法或构造函数会取消未经检查的有关不可变可变参数(vararg)类型的警告,并禁止在呼叫站点创建有关参数化数组创建的未经检查的警告。
除了由其@Target元注释施加的使用限制之外,编译器还需要对此注释类型实施附加使用限制; 如果方法或构造函数声明使用@SafeVarargs注释进行注释,则会出现编译时错误:
static nor final. 鼓励编译器在将此注释类型应用于方法或构造函数声明时发出警告,其中:
Object, and String. (The unchecked warnings this annotation type suppresses already do not occur for a reifiable element type.)
@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!
}
leads to a ClassCastException at runtime. 未来版本的平台可能会要求编译器出现这种不安全操作的错误。
继承方法(Inherited methods) |
|
|---|---|
java.lang.annotation.Annotation
|
|