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

Loophole in typesafety

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_7

      Description

      FULL PRODUCT VERSION :


      A DESCRIPTION OF THE PROBLEM :
      The following program compiles without errors or warnings and does not contain explicit cast operators, but throws CastClassException at runtime, because it manages to pass a String array into a method accepting Integer array.

      interface A<T>
      {
          void foo(T x);
      }

      interface B<T> extends A<T[]> { }

      class C implements B<Integer>
      {
          static <T extends B<?>> void bar(T x)
          {
              x.foo(new String[]{"A"});
          }

          public void foo(Integer[] x)
          {
              int y = x[0];
          }

          public static void main(String[] args)
          {
              bar(new C());
          }
      }


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      interface A<T>
      {
          void foo(T x);
      }

      interface B<T> extends A<T[]> { }

      class C implements B<Integer>
      {
          static <T extends B<?>> void bar(T x)
          {
              x.foo(new String[]{"A"});
          }

          public void foo(Integer[] x)
          {
              int y = x[0];
          }

          public static void main(String[] args)
          {
              bar(new C());
          }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mcimadamore Maurizio Cimadamore
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: