Bug 150362 - Map<String, ?>) properties result in "Cannot cast" error. Javac does not complain.
Summary: Map<String, ?>) properties result in "Cannot cast" error. Javac does not comp...
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-07-12 06:52 EDT by Max Rydahl Andersen CLA
Modified: 2006-07-12 13:03 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Max Rydahl Andersen CLA 2006-07-12 06:52:11 EDT
The following code compiles with javac, but not with eclipse:

Properties properties = new Properties();
for ( Map.Entry<String, ?> entry : ( (Map<String, ?>) props ).entrySet() ) {
  System.out.println(entry);
}

A Properties implements Map<K,V> so the above should work IMO.
Comment 1 Olivier Thomann CLA 2006-07-12 07:00:34 EDT
It compiles with javac 1.5.0_07, but not with javac 1.5.0_08.
X.java:8: inconvertible types
found   : java.util.Properties
required: java.util.Map<java.lang.String,?>
                for (Map.Entry<String, ?> entry : ((Map<String, ?>) props).entrySet()) {
                                                                    ^
1 error

import java.util.Map;
import java.util.Properties;

public class X {

	public static void main(String[] args) {
		Properties props = new Properties();
		for (Map.Entry<String, ?> entry : ((Map<String, ?>) props).entrySet()) {
			System.out.println(entry);
		}
	}
}
Also fail with javac 6.0b88. This looks like a fixed bug.
Comment 2 Max Rydahl Andersen CLA 2006-07-12 07:05:41 EDT
ah, those pesky nano updates. sorry for bothering you ;)
Comment 3 Max Rydahl Andersen CLA 2006-07-12 07:07:22 EDT
...just as sidenote: What rule is broken here ? (if you know)
Comment 4 Philipe Mulet CLA 2006-07-12 10:58:25 EDT
Properties is not a subtype of Map<String,?>.
It inherits from Map<Object,Object>, which is provably distinct from Map<String,?>.

The following would work:
import java.util.Map;
import java.util.Properties;

public class X {

	public static void main(String[] args) {
		Properties props = new Properties();
		for (Map.Entry<Object, ?> entry : ((Map<Object, ?>) props).entrySet()) {
			System.out.println(entry);
		}
	}
}

or you could even use: Map.Entry<?, ?> entry : ((Map<?, ?>).
BTW, I don't get what the cast buys you here.
Comment 5 Philipe Mulet CLA 2006-07-12 11:01:20 EDT
It works as per design.
Added GenericTypeTest#test1023
Comment 6 Max Rydahl Andersen CLA 2006-07-12 13:03:24 EDT
i understand. the only thing it bought were no need to cast the key to a string.