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

Generic method allowing passing of types that don't match collection types

XMLWordPrintable

    • b49
    • x86
    • windows_xp



      Name: rmT116609 Date: 03/25/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
      Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP Professional

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      -source 1.5 is used to compile generic code

      A DESCRIPTION OF THE PROBLEM :
      When you create a generic method the takes a generic type and an array type the compiler allows you to call with two different types. Best in code.

      public static <Var> void transferBug(Var[] from, Collection<Var> to){
             to.add(from[0]);
      }

      The above method can be called with this snippet.

      Object[] objArray = {new Object()};
      ArrayList<String> strList = new ArrayList<String>();
      transferBug(a, strList);

      Which results in an Object in an ArrayList designed to only hold strings
      Continuing the code this would cause a Class cast exception, but not be noted on compile.

      String str = strList.get(0);


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the sample code provided

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Shoud the compiler generate an error like the following.

      <Var>transferBug(Var[], java.util.ArrayList<Var>)
      cannot be applied to (java.lang.Object,java.util.ArrayList<java.lang.String>)
      ; no instance(s) of type variable(s) Var exist so that argument type java.util.ArrayList<java.lang.S
      tring> conforms to formal parameter type Object


      ACTUAL -
      With the error in place it will compile and generate the following exception

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Exception in thread "main" java.lang.ClassCastException: java.lang.Object
              at BugDemo.main(BugDemo.java:11)


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.ArrayList;
      import java.util.Collection;

      public class BugDemo{

          public static void main(String[] args){
              Object[] objArray = {new Object()};
              ArrayList<String> strList = new ArrayList<String>();
              transferBug(objArray, strList);
              String str = strList.get(0);
      }

      public static <Var> void transferBug(Var[] from, Collection<Var> to){
          to.add(from[0]);
      }
      }
      ---------- END SOURCE ----------
      (Incident Review ID: 244871)
      ======================================================================

            gafter Neal Gafter (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: