Re: What's wrong with this sentence?



On Wed, 31 Oct 2007 04:17:29 -0400, JTL.zheng <jtl.zheng@xxxxxxxxx> wrote:

Hashtable<String, ItemInfo> userCart = (Hashtable<String, ItemInfo>)
session.getAttribute("userCart");

I am using Eclipse

It get a warning:
Type safety: The cast from Object to Hashtable<String,ItemInfo> is
actually checking against the erased type
Hashtable

Ultimately, generics are erased, and this compiles down to

Hashtable userCart = (Hashtable) session.getAttribute("userCart");

You get the warning, since the runtime will only cast your "session.getAttribute" to Hashtable, and can not verify the contents of the table. Thus, a subsequent call to

ItemInfo info=userCart.getValue("somevalue");

*could* spuriously throw a ClassCastException (since the cast to ItemInfo is automatically generated by the compiler).



Hashtable<String, ItemInfo> userCart = (Hashtable)
session.getAttribute("userCart");

still get a warning too:
Type safety: The expression of type Hashtable needs unchecked
conversion to conform to
Hashtable<String,ItemInfo>

what's wrong with this sentence?
how can I fix it?

Some other posters have commented on using a Map instead of Hashtable. It would be a good idea, when map is initially created, to use Collections.checkedMap - which will ensure that the contents are what you expect.

Map<String, ItemInfo> cart=Collections.checkedMap(
Collections.synchronizedMap(new HashMap()),
String.class,
ItemInfo.class));

Now, you can safely assume that the contents of your map are always String, ItemInfo. The compiler will still generate the same warning, which you can now safely turn off by decorating the calling method with

@SuppressWarnings("unchecked")

Note that this turns off all unchecked cast warnings in that method, so do make sure that you have taken reasonable precautions to externally protect your data before turning off the warning.

HTH,

-Zig
.



Relevant Pages

  • Re: generics and type checking
    ... I'm giving generics a shot, and I'm getting this warning: Type safety: The cast from Object to ArrayListis actually checking against the erased type ... Of course, when I remove all parameters and do it the "old" way, I get the warning "Type safety: The method getbelongs to the raw type ArrayList. ...
    (comp.lang.java.programmer)
  • Annoying 1.5 warnings
    ... Why does the following code cause a warning and how to avoid it w/o ... turning warnings off? ... Type safety: The cast from Object to Vectoris actually checking ...
    (comp.lang.java.programmer)
  • Re: Annoying 1.5 warnings
    ... > and how to avoid it w/o ... The warning is: ... Type safety: The cast from Object to Vectoris actually checking ...
    (comp.lang.java.programmer)
  • Re: [PATCH] [0/9] Use 64bit x86 machine check code for 32bit too
    ... warning: passing argument 2 of ‘strict_strtoull’ makes integer from pointer without a cast ...
    (Linux-Kernel)
  • DBD::ODBC compile problem from v1.00 to 1.09 on RH73
    ... Warning: LD_LIBRARY_PATH doesn't include /usr ... dbdimp.c:162: warning: passing arg 1 of `fprintf' makes pointer from ... integer without a cast ...
    (perl.dbi.users)