File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Want to avoid warning when I Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Want to avoid warning when I "cast" Properties to Map" Watch "Want to avoid warning when I "cast" Properties to Map" New topic
Author

Want to avoid warning when I "cast" Properties to Map

Eric Lemaitre
Ranch Hand

Joined: Jul 03, 2004
Posts: 538

Hi all,

I would like to avoid a warning when I "cast" Properties to Map, but I couldn't achieve it so far.

Example:
Properties props = new Properties();
Map<String, String> map = new HashMap<String, String>((Map) props); << Here is the warning, on ((Map) props)

It works in practice, I just would like to get rid of the warning if it is possible.

Best regards.


Eric LEMAITRE
CNAM IT Engineer, MS/CS (RHCE, RHCX, SCJA, SCJP, SCJD, SCWCD, SCBCD, SCEA, Net+)
Free Online Tutorials: http://www.free-tutorials-online.net/
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

In theory, you could add an annotation to suppress the warning:



I think I'd be more comfortable writing my own loop to retrieve the properties and add them to the HashMap.
Eric Lemaitre
Ranch Hand

Joined: Jul 03, 2004
Posts: 538

Greg Charles wrote:In theory, you could add an annotation to suppress the warning: @SuppressWarnings({ "rawtypes", "unchecked" })


Hi Greg,

Yes, I know the workaround and it works, but I really would like to know whether there is something which would allow to eliminate warning from this "cast" without having to do the whole loop to populate the Map.

Not sure at all it is possible, apart the @SuppressWarning annotation trick to hide it under the rug...

Best regards.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Eric Lemaitre wrote:like to know whether there is something which would allow to eliminate warning from this "cast" without having to do the whole loop to populate the Map.


Its actually bad form to do what you are trying to do. Its an accident that a Properties is a Hashtable, which happens to be implemented as a HashMap. The contract for Properties is that its a Hashtable, and the Hashtable is in turn a Map, but there is no contract that its a HashMap. This is a side effect and should not be relied upon as it might change in a future Java.

In practice, I think it will never change, but if you read the Javadocs from the JDK, there is no mention that a Properties is really a HashMap. And the Hashtable is synchronized, so unless you want that, you will have to move the K,V pairs to the proper Map.

I'm pretty sure that if you use Google's wonderful open source Guava library, then you can do something like:




If the props is empty like your trivial example, of course, then you don't need to do anything other than use the proper constructor.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Pat Farrell wrote:The contract for Properties is that its a Hashtable, and the Hashtable is in turn a Map, but there is no contract that its a HashMap.

True - but nothing Eric is doing here requires that it be a HashMap. Or a Hashtable, for that matter. He requires it to be a Map, which is fine. The real problem comes when he tries to cast it as a Map<String, String>. And it simply isn't one. Even though it should have been one. Sun made it a Map<Object, Object> instead, to avoid backwards compatibility issues.

Eric: no, there's really no workaround. You're dealing with a design defect in Properties, and there's no shortcut. Sorry.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Mike Simmons wrote: Sun made it a Map<Object, Object> instead, to avoid backwards compatibility issues.


IMHO, Sun did way too many of these "make it a kludge rather than breaking backwards compatibility" decisions. A classic example is the mess that they made of generics in Java 5.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
And this is a particularly stupid example, because they're only helping people who decided to put something other than a String into their Properties object. Such people should be punished, not catered to. But that's the Java we live with.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
And why do you want to cast it anyway? That is an upcast, which is usually unnecessary.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Ummm... to fix Sun's mistake? It should be a Map<String,String>, and using methods like entrySet() and keySet() requires extra casts because Sun made it a Map<Object,Object>. We should abandon Properties entirely, except its ability to read a standard prop file is something actually useful. So instead, we need a quick way to read a prop file and translate it to a Map<String,String>.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Want to avoid warning when I "cast" Properties to Map