提交 86730620 编写于 作者: P pchelko

7173464: Clipboard.getAvailableDataFlavors: Comparison method violates contract

Reviewed-by: anthony, art, serb
上级 3c4d1283
......@@ -98,8 +98,7 @@ public class ClipboardTransferable implements Transferable {
}
flavors = DataTransferer.getInstance().
setToSortedDataFlavorArray(flavorsToData.keySet(),
flavorsForFormats);
setToSortedDataFlavorArray(flavorsToData.keySet());
}
} finally {
clipboard.closeClipboard();
......
......@@ -2405,15 +2405,6 @@ search:
return retval;
}
/**
* 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>.
......@@ -2427,24 +2418,6 @@ search:
return flavors;
}
/**
* 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.
*/
......@@ -2724,11 +2697,9 @@ search:
* 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 @@ search:
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 @@ search:
}
}
// 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());
}
}
......
/*
* 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");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册