Java 1.7 varargs function reported as unchecked warning

喜夏-厌秋 提交于 2019-12-01 15:03:33
Santiago Benoit

Heap pollution is a term that refers to a type that is pointing to an object that it is not the supertype of when using varargs with a generic type. It occurs when a variable of a parameterized type refers to an object that is not of that parameterized type. This post on stack overflow explains to you exactly what this means and what you should do about it, and gives details on the @SafeVarargs annotation. So, in interface ICache, vararg type O is pointing to Object in your interface, but O is not the supertype of Object, and this generates a heap pollution warning. Notice how it says possible heap pollution. If your code is not causing any problems such as leading to a ClassCastException, it will probably be safe and not pollute the heap, but the compiler has no way of proving this and cannot verify the correctness of the operation, so it will still generate the warning. That is actually the definition of an unchecked warning: when the correctness of an operation involving a parameterized type cannot be verified. See this Oracle page on non-reifiable types for more information. If you don't want to get this warning, you can prevent it with SafeVarargs, or simply suppress it by adding @SuppressWarnings ({"unchecked", "varargs"}) to the method declaration, but you will not get the warning in the event that the method is indeed unsafe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!