通过变量参数对潜在的堆污染 [英] Potential heap pollution via varargs parameter
问题描述
但是我的问题是..
当Eclipse表示它的使用可能会污染堆时,Eclipse是什么意思?
和
新的 @SafeVarargs
注释如何防止这种情况?
列表< A> listOfAs = new ArrayList<>();
列表< B> listOfBs =(List< B>)(Object)listOfAs; //指向As
的列表这可能导致无法解释 ClassCastException
s。
//如果堆没有被弄脏,这个不应该抛出一个CCE
B b = listOfBs.get(0);
@SafeVarargs
。然而,有些方法可能不会污染堆,编译器无法证明它。以前,这些API的呼叫者将会产生令人讨厌的警告,这些警告是完全无意义的,但必须在每个呼叫站点被抑制。现在,API作者可以在声明网站上抑制一次。
但是,如果该方法实际上不是安全,则用户将不再警告。
I understand this occurs with Java 7 when using varargs with a generic type;
But my question is..
What exactly does Eclipse mean when it says "its use could potentially pollute the heap?"
And
How does the new @SafeVarargs
annotation prevent this?
Heap pollution is a technical term. It refers to references which have a type that is not a supertype of the object they point to.
List<A> listOfAs = new ArrayList<>();
List<B> listOfBs = (List<B>)(Object)listOfAs; // points to a list of As
This can lead to "unexplainable" ClassCastException
s.
// if the heap never gets polluted, this should never throw a CCE
B b = listOfBs.get(0);
@SafeVarargs
does not prevent this at all. However, there are methods which provably will not pollute the heap, the compiler just can't prove it. Previously callers of such APIs would get annoying warnings that were completely pointless, but had to be suppressed at every call site. Now the API author can suppress it once at the declaration site.
However, if the method actually is not safe, users will no longer be warned.
这篇关于通过变量参数对潜在的堆污染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!