You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/09/28 10:53:45 UTC

svn commit: r1705620 - in /commons/proper/collections/trunk/src: changes/changes.xml main/java/org/apache/commons/collections4/keyvalue/MultiKey.java test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java

Author: tn
Date: Mon Sep 28 08:53:44 2015
New Revision: 1705620

URL: http://svn.apache.org/viewvc?rev=1705620&view=rev
Log:
[COLLECTIONS-576] Fix de-serialization of MultiKey subclasses: hashcode was not re-calculated. Thanks to Stephan Roch.

Modified:
    commons/proper/collections/trunk/src/changes/changes.xml
    commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/keyvalue/MultiKey.java
    commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java

Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1705620&r1=1705619&r2=1705620&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Mon Sep 28 08:53:44 2015
@@ -22,6 +22,9 @@
   <body>
 
   <release version="4.1" date="TBD" description="">
+    <action issue="COLLECTIONS-576" dev="tn" type="fix" due-to="Stephan Roch">
+      Subclasses of MultiKey did not re-calculate their hashcode after de-serialization.
+    </action>
     <action issue="COLLECTIONS-572" dev="tn" type="add">
       Added set operations to "SetUtils": union, difference, intersection and disjunction.
       The operations return a view of the result that is backed by the input sets.

Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/keyvalue/MultiKey.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/keyvalue/MultiKey.java?rev=1705620&r1=1705619&r2=1705620&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/keyvalue/MultiKey.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/keyvalue/MultiKey.java Mon Sep 28 08:53:44 2015
@@ -274,7 +274,7 @@ public class MultiKey<K> implements Seri
      * only stable for the same process).
      * @return the instance with recalculated hash code
      */
-    private Object readResolve() {
+    protected Object readResolve() {
         calculateHashCode(keys);
         return this;
     }

Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java?rev=1705620&r1=1705619&r2=1705620&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/keyvalue/MultiKeyTest.java Mon Sep 28 08:53:44 2015
@@ -254,4 +254,42 @@ public class MultiKeyTest extends TestCa
         final MultiKey<?> mk2 = new MultiKey<Object>(ONE, sysKey);
         assertEquals(TWO, map2.get(mk2));
     }
+    
+    static class DerivedMultiKey<T> extends MultiKey<T> {
+
+        private static final long serialVersionUID = 1928896152249821416L;
+
+        public DerivedMultiKey(T key1, T key2) {
+            super(key1, key2);
+        }
+
+        public T getFirst() {
+            return getKey(0);
+        }
+
+        public T getSecond() {
+            return getKey(1);
+        }
+
+    }
+
+    public void testEqualsAfterSerializationOfDerivedClass() throws IOException, ClassNotFoundException
+    {
+        final DerivedMultiKey<?> mk = new DerivedMultiKey<String>("A", "B");
+
+        // serialize
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        final ObjectOutputStream out = new ObjectOutputStream(baos);
+        out.writeObject(mk);
+        out.close();
+
+        // deserialize
+        final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+        final ObjectInputStream in = new ObjectInputStream(bais);
+        final DerivedMultiKey<?> mk2 = (DerivedMultiKey<?>)in.readObject();
+        in.close();
+
+        assertEquals(mk.hashCode(), mk2.hashCode());
+    }
+    
 }