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

ClassCastException occurs in assignment expressions without any heap pollutions

XMLWordPrintable

    • b70
    • x86
    • windows_xp
    • Verified

      FULL PRODUCT VERSION :
      java version "1.6.0_11"
      Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
      Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)

      javac 1.6.0_11


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The compiler allows non-reifiable array type when invoking variable arity generic methods. For example, an invocation of a method "<T> T[] error(T...a)" with arguments (java.lang.Integer, java.lang.Long) makes the type parameter "T" infer to a non-reifiable type (seems Number & Comparable<...>).
      Then a result of the expression "error(1, 2L)" is assignable to "Comparable<?>[]" with no warnings, although the compiler evaluates the expression as "error(new Number[] {1, 2L})". As a result, the assignment from this expression to a variable of "Comparable<?>[]" occurs ClassCastException (Number[] -> Comparable[]).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run below "Source code for an executable test case"

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Show Errors/Warnings when the compiler try to create a non-reifiable array type.
      ACTUAL -
      Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Number; can
      not be cast to [Ljava.lang.Comparable;
              at CceOnAssign.main(CceOnAssign.java:6)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class CceOnAssign {
          
          public static void main(String[] args) {
              Integer a = 1;
              Long b = 2L;
              Comparable<?>[] result = error(a, b);
          }
          
          static <T> T[] error(T...a) {
              return a;
          }
      }
      ---------- END SOURCE ----------

            mcimadamore Maurizio Cimadamore
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: