Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

How do you prevent unchecked cast warning when reading from ObjectInputStream?

Thread view: 
Knute Johnson - 15 Apr 2007 03:20 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?

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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.