public static <T> void foo(List<T>... bar) 编译器将其转换为
public static <T> void foo(List<T>[] bar) 然后到
public static <T> void foo(List[] bar)
然后出现危险,你将错误地将错误的值分配给列表,编译器不会触发任何错误 . 例如,如果 T 是 String ,则以下代码将编译而不会出现错误但在运行时将失败:
// First, strip away the array type (arrays allow this kind of upcasting)
Object[] objectArray = bar;
// Next, insert an element with an incorrect type into the array
objectArray[0] = Arrays.asList(new Integer(42));
// Finally, try accessing the original array. A runtime error will occur
// (ClassCastException due to a casting from Integer to String)
T firstElement = bar[0].get(0);
5 回答
当您使用varargs时,它可以导致创建
Object[]
来保存参数 .由于逃逸分析,JIT可以优化此阵列创建 . (我发现它的少数几次之一)它不能保证被优化掉,但除非你在内存分析器中看到它的问题,否则我不会担心它 .
AFAIK
@SafeVarargs
禁止编译器发出警告,并且不会更改JIT的行为方式 .原因是因为varargs提供了使用非参数化对象数组调用的选项 . 因此,如果您的类型是List <A> ...,也可以使用List []非varargs类型调用它 .
这是一个例子:
如您所见,List [] b可以包含任何类型的使用者,但此代码可编译 . 如果您使用varargs,那么您没问题,但如果您在type-erasure - void test(List [])之后使用方法定义 - 那么编译器将不会检查模板参数类型 . @SafeVarargs将禁止此警告 .
堆污染是一个技术术语 . 它引用的引用的类型不是它们指向的对象的超类型 .
这可能导致"unexplainable"
ClassCastException
s .@SafeVarargs
根本不会阻止这种情况 . 但是,有些方法可以证明不会污染堆,编译器就是无法证明它 . 以前这种API的调用者会得到令人讨厌的警告,这些警告完全没有意义,但必须在每个调用站点被抑制 . 现在API作者可以在声明站点禁止它一次 .但是,如果该方法实际上不安全,将不再警告用户 .
@SafeVarargs
并不阻止它发生,但它要求编译器在编译使用它的代码时更严格 .http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html更详细地解释了这一点 .
堆污染是指在通用接口上执行操作时得到
ClassCastException
,并且它包含的声明不是另一种类型 .当你申报时
public static <T> void foo(List<T>... bar)
编译器将其转换为public static <T> void foo(List<T>[] bar)
然后到public static <T> void foo(List[] bar)
然后出现危险,你将错误地将错误的值分配给列表,编译器不会触发任何错误 . 例如,如果
T
是String
,则以下代码将编译而不会出现错误但在运行时将失败:如果您查看了该方法以确保它不包含此类漏洞,则可以使用
@SafeVarargs
对其进行注释以禁止显示警告 . 对于接口,请使用@SuppressWarnings("unchecked")
.如果您收到此错误消息:
并且您确定您的使用是安全的,那么您应该使用
@SuppressWarnings("varargs")
. 有关第二种错误的详细解释,请参阅Is @SafeVarargs an appropriate annotation for this method?和https://stackoverflow.com/a/14252221/14731 .参考文献:
http://docs.oracle.com/javase/7/docs/technotes/guides/language/non-reifiable-varargs.html
http://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html#heap_pollution