changeset 6797:837746b32b81

7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract Reviewed-by: anthony, art, serb
author pchelko
date Fri, 15 Nov 2013 14:04:16 +0400
parents 7383847ce180
children def34c4a7986
files src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java src/share/classes/sun/awt/datatransfer/DataTransferer.java test/sun/awt/datatransfer/DataFlavorComparatorTest.java
diffstat 3 files changed, 51 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Sat Oct 19 15:19:40 2013 -0700
+++ b/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java	Fri Nov 15 14:04:16 2013 +0400
@@ -98,8 +98,7 @@
                 }
 
                 flavors = DataTransferer.getInstance().
-                    setToSortedDataFlavorArray(flavorsToData.keySet(),
-                                               flavorsForFormats);
+                    setToSortedDataFlavorArray(flavorsToData.keySet());
             }
         } finally {
             clipboard.closeClipboard();
--- a/src/share/classes/sun/awt/datatransfer/DataTransferer.java	Sat Oct 19 15:19:40 2013 -0700
+++ b/src/share/classes/sun/awt/datatransfer/DataTransferer.java	Fri Nov 15 14:04:16 2013 +0400
@@ -2406,15 +2406,6 @@
     }
 
     /**
-     * Helper function to reduce a Map with DataFlavor keys to a DataFlavor
-     * array. The array will be sorted according to
-     * <code>DataFlavorComparator</code>.
-     */
-    public static DataFlavor[] keysToDataFlavorArray(Map map) {
-        return setToSortedDataFlavorArray(map.keySet(), map);
-    }
-
-    /**
      * Helper function to convert a Set of DataFlavors to a sorted array.
      * The array will be sorted according to <code>DataFlavorComparator</code>.
      */
@@ -2428,24 +2419,6 @@
     }
 
     /**
-     * Helper function to convert a Set of DataFlavors to a sorted array.
-     * The array will be sorted according to a
-     * <code>DataFlavorComparator</code> created with the specified
-     * flavor-to-native map as an argument.
-     */
-    public static DataFlavor[] setToSortedDataFlavorArray
-        (Set flavorsSet, Map flavorToNativeMap)
-    {
-        DataFlavor[] flavors = new DataFlavor[flavorsSet.size()];
-        flavorsSet.toArray(flavors);
-        Comparator comparator =
-            new DataFlavorComparator(flavorToNativeMap,
-                                     IndexedComparator.SELECT_WORST);
-        Arrays.sort(flavors, comparator);
-        return flavors;
-    }
-
-    /**
      * Helper function to convert an InputStream to a byte[] array.
      */
     protected static byte[] inputStreamToByteArray(InputStream str)
@@ -2723,11 +2696,9 @@
      * application/x-java-* MIME types. Unknown application types are preferred
      * because if the user provides his own data flavor, it will likely be the
      * most descriptive one. For flavors which are otherwise equal, the
-     * flavors' native formats are compared, with greater long values
-     * taking precedence.
+     * flavors' string representation are compared in the alphabetical order.
      */
     public static class DataFlavorComparator extends IndexedComparator {
-        protected final Map flavorToFormatMap;
 
         private final CharsetComparator charsetComparator;
 
@@ -2863,20 +2834,6 @@
             super(order);
 
             charsetComparator = new CharsetComparator(order);
-            flavorToFormatMap = Collections.EMPTY_MAP;
-        }
-
-        public DataFlavorComparator(Map map) {
-            this(map, SELECT_BEST);
-        }
-
-        public DataFlavorComparator(Map map, boolean order) {
-            super(order);
-
-            charsetComparator = new CharsetComparator(order);
-            HashMap hashMap = new HashMap(map.size());
-            hashMap.putAll(map);
-            flavorToFormatMap = Collections.unmodifiableMap(hashMap);
         }
 
         public int compare(Object obj1, Object obj2) {
@@ -2972,10 +2929,9 @@
                 }
             }
 
-            // As a last resort, take the DataFlavor with the greater integer
-            // format.
-            return compareLongs(flavorToFormatMap, flavor1, flavor2,
-                                UNKNOWN_OBJECT_LOSES_L);
+            // The flavours are not equal but still not distinguishable.
+            // Compare String representations in alphabetical order
+            return flavor1.getMimeType().compareTo(flavor2.getMimeType());
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/sun/awt/datatransfer/DataFlavorComparatorTest.java	Fri Nov 15 14:04:16 2013 +0400
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+   @bug 7173464
+   @summary Clipboard.getAvailableDataFlavors: Comparison method violates contract
+   @author Petr Pchelko
+   @run main DataFlavorComparatorTest
+*/
+
+import sun.awt.datatransfer.DataTransferer;
+
+import java.awt.datatransfer.DataFlavor;
+
+public class DataFlavorComparatorTest {
+
+    public static void main(String[] args) {
+        DataTransferer.DataFlavorComparator comparator = new DataTransferer.DataFlavorComparator();
+        DataFlavor flavor1 = new DataFlavor("application/image", "Image");
+        DataFlavor flavor2 = new DataFlavor("application/html", "HTML");
+        if (comparator.compare(flavor1, flavor2) == 0) {
+            throw new RuntimeException(flavor1.getMimeType() + " and " + flavor2.getMimeType() +
+                " should not be equal");
+        }
+    }
+}