Skip to main content

Properties Cast to Map? ok in 5.0, compile error in 6.0Beta

2 replies [Last post]
Joined: 2004-11-15

Java 5.0 compiler allowed:
(Map )(System.getProperties());

Java 6 Compiler reports:
found : java.util.Properties
required: java.util.Map
retVal = (Map )(System.getProperties());

While this is probably what "should" be reported, based on the Javadoc showing the superclass, since it is a properties file, why not subclass as

This impacts a Very large codebase.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-06-19

If Properties did extend Hashtable, then old code that extends Properties may not compile. Also client code can legitimately store non-Strings in Properties objects. The real mistake was making Properties extend Hashtable.

The JDK 1.5 javac has a bug (or two) handling classes that specify specific generic parameters of their superclass. Code that exploits that bug should be fixed (or you could hack it with an intermediate cast to Map).

Joined: 2004-09-02

Does the code compile without a single unchecked warning on Tiger? If so that would be a bug in Java SE 5.

IMO, the statement should read:
[code]Map retVal = (Map) System.getProperties();[/code]