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

(coll) checked collection of primitive type: can create but cannot add to it

    Details

      Description

      A DESCRIPTION OF THE FIX :
        Bug Description : This is about java.util.Collection.checkedCollection(Collection c, Class type)
      One can create checked collection of primitive type. But an instance of such collection is unusable because it's impossible to add any element to it. I propose to convert primitive type to its reference wrapper type while creating checked collection (or forbid creation of checked collection of primitive type to identify the problem early).
      Subset of Releases affected : 1.5.0, Mustang b72.
      Test case : See Test case section.
      Diff baseline : Mustang b72
      Diff (convertion to reference type) :
      --- old\java\util\Collections.java Thu Feb 16 00:12:30 2006
      +++ new\java\util\Collections.java Tue Feb 28 17:46:15 2006
      @@ -2211,7 +2211,7 @@
                   if (c==null || type == null)
                       throw new NullPointerException();
                   this.c = c;
      - this.type = type;
      + this.type = toReferenceClass(type);
               }
       
               public int size() { return c.size(); }
      @@ -2280,6 +2280,30 @@
                   return zeroLengthElementArray;
               }
           }
      + // where
      + @SuppressWarnings("unchecked")
      + static <T> Class<T> toReferenceClass(Class<T> type) {
      + if(!type.isPrimitive())
      + return type;
      + if(type == boolean.class)
      + return (Class<T>) Boolean.class;
      + if(type == char.class)
      + return (Class<T>) Character.class;
      + if(type == byte.class)
      + return (Class<T>) Byte.class;
      + if(type == short.class)
      + return (Class<T>) Short.class;
      + if(type == int.class)
      + return (Class<T>) Integer.class;
      + if(type == long.class)
      + return (Class<T>) Long.class;
      + if(type == float.class)
      + return (Class<T>) Float.class;
      + if(type == double.class)
      + return (Class<T>) Double.class;
      + // void.class remains
      + throw new IllegalArgumentException(type + " is not permitted");
      + }
       
           /**
            * Returns a dynamically typesafe view of the specified set.
      Alternative Diff (forbid creation of primitive type checked collection):
      --- old\java\util\Collections.java Thu Feb 16 00:12:30 2006
      +++ new\java\util\Collections.java Tue Feb 28 16:51:42 2006
      @@ -2210,6 +2210,8 @@
               CheckedCollection(Collection<E> c, Class<E> type) {
                   if (c==null || type == null)
                       throw new NullPointerException();
      + if (type.isPrimitive())
      + throw new IllegalArgumentException("primitive types are not permitted");
                   this.c = c;
                   this.type = type;
               }


      JUnit TESTCASE :
      // Test Case (convertion to reference type)
      import java.util.*;

      import junit.framework.TestCase;

      public class CheckedCollectionTest extends TestCase {

      private Collection collection;

      public void setUp() {
      collection = Collections.checkedCollection(new ArrayList(), int.class);
      }

      public void testAdd() throws Exception {
      collection.add(10);
      }

      public void testAddAll() throws Exception {
      collection.addAll(Collections.emptyList());
      }

      public static void main(String[] args) {
      junit.textui.TestRunner.run(CheckedCollectionTest.class);
      }

      }

      // Alternative Test Case (forbid creation of primitive type checked collection):
      import java.util.*;

      import junit.framework.TestCase;

      public class CheckedCollectionTest extends TestCase {

      public void test() throws Exception {
      try {
      Collections.checkedCollection(new ArrayList(), int.class);
      fail("primitive types should not be permitted");
      }
      catch(IllegalArgumentException exc) {
      // ok
      }
      }

      public static void main(String[] args) {
      junit.textui.TestRunner.run(CheckedCollectionTest.class);
      }

      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                tbell Tim Bell
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Imported:
                  Indexed: