]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Bug254570Workaround.java
86b257a746ed53a0045f37b8d5be917ca1e50e24
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / internal / Bug254570Workaround.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.internal;\r
13 \r
14 import java.lang.reflect.Field;\r
15 import java.util.Arrays;\r
16 import java.util.Collections;\r
17 \r
18 import org.eclipse.jface.viewers.IElementComparer;\r
19 import org.eclipse.jface.viewers.ISelection;\r
20 import org.eclipse.jface.viewers.StructuredSelection;\r
21 \r
22 /**\r
23  * This class works around Eclipse bug #254570 - StructuredSelection is missing\r
24  * {@link #hashCode()} implementation.\r
25  * \r
26  * <p>\r
27  * Use {@link #wrapSelection(ISelection)} to obtain a wrapper for\r
28  * StructuredSelection instances that "injects" a hashCode implementation.\r
29  * \r
30  * @author Tuukka Lehtonen\r
31  * @deprecated does not work, do not use, causes problems with DB requests:\r
32  * <pre>\r
33  * Exception in thread "Query Thread 1" java.lang.IllegalArgumentException: Equal objects must have equal hashcodes. During rehashing, Trove discovered that the following two objects claim to be equal (as in java.lang.Object.equals()) but their hashCodes (or those calculated by your TObjectHashingStrategy) are not equal.This violates the general contract of java.lang.Object.hashCode().  See bullet point two in that method's documentation. object #1 =LazyViewpoint[22182778].childQuery; object #2 =LazyViewpoint[2672086].childQuery\r
34  * at org.simantics.db.impl.query.StableObjectHash.throwObjectContractViolation(StableObjectHash.java:348)\r
35  * at org.simantics.db.impl.query.StableHashMap.rehash(StableHashMap.java:410)\r
36  * at gnu.trove.THash.postInsertHook(THash.java:368)\r
37  * at org.simantics.db.impl.query.StableHashMap.doPut(StableHashMap.java:214)\r
38  * at org.simantics.db.impl.query.StableHashMap.put(StableHashMap.java:182)\r
39  * at org.simantics.db.impl.query.QueryProcessor.performForEach(QueryProcessor.java:1433)\r
40  * at org.simantics.db.impl.query.QueryProcessor.runRead(QueryProcessor.java:1020)\r
41  * at org.simantics.db.impl.query.QueryProcessor$4.run(QueryProcessor.java:1178)\r
42  * at org.simantics.db.impl.query.QueryProcessor$1.run(QueryProcessor.java:680)\r
43  * </pre>\r
44  */\r
45 @Deprecated\r
46 class Bug254570Workaround extends StructuredSelection {\r
47 \r
48     private final Object[]         elements;\r
49     private final IElementComparer comparer;\r
50 \r
51     public static ISelection wrapSelection(ISelection s) {\r
52         if (s == null)\r
53             return null;\r
54         try {\r
55             return StructuredSelection.class.equals(s.getClass()) ? Bug254570Workaround.make((StructuredSelection) s) : s;\r
56         } catch (SecurityException e) {\r
57             throw new RuntimeException("Workaround for #254570 failed", e);\r
58         } catch (NoSuchFieldException e) {\r
59             throw new RuntimeException("Workaround for #254570 failed", e);\r
60         } catch (IllegalArgumentException e) {\r
61             throw new RuntimeException("Workaround for #254570 failed", e);\r
62         } catch (IllegalAccessException e) {\r
63             throw new RuntimeException("Workaround for #254570 failed", e);\r
64         }\r
65         //return s;\r
66     }\r
67 \r
68     public static Bug254570Workaround make(StructuredSelection s) throws SecurityException,\r
69     NoSuchFieldException, IllegalArgumentException, IllegalAccessException {\r
70         if (s == null)\r
71             throw new NullPointerException("null StructuredSelection");\r
72 \r
73         Class<?> clazz = s.getClass();\r
74         Field elementsField = null;\r
75         Field comparerField = null;\r
76         for (Field f : clazz.getDeclaredFields()) {\r
77             if ("elements".equals(f.getName()))\r
78                 elementsField = f;\r
79             else if ("comparer".equals(f.getName()))\r
80                 comparerField = f;\r
81         }\r
82         if (elementsField == null)\r
83             throw new IllegalArgumentException("could not find field 'elements'");\r
84         if (comparerField == null)\r
85             throw new IllegalArgumentException("could not find field 'comparer'");\r
86 \r
87         Object[] elements = getAccessible(elementsField, s);\r
88         IElementComparer comparer = getAccessible(comparerField, s);\r
89 \r
90         return new Bug254570Workaround(elements, comparer);\r
91     }\r
92 \r
93     private static <T> T getAccessible(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {\r
94         boolean accessible = f.isAccessible();\r
95         try {\r
96             if (!accessible)\r
97                 f.setAccessible(true);\r
98             @SuppressWarnings("unchecked")\r
99             T t = (T) f.get(obj);\r
100             return t;\r
101         } finally {\r
102             if (!accessible)\r
103                 f.setAccessible(false);\r
104         }\r
105     }\r
106 \r
107     private Bug254570Workaround(Object[] elements, IElementComparer comparer) {\r
108         super(elements == null ? Collections.emptyList() : Arrays.asList(elements), comparer);\r
109         this.elements = elements;\r
110         this.comparer = comparer;\r
111     }\r
112 \r
113     private int hashCode(Object element) {\r
114         return element == null ? 0 :\r
115             comparer != null ? comparer.hashCode(element) : element.hashCode();\r
116     }\r
117 \r
118     /**\r
119      * StructuredSelection.hashCode is not implemented, need to implement\r
120      * it here.\r
121      */\r
122     @Override\r
123     public int hashCode() {\r
124         int code = 31;\r
125         if (elements != null) {\r
126             for (int i = 0; i < elements.length; i++) {\r
127                 code = code * 17 + hashCode(elements[i]);\r
128             }\r
129         }\r
130         return code;\r
131     }\r
132 }