Eclipse正在向我发出以下形式的警告:
类型安全:从Object到HashMap的未选中转换
这是来自对我无法控制的API的调用返回Object:
HashMap<String, String> getItems(javax.servlet.http.HttpSession session) {
  HashMap<String, String> theHash = (HashMap<String, String>)session.getAttribute("attributeKey");
  return theHash;
}
如果可能的话,我想避免Eclipse警告,因为理论上它们至少表明存在潜在的代码问题.但是,我还没有找到一种消除这种方法的好方法.我可以将涉及的单行提取到方法中并添加@SuppressWarnings("unchecked")到该方法,从而限制了我忽略警告的代码块的影响.有更好的选择吗?我不想在Eclipse中关闭这些警告.
在我来到代码之前,它更简单,但仍然引发了警告:
HashMap getItems(javax.servlet.http.HttpSession session) {
  HashMap theHash = (HashMap)session.getAttribute("attributeKey");
  return theHash;
}
当您尝试使用哈希时,问题出在其他地方,您会收到警告:
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.
我有:
class CustomerActionListener implements ActionListener
{
  @Override
  public void actionPerformed(ActionEvent event)
  {
    JComboBox cb = (JComboBox)event.getSource();
    .. do something
  }
}
这导致jdk7中的以下编译器警告:
JComboBox是原始类型.应参数化对泛型类型JComboBox的引用
我试图将它参数化为:
JComboBox<String> cb = (JComboBox<String>)event.getSource();
但这仍然留下以下编译器警告:
类型安全:未选中从Object转换为JComboBox
因此我不确定如何消除编译器警告......
我想知道为什么以下发出关于不安全/未检查操作的警告:
Map<String, ProxySession> sessionMap = (Map<String, ProxySession>) se.getSession().getServletContext().getAttribute("myattribute");
演员错了吗?我无法理解我在这里缺少的东西.
PS我不想摆脱警告,我想了解不安全的操作.
谢谢!