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

Missing checkcast when casting to type parameter bounded by intersection type

XMLWordPrintable

    • b13
    • Verified

      In the following example the method 'f' completes without exceptions, and the method 'f' (which differs only in the order of the components of the bounds of its type parameter) fails with a CCE.

      This seems similar to JDK-8011392. Should javac be emitting additional checkcasts for the components of the type parameters' bounds, or only for the erased type?

      ```
      import java.util.*;

      class T {
        public static void main(String[] args) {
          f();
          g(); // CCE
        }

        static <T extends List<?> & Runnable> void f() {
          Runnable r = (T) new ArrayList<>();
        }

        static <T extends Runnable & List<?>> void g() {
          Runnable r = (T) new ArrayList<>();
        }
      }
      ```

      $ javap T
      ...
        static <T extends java.util.List<?> & java.lang.Runnable> void f();
          descriptor: ()V
          flags: (0x0008) ACC_STATIC
          Code:
            stack=2, locals=1, args_size=0
               0: new #4 // class java/util/ArrayList
               3: dup
               4: invokespecial #5 // Method java/util/ArrayList."<init>":()V
               7: astore_0
               8: return
      ...
        static <T extends java.lang.Runnable & java.util.List<?>> void g();
          descriptor: ()V
          flags: (0x0008) ACC_STATIC
          Code:
            stack=2, locals=1, args_size=0
               0: new #4 // class java/util/ArrayList
               3: dup
               4: invokespecial #5 // Method java/util/ArrayList."<init>":()V
               7: checkcast #6 // class java/lang/Runnable
              10: astore_0
              11: return
      $ java T
      Exception in thread "main" java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.Runnable (java.util.ArrayList and java.lang.Runnable are in module java.base of loader 'bootstrap')
              at T.g(T.java:14)
              at T.main(T.java:6)

            vromero Vicente Arturo Romero Zaldivar
            cushon Liam Miller-Cushon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: