Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6507802

(reflect) Class.asSubclass throws uninformative CCE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 7
    • core-libs

      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.

            Unassigned Unassigned
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: