How do I address unchecked cast warnings?

后端 未结 23 1185
醉梦人生
醉梦人生 2020-11-22 03:06

Eclipse is giving me a warning of the following form:

Type safety: Unchecked cast from Object to HashMap

This is from a call to

23条回答
  •  别跟我提以往
    2020-11-22 03:56

    If you are sure that the type returned by session.getAttribute() is HashMap then you can not typecast to that exact type, but rely on only checking the generic HashMap

    HashMap getItems(javax.servlet.http.HttpSession session) {  
        HashMap theHash = (HashMap)session.getAttribute("attributeKey");
        return theHash;
    } 
    

    Eclipse will then surprise warnings, but of course this can lead to runtime errors that can be hard to debug. I use this approach in not operation-critical contexts only.

提交回复
热议问题