aspose file tools*
The moose likes Java in General and the fly likes Type safety: Unchecked cast from Object to TreeMap<String,String> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Type safety: Unchecked cast from Object to TreeMap<String,String>" Watch "Type safety: Unchecked cast from Object to TreeMap<String,String>" New topic
Author

Type safety: Unchecked cast from Object to TreeMap<String,String>

Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47


Above is a simple piece of code. If you put this in Eclipse (or other IDEs), a warning will be shown: Type safety: Unchecked cast from Object to TreeMap<String,String>.
I can add @SuppressWarnings("unchecked") to remove this warning, which I do not want to do.
1. Is there any other ways (industry standard or best practice) to get rid of this warning?
2. Why I get this warning? What is the protential risk in this snippet?
Thank you very much!


Enjoy life!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to JavaRanch

change returnTreeMap return type to TreeMap<String,String> from Object .

<edit>I do suggest you to use Interface to Type assignment instead of class itself</edit>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Ziyang Zhang wrote:1. Is there any other ways (industry standard or best practice) to get rid of this warning?

No. Well, except by returning Map<String,String>, SortedMap<String,String>, NavigableMap<String,String> or TreeMap<String,String>. In other words, TreeMap itself or one of the interfaces it implements, including the generic types.

2. Why I get this warning? What is the protential risk in this snippet?

Because of type erasure, the JVM cannot check if the object really is a TreeMap<String,String>. It can check if it's a TreeMap, but not the <String,String> part. That means that this cast may succeed, and you'll get casting problems elsewhere, and it will be very difficult to find out what went wrong -- since the cause can be located several classes and minutes away.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 47
Rob Prime wrote:
Ziyang Zhang wrote:1. Is there any other ways (industry standard or best practice) to get rid of this warning?

No. Well, except by returning Map<String,String>, SortedMap<String,String>, NavigableMap<String,String> or TreeMap<String,String>. In other words, TreeMap itself or one of the interfaces it implements, including the generic types.

2. Why I get this warning? What is the protential risk in this snippet?

Because of type erasure, the JVM cannot check if the object really is a TreeMap<String,String>. It can check if it's a TreeMap, but not the <String,String> part. That means that this cast may succeed, and you'll get casting problems elsewhere, and it will be very difficult to find out what went wrong -- since the cause can be located several classes and minutes away.


Got it! Thank you for your great answer!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Type safety: Unchecked cast from Object to TreeMap<String,String>