# HG changeset patch # User pchelko # Date 1376486409 -14400 # Node ID f6324eb18ece3299f48cf16a637a53a298b5a663 # Parent 0b4d21558b32ffd87f61337379daf45192ecfbc3 7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract Reviewed-by: anthony, art, serb diff -r 0b4d21558b32 -r f6324eb18ece jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java --- a/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java Wed Aug 14 16:17:28 2013 +0400 +++ b/jdk/src/share/classes/sun/awt/datatransfer/ClipboardTransferable.java Wed Aug 14 17:20:09 2013 +0400 @@ -98,8 +98,7 @@ } flavors = DataTransferer.getInstance(). - setToSortedDataFlavorArray(flavorsToData.keySet(), - flavorsForFormats); + setToSortedDataFlavorArray(flavorsToData.keySet()); } } finally { clipboard.closeClipboard(); diff -r 0b4d21558b32 -r f6324eb18ece jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java --- a/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java Wed Aug 14 16:17:28 2013 +0400 +++ b/jdk/src/share/classes/sun/awt/datatransfer/DataTransferer.java Wed Aug 14 17:20:09 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 - * DataFlavorComparator. - */ - 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 DataFlavorComparator. */ @@ -2428,24 +2419,6 @@ } /** - * Helper function to convert a Set of DataFlavors to a sorted array. - * The array will be sorted according to a - * DataFlavorComparator 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) @@ -2724,11 +2697,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; @@ -2864,20 +2835,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) { @@ -2973,10 +2930,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()); } } diff -r 0b4d21558b32 -r f6324eb18ece jdk/test/sun/awt/datatransfer/DataFlavorComparatorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/sun/awt/datatransfer/DataFlavorComparatorTest.java Wed Aug 14 17:20:09 2013 +0400 @@ -0,0 +1,47 @@ +/* + * 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 = DataFlavor.imageFlavor; + DataFlavor flavor2 = DataFlavor.selectionHtmlFlavor; + if (comparator.compare(flavor1, flavor2) == 0) { + throw new RuntimeException(flavor1.getMimeType() + " and " + flavor2.getMimeType() + + " should not be equal"); + } + } +} +