You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2009/07/13 15:20:33 UTC
svn commit: r793579 - in /harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/HashMap.java
test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
Author: tellison
Date: Mon Jul 13 13:20:33 2009
New Revision: 793579
URL: http://svn.apache.org/viewvc?rev=793579&view=rev
Log:
Fix for HARMONY-6265 ([classlib][luni] Improve the performance of HashMap)
Note that this is not only performance improvement, it is also a correctness fix for storing proxy objects in the hashmap.
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java?rev=793579&r1=793578&r2=793579&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/HashMap.java Mon Jul 13 13:20:33 2009
@@ -21,7 +21,6 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
-import java.lang.reflect.Proxy;
/**
* HashMap is an implementation of Map. All optional operations (adding and
@@ -481,16 +480,9 @@
final Entry<K,V> findNonNullKeyEntry(Object key, int index, int keyHash) {
Entry<K,V> m = elementData[index];
- // To support proxy instance as keys
- if (Proxy.isProxyClass(key.getClass())) {
- while (m != null && (m.origKeyHash != keyHash || key != m.key)) {
- m = m.next;
- }
- } else {
- while (m != null
- && (m.origKeyHash != keyHash || !key.equals(m.key))) {
- m = m.next;
- }
+ while (m != null
+ && (m.origKeyHash != keyHash || !areEqualKeys(key, m.key))) {
+ m = m.next;
}
return m;
}
@@ -824,11 +816,11 @@
}
static boolean areEqualKeys(Object key1, Object key2) {
- return key1.equals(key2);
+ return (key1 == key2) || key1.equals(key2);
}
static boolean areEqualValues(Object value1, Object value2) {
- return value1.equals(value2);
+ return (value1 == value2) || value1.equals(value2);
}
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java?rev=793579&r1=793578&r2=793579&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/HashMapTest.java Mon Jul 13 13:20:33 2009
@@ -29,7 +29,6 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
-import java.util.Map.Entry;
import org.apache.harmony.testframework.serialization.SerializationTest;
@@ -337,17 +336,47 @@
k.setKey(17);
assertNull(map.get(k));
-
+ }
+
+ /**
+ * Tests for proxy object keys and values
+ */
+ public void test_proxies() {
// Regression for HARMONY-6237
- MockInterface proxyInstance = (MockInterface) Proxy.newProxyInstance(
+ MockInterface proxyKey = (MockInterface) Proxy.newProxyInstance(
+ MockInterface.class.getClassLoader(),
+ new Class[] { MockInterface.class }, new MockHandler(
+ new MockClass()));
+ MockInterface proxyValue = (MockInterface) Proxy.newProxyInstance(
MockInterface.class.getClassLoader(),
new Class[] { MockInterface.class }, new MockHandler(
new MockClass()));
- hm.put(proxyInstance, "value2");
-
- assertEquals("Failed with proxy object key", "value2", hm
- .get(proxyInstance));
+ // Proxy key
+ Object val = new Object();
+ hm.put(proxyKey, val);
+
+ assertEquals("Failed with proxy object key", val, hm
+ .get(proxyKey));
+ assertTrue("Failed to find proxy key", hm.containsKey(proxyKey));
+ assertEquals("Failed to remove proxy object key", val,
+ hm.remove(proxyKey));
+ assertFalse("Should not have found proxy key", hm.containsKey(proxyKey));
+
+ // Proxy value
+ Object k = new Object();
+ hm.put(k, proxyValue);
+
+ assertTrue("Failed to find proxy object as value", hm.containsValue(proxyValue));
+
+ // Proxy key and value
+ HashMap map = new HashMap();
+ map.put(proxyKey, proxyValue);
+ assertTrue("Failed to find proxy key", map.containsKey(proxyKey));
+ assertEquals(1, map.size());
+ Object[] entries = map.entrySet().toArray();
+ Map.Entry entry = (Map.Entry)entries[0];
+ assertTrue("Failed to find proxy association", map.entrySet().contains(entry));
}
/**