• Post Reply Bookmark Topic Watch Topic
  • New Topic

how do I get rid of the type safety warning?  RSS feed

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read in a Hashtable from a file, but when I try to cast the object I get the warning "Type safety: The cast from Object to Hashtable<String,Account> is actually checking against the erased type Hashtable"



Is there a way to deserialize a generic'd object in a "typesafe" way?

Thanks,
Yuriy
 
Nicole Lacoste
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Your title "how do I get rid of the type safety warning?" I can answer
@SuppressWarnings({"unchecked"})

The rest is a little trickier! I don't know much about ObjectInputStreams but I have been having lots of similar problems with generics (old C++ habits). There are moments in Java you just can't do stuff without generatng the warning. Even in the collection classes there are these warnings. Look at the code for ListArray ctor
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = (E[])new Object[initialCapacity];
}
They do an unsafe cast. If you google you can find lots of discussion about generics and this problem.

Niki
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put this at the top of the method header when there seems to be no other way to solve it: @SuppressWarnings("unchecked")
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions! Too bad Sun didn't feel necessary to make this easier..... Suppress Warnings seems like a hack that shouldn't be needed.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!