通过可变参数可能造成的堆污染
我知道这与Java 7使用可变参数与genericstypes时发生;
但是我的问题是..
当Eclipse说“它的使用可能会污染堆”究竟是什么意思呢?
和
新的@SafeVarargs注释如何防止这种情况发生? 
堆污染是一个技术术语。 它指的是引用的types不是它们指向的对象的超types。
 List<A> listOfAs = new ArrayList<>(); List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As 
 这可能导致“无法解释的” ClassCastException 。 
 // if the heap never gets polluted, this should never throw a CCE B b = listOfBs.get(0); 
  @SafeVarargs并没有阻止这一点。 但是,有些方法可以不污染堆,编译器不能certificate它。 以前这些API的调用者会得到一些毫无意义的恼人的警告,但是在每个调用站点都必须被压制。 现在,API作者可以在声明站点将其压制一次。 
但是,如果方法实际上不安全,用户将不再受到警告。
当你声明
  public static <T> void foo(List<T>... bar)编译器将其转换为 
  public static <T> void foo(List<T>[] bar) then 
 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); 
 如果您查看了方法以确保它不包含此类漏洞,则可以使用@SafeVarargs进行注释以取消警告。 对于接口,使用@SuppressWarnings("unchecked") 。 
如果您收到此错误消息:
可变参数法可能会导致不可保证的可变参数的堆污染
 你确定你的使用是安全的,那么你应该使用@SuppressWarnings("varargs")来代替。 请参阅@SafeVarargs此方法的适当注释吗? 和https://stackoverflow.com/a/14252221/14731第二类错误的一个很好的解释。; 
参考文献:
  @SafeVarargs不会阻止它的发生,但它要求在编译使用它的代码时编译器更严格。 
http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html对此进行了更详细的解释。;
 堆污染是当您在通用接口上执行操作时遇到ClassCastException ,并且它包含的另一个types不是声明的。 
 当您使用可变参数时,可能会导致创build一个Object[]来保存参数。 
由于逃逸分析,JIT可以优化这个数组的创build。 (我发现它的几次之一)它不保证被优化,但我不会担心它,除非你在内存分析器中看到它的问题。
  AFAIK @SafeVarargs禁止编译器发出警告,并不会改变JIT的行为。