changeset 7308:ffe9f459d921

Fix a serialization regression caused by Comparator default method reverseOrder
author henryjen
date Mon, 11 Feb 2013 13:35:27 -0800
parents 830af530db9c
children add7a96e56d3
files src/share/classes/java/util/Collections.java test/java/util/Collections/ReverseOrder.java
diffstat 2 files changed, 36 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/util/Collections.java	Mon Feb 11 16:21:51 2013 -0500
+++ b/src/share/classes/java/util/Collections.java	Mon Feb 11 13:35:27 2013 -0800
@@ -3786,7 +3786,7 @@
             return c2.compareTo(c1);
         }
 
-        private Object readResolve() { return reverseOrder(); }
+        private Object readResolve() { return Collections.reverseOrder(); }
     }
 
     /**
--- a/test/java/util/Collections/ReverseOrder.java	Mon Feb 11 16:21:51 2013 -0500
+++ b/test/java/util/Collections/ReverseOrder.java	Mon Feb 11 13:35:27 2013 -0800
@@ -23,23 +23,56 @@
 
 /*
  * @test
- * @bug 4593209
+ * @bug 4593209 8001667
  * @summary Reverse comparator was subtly broken
  * @author Josh bloch
  */
 
 import java.util.*;
+import java.io.*;
 
 public class ReverseOrder {
+    static byte[] serialBytes(Object o) {
+        try {
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            ObjectOutputStream oos = new ObjectOutputStream(bos);
+            oos.writeObject(o);
+            oos.flush();
+            oos.close();
+            return bos.toByteArray();
+        } catch (Throwable t) {
+            throw new Error(t);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    static <T> T serialClone(T o) {
+        try {
+            ObjectInputStream ois = new ObjectInputStream
+                (new ByteArrayInputStream(serialBytes(o)));
+            T clone = (T) ois.readObject();
+            return clone;
+        } catch (Throwable t) {
+            throw new Error(t);
+        }
+    }
+
     public static void main(String[] args) throws Exception {
         Foo[] a = { new Foo(2), new Foo(3), new Foo(1) };
         List list = Arrays.asList(a);
-        Collections.sort(list, Collections.reverseOrder());
+        Comparator cmp = Collections.reverseOrder();
+        Collections.sort(list, cmp);
 
         Foo[] golden = { new Foo(3), new Foo(2), new Foo(1) };
         List goldenList = Arrays.asList(golden);
         if (!list.equals(goldenList))
             throw new Exception(list.toString());
+
+        Comparator clone = serialClone(cmp);
+        List list2 = Arrays.asList(a);
+        Collections.sort(list2, clone);
+        if (!list2.equals(goldenList))
+            throw new Exception(list.toString());
     }
 }