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