• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics and Serialization

 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just performed a testing on serialization. I declared a HashMap with generics. When I serialized this object and received it at the server, I tried doing this:



The code compiles, but generates an unchecked warning. How do I get rid of that warning? The casting is definitely legal, but I don't want any warnings. Thanks for any help.
[ February 12, 2005: Message edited by: Liang Anmian ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. The generic type info (the <Integer, String> part) is not known to the JVM - only the compiler. There's no way the JVM will be able to verify at runtime that the HashMap is really a HashMap<Integer, String>. Using JDK 1.5.0 the best solution would be to remove the unsupported type information from your code. amd use traditional casts:

At some point in the future, you'll be able to make this warning go away using the @SuppressWarnings annotation:

However this annotation is not yet fully supported by JDK 1.5: Bug 4986256.
 
Liang Anmian
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there is nothing I can do in this case? Should I just leave it and document this behaviour?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic