Name: rm29839 Date: 10/30/97
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
XXjava.lang.Object@1ecb10 <<< 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)
======================================================================