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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt.internal;
\r
14 import java.lang.reflect.Field;
\r
15 import java.util.Arrays;
\r
16 import java.util.Collections;
\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
23 * This class works around Eclipse bug #254570 - StructuredSelection is missing
\r
24 * {@link #hashCode()} implementation.
\r
27 * Use {@link #wrapSelection(ISelection)} to obtain a wrapper for
\r
28 * StructuredSelection instances that "injects" a hashCode implementation.
\r
30 * @author Tuukka Lehtonen
\r
31 * @deprecated does not work, do not use, causes problems with DB requests:
\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
46 class Bug254570Workaround extends StructuredSelection {
\r
48 private final Object[] elements;
\r
49 private final IElementComparer comparer;
\r
51 public static ISelection wrapSelection(ISelection s) {
\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
68 public static Bug254570Workaround make(StructuredSelection s) throws SecurityException,
\r
69 NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
\r
71 throw new NullPointerException("null StructuredSelection");
\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
79 else if ("comparer".equals(f.getName()))
\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
87 Object[] elements = getAccessible(elementsField, s);
\r
88 IElementComparer comparer = getAccessible(comparerField, s);
\r
90 return new Bug254570Workaround(elements, comparer);
\r
93 private static <T> T getAccessible(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
\r
94 boolean accessible = f.isAccessible();
\r
97 f.setAccessible(true);
\r
98 @SuppressWarnings("unchecked")
\r
99 T t = (T) f.get(obj);
\r
103 f.setAccessible(false);
\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
113 private int hashCode(Object element) {
\r
114 return element == null ? 0 :
\r
115 comparer != null ? comparer.hashCode(element) : element.hashCode();
\r
119 * StructuredSelection.hashCode is not implemented, need to implement
\r
123 public int hashCode() {
\r
125 if (elements != null) {
\r
126 for (int i = 0; i < elements.length; i++) {
\r
127 code = code * 17 + hashCode(elements[i]);
\r