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

Spec clarification in Map interface

    XMLWordPrintable

Details

    • rc1
    • generic, x86, sparc
    • generic, solaris_2.5, solaris_2.6, windows_nt

    Description



      Name: ksC84122 Date: 12/25/98


      java.util.jar.Attributes.containsKey(null) doesn't throw NullPointerException.
      In fact java.util.jar.Attributes.containsKey(null) returns false.

      java.util.jar.Attributes implements interface java.util.Map
      JavaDoc for java.util.Map.containsKey() says:
      ---------------------------------
      containsKey

      public boolean containsKey(Object key)

           Returns true if this map contains a mapping for the specified key.
           Parameters:
               key - key whose presence in this map is to be tested.
           Returns:
               true if this map contains a mapping for the specified key.
           Throws:
               ClassCastException - if the key is of an inappropriate type for this
      map.
               NullPointerException - if the key is null and this map does not not
      permit null keys.
      ---------------------------------

      However, as follows from JavaDoc comments ("Attribute names are case-insensitive
      and restricted to the ASCII characters in the set [0-9a-zA-Z_-]"), the
      Attributes map should not permit null keys.
      Therefore NullPointerException is expected to be thrown by containsKey(null).

      Please note that all other Attributes key related methods called with null
      should throw NullPointerException. These methods are:
      java.util.jar.Attributes.get(null),
      java.util.jar.Attributes.put(null, value),
      java.util.jar.Attributes.getValue(Attributes.Name null)
      Currently they also do not throw NullPointerException.

      An example to find out containsKey(null):
      ===== Test17.java ========
      import java.util.jar.Attributes;

      public class Test17 {

          public static void main (String argv[]) {
            if ( (new Attributes()).containsKey(null) == false ) {
                System.out.println("containsKey(null) == false");
            }
            else {
                System.out.println("containsKey(null) == true");
            }
            return;
          }
      }
      ========= Sample run (JDK-1.2fcs) ==========
      #>java Test17
      containsKey(null) == false

      ======================================================================



      Name: boT120536 Date: 12/07/2000


      java version "1.3.0_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
      Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)


      AbstractMap.equals() calls get() and containsKey() on the map to be compared
      with. The specification explicitly allows these methods to throw ClassCast- and
      NullPointerExceptions (unless being given valid objects). As AbstractMap (and
      its subclasses) do not know anything about the other Map, they should not call
      get() or containsKey() on it, instead iterate over the other Map and call get()
      and containsKey() on itself.

      Example:

      import java.util.*;


      public class MapTest
      {
          public static void main(String[] args)
          {
              Map m = new HashMap();

              m.put(null, "Dummy");

              Map n = new Hashtable();
              
              n.put("something", "Dummy");

              System.out.println(m.equals(n));
          }
      }

      Here equals() throws NullPointerException, instead it should return false.
      The same will happen with TreeMap if the

      I haven't investigated if this also affects the subview collections (entries(),
      keySet(), values()).
      (Review ID: 113439)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              jjb Josh Bloch
              ksoshals Kirill Soshalskiy (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: