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