How do you prevent unchecked cast warning when you read a parameterized
object from an ObjectInputStream? If TreeMap isn't parameterized then
this doesn't happen but that isn't really a good solution. Or should I
just ignore this?
The TreeMap in this case is TreeMap<String,Boolean>.
C:\>javac -Xlint:unchecked
com/knutejohnson/smallbook/sportsnetwork/TickerClient.java
com\knutejohnson\smallbook\sportsnetwork\TickerClient.java:390: warning:
[unchecked] unchecked cast found : java.lang.Object
required: java.util.TreeMap<java.lang.String,java.lang.Boolean>
map = (TreeMap<String,Boolean>)ois.readObject();
^
1 warning
Thanks,

Signature
Knute Johnson
email s/nospam/knute/
Tom Hawtin - 15 Apr 2007 03:51 GMT
> How do you prevent unchecked cast warning when you read a parameterized
> object from an ObjectInputStream? If TreeMap isn't parameterized then
> this doesn't happen but that isn't really a good solution. Or should I
> just ignore this?
A common problem.
Write yourself a little utility method to read an object and cast. That
method can suppress the warnings "safely".
@SuppressWarnings("unchecked")
static <T> T readObject(
java.io.ObjectInputStream in
) throws java.io.IOException, java.lang.ClassNotFoundException {
return (T)in.readObject();
}
http://groups.google.com/group/comp.lang.java.programmer/tree/browse_frm/thread/
3c05ddbbe4eaaba4#doc_585daaa682e4e563
Tom Hawtin
Knute Johnson - 15 Apr 2007 20:17 GMT
>> How do you prevent unchecked cast warning when you read a
>> parameterized object from an ObjectInputStream? If TreeMap isn't
[quoted text clipped - 16 lines]
>
> Tom Hawtin
Thanks Tom, that worked perfectly!

Signature
Knute Johnson
email s/nospam/knute/