JDK-6507802 : (reflect) Class.asSubclass throws uninformative CCE
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 7
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: linux
  • CPU: x86
  • Submitted: 2006-12-25
  • Updated: 2024-04-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
Run:

---%<---
public class CaS_CCE {
    public static void main(String[] x) {
        try {
            Object o = "";
            Integer i = (Integer) o;
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }
        try {
            Object o = "";
            Integer i = Integer.class.cast(o);
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }
        try {
            Class<?> c = String.class;
            Class<? extends Integer> i = c.asSubclass(Integer.class);
        } catch (ClassCastException e) {
            System.out.println(e.getMessage());
        }
    }
}
---%<---

I get:

---%<---
java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b04)
Java HotSpot(TM) Client VM (build 1.7.0-ea-b04, mixed mode, sharing)

java.lang.String cannot be cast to java.lang.Integer
Cannot cast java.lang.String to java.lang.Integer
class java.lang.String
---%<---

That last message is not too helpful. #6456938 fixed the second one.

Comments
EVALUATION Yes.
04-01-2007

SUGGESTED FIX Change throw new ClassCastException(this.toString()); to e.g. throw new ClassCastException("Cannot cast " + getName() + " to " + clazz.getName());
25-12-2006