Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 530846

Summary: ClassCastException: java.lang.String cannot be cast to javax.xml.namespace.QName
Product: [Eclipse Project] JDT Reporter: Kirill Golovin <golovin>
Component: CoreAssignee: Stephan Herrmann <stephan.herrmann>
Status: CLOSED NOT_ECLIPSE QA Contact:
Severity: normal    
Priority: P3 CC: stephan.herrmann
Version: 4.7.2   
Target Milestone: 4.8 M6   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Test java sorce none

Description Kirill Golovin CLA 2018-02-07 10:32:09 EST
Created attachment 272571 [details]
Test java sorce

I have problem with debug my application in Eclipse.
I have java.lang.ClassCastException: java.lang.String cannot be cast to javax.xml.namespace.QName.
But, my application compilled with Sun JDK 1.7.0_80 dont have this problem. This application has been working for many customers for many years.

I make simple example Main.java
When executing it in the Eclipse, an error occurs:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to javax.xml.namespace.QName
	at Main.main(Main.java:15)

If i make compile Main.java with javac 1.7.0_80 then no have execution errors.

Of course, I already created a custom ant task for building an application with sun javac. But this is a very inconvenient way of building.
Comment 1 Stephan Herrmann CLA 2018-02-07 11:20:21 EST
By minimal extension your program will throw CCE even when compiled with javac.

Simply add after your for-loop:
//---
            for (QName n : ports.keySet()) {
                System.out.println(n.getPrefix());
            }
//---

That program is broken, and javac just happens to generate bytecode that triggers the CCE later.

You are creating & filling a map with Entry<String,String>.

By use of the rawtype Map and a subsequent uncheck cast (two warnings) you force the compiler into accepting an erroneous program.

Any real use of the coerced Map will inevitable throw CCE.

Deferring the CCE to the point of usage is a bad idea when you have to debug such program, as it can be arbitrarily hard, to find the location that corrupted the Map.

Ecj works as designed.
Comment 2 Kirill Golovin CLA 2018-02-08 01:34:46 EST
Thank you for the explanation.
I know that the code contains incorrect casts. But I was wrong in thinking that the code is working correctly.