我无法通过未经检查的强制转换来删除警告。我认为这很奇怪,因为我在方法上添加了 @SuppressWarnings("unchecked")
注释,但 Javac 仍然显示警告。
[unchecked] unchecked cast (List<Integer>) getObject(LIST);
return (List<Integer>) getObject(LIST);
required: List<Integer>
found: Object
我创建了一个示例,它抛出了 2 个警告。我正在使用 Oracle JDK 1.7 更新 79。
package strange;
import java.util.List;
public class Strange implements Constants {
//Throw Warning
@SuppressWarnings("unchecked")
protected List<Integer> getList() {
return (List<Integer>) getObject(LIST);
}
@SuppressWarnings("unchecked")
protected List<Integer> getList2() {
return (List<Integer>) getObject(LIST);
}
//Throw Warning
@SuppressWarnings("unchecked")
protected List<Integer> getOtherList() {
return (List<Integer>) getObject(OTHER);
}
protected Object getObject(String key) {
return new Object();
}
}
interface Constants {
public static final String LIST = "list";
public static final String OTHER = "other";
}
谢谢。
最佳答案
这看起来像是一个 javac
错误。但它不会在 1.8.0u45 中重现。
快速搜索了一下,它可能与例如 JDK-8016099 相关。或JDK-8022144 。人们可以进行 hg bisect 来找到解决问题的确切变更集。
可能相关:
关于java - Javac @SuppressWarnings 注释不会删除未检查的警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30717476/