Java 1.7 varargs 函数报告为未经检查的警告 [英] Java 1.7 varargs function reported as unchecked warning

查看:25
本文介绍了Java 1.7 varargs 函数报告为未经检查的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用了一些可变参数函数,当我们迁移到 Java 1.7 时,我们收到了一个奇怪的未经检查的警告.

We use some varargs functions and as we move to java 1.7 we are getting a strange unchecked warning.

ICache 接口中的函数添加

Function add in interface ICache

public interface ICache<O> {
    void add(Object source, O... objects);
}

在一个界面中报告错误.

in an interface reports the error.

ICache.java:18: warning: [unchecked] Possible heap pollution from parameterized vararg type O
    void add(Object source, O... objects);
  where O is a type-variable:
    O extends Object declared in interface ICache
1 warning

O 扩展 Object,作为它的通用缓存类.

O extends Object, as its generic cache class.

我阅读了 xlint 警告,我们确实在未选中的情况下进行编译,但是 http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#xlintwarnings 似乎暗示此错误应该是 [varargs] 类型而不是未经检查的类型.

I read the xlint warnings and we do compile with unchecked on, but http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#xlintwarnings seems to imply that this error should be a [varargs] type not an unchecked type.

我错过了什么吗?

推荐答案

堆污染是一个术语,它指的是在使用带有泛型类型的可变参数时,指向的对象不是其超类型的类型.当参数化类型的变量引用不属于该参数化类型的对象时,就会发生这种情况.这篇关于堆栈溢出的帖子向您确切解释了这意味着什么以及您应该做什么做一下,并提供有关 @SafeVarargs 注释的详细信息.因此,在接口 ICache 中,可变参数类型 O 指向接口中的 Object,但 O 不是Object 的超类型,这会生成堆污染警告.请注意它是如何说可能堆污染的.如果你的代码没有引起任何问题,例如导致ClassCastException,它可能是安全的,不会污染堆,但编译器无法证明这一点,也无法验证操作的正确性,所以它仍然会产生警告.这实际上是未检查警告的定义:当无法验证涉及参数化类型的操作的正确性时.有关详细信息,请参阅 this Oracle 页面.如果你不想收到这个警告,你可以用 SafeVarargs 阻止它,或者简单地通过添加 @SuppressWarnings ({"unchecked", "varargs"}) 到方法声明,但如果方法确实不安全,您将不会收到警告.

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.

这篇关于Java 1.7 varargs 函数报告为未经检查的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆