Eclipse向我发出了以下形式的警告:
类型安全:未选中从对象到HashMap的强制转换
这是对API的调用,我无法控制该API返回的对象:
HashMap<String, String> getItems(javax.servlet.http.HttpSession session) {
HashMap<String, String> theHash = (HashMap<String, String>)session.getAttribute("attributeKey");
return theHash;
}
如果可能的话,我希望避免出现Eclipse警告,因为理论上它们至少表明了潜在的代码问题.不过,我还没有找到消除这个问题的好方法.我可以单独将涉及的单行提取到一个方法中,并在该方法中添加@SuppressWarnings("unchecked")
,从而限制忽略警告的代码块的影响.还有更好的 Select 吗?我不想在Eclipse中关闭这些警告.
在我开始编写代码之前,它比较简单,但仍然引发了警告:
HashMap getItems(javax.servlet.http.HttpSession session) {
HashMap theHash = (HashMap)session.getAttribute("attributeKey");
return theHash;
}
问题出在其他地方,当您try 使用散列时,会收到警告:
HashMap items = getItems(session);
items.put("this", "that");
Type safety: The method put(Object, Object) belongs to the raw type HashMap. References to generic type HashMap<K,V> should be parameterized.