Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4089896
Votes 0
Synopsis Hashtable.put() calls itself after a rehash()
Category java:classes_util
Reported Against 3.0 , 1.1.2 , 1.1.4 , 1.1.5
Release Fixed 1.2(1.2beta3)
State 11-Closed, Unverified, request for enhancement
Priority: 3-Medium
Related Bugs 4075132 , 4201900
Submit Date 30-OCT-1997
Description




1. Write a subclass of Hashtable which overrides
put() and in that put() method, call super.put().

2. 

import java.util.*;

public class BrokenHash extends Hashtable {

  public BrokenHash() {}

  public Object put(Object key, Object value) {
    String myValue = "X" + String.valueOf(value);
    return super.put(key, myValue);
  }

  private static boolean rehashed = false;

  public void rehash() {
    rehashed = true;
    super.rehash();
  }

  public static void main(String[] argv) {
    BrokenHash h = new BrokenHash();
    while (!rehashed) {
      h.put(new Object(), new Object());
    }

    Enumeration values = h.elements();
    while (values.hasMoreElements()) {
      String v = (String)values.nextElement();
      if (v.startsWith("XX")) System.out.println(v + " <<< BUG");
    }
  }
}

3.
bash$ javac BrokenHash.java
bash$ java BrokenHash
 xxxxx@xxxxx  <<< BUG
bash$

4. None.

5. The problem is tat Hashtable.put() calls itself
if it has to rehash(). Which ends up calling back
into the subclasses version of put(). Which is not
what you want.
(Review ID: 14947)
======================================================================
Work Around




*** Hashtable.java.orig	Mon Oct 06 15:09:13 1997
--- Hashtable.java	Mon Oct 06 15:08:57 1997
***************
*** 345,351 ****
          if (count >= threshold) {
              // Rehash the table if the threshold is exceeded
              rehash();
!             return put(key, value);
          } 
  
          // Creates the new entry.
--- 345,352 ----
          if (count >= threshold) {
              // Rehash the table if the threshold is exceeded
              rehash();
!             tab = table;
!             index = (hash & 0x7FFFFFFF) % tab.length;
          } 
  
          // Creates the new entry.
======================================================================
Evaluation
     I don't consider this a bug per se, but rather an RFE; Hashtable makes no commitments as to how it implements its operations in terms of its  xxxxx  operations.  That said, I think that the suggested implementation is cleaner and better then the current implementation and I'll happily changed it.

    This sort of problem can be avoided by using delegation rather than subclassing.

 xxxxx@xxxxx  1997-11-09
Comments
  
  Include a link with my name & email   

Submitted On 07-JUN-1999
wft
I know this one is closed, but please fix it back in the 1.1.x maintenance
stream
This may not be a bug, but the original implementation was less than perfect.
And saying that the class makes no commitments on the implemenation is a
cop-out.



PLEASE NOTE: JDK6 is formerly known as Project Mustang