Deserializing in JDK1.5 without unchecked cast warning

A

Andrew Lee

Hi,

I am using JDK1.5 and am wondering if it is possible to deserialize an
Object and not get an unchecked cast warning from the compiler when I
cast it to another type. In my code I deserialize an Object and cast
it to Hashtable<Integer, String> and I get an unchecked cast warning.
If I cast it to just a Hashtable object I don't get the warning until
I actually try and use it (e.g. with the "put" method).

I understand that I am casting an Object to a type that could cause a
runtime error (hence the warning), but how can I deserialize a
Hashtable directly?

I would like to create warning-free code for JDK1.5 and am hoping this
is not stopping me from doing it.

Thanks in advance.
 
B

bilbo

As far as I know you currently can't. Sun has defined a
@SuppressWarnings annotation that can be used to suppress unchecked
warnings, among others, in a section of code. Unfortunately it's not
actually implemented in the first Java 1.5 release. So you can add the
annotation to your code now and take comfort in the fact that in some
future release of the JDK your code will compile with no warnings.

See
http://www.langer.camelot.de/GenericsFAQ/FAQSections/TechnicalDetails.html#FAQ004
for more information.
 
A

Andrew Lee

Thanks for the link. Yeah, it's unfortunate that @SuppressWarnings
hasn't been implemented but I'll use it for now and hope for the best.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top