]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ThrowableBinding.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / ThrowableBinding.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.databoard.binding.impl;
13
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
16
17 import org.simantics.databoard.Bindings;
18 import org.simantics.databoard.Datatypes;
19 import org.simantics.databoard.binding.Binding;
20 import org.simantics.databoard.binding.RecordBinding;
21 import org.simantics.databoard.binding.error.BindingConstructionException;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.type.OptionalType;
24 import org.simantics.databoard.type.RecordType;
25
26 /**
27  * This class Binds throwables to the following record: 
28  *   { detailMessage : Optional(String) }
29  *   
30  * The bound throwable class must have constructor( String ).
31  *
32  * @author toni.kalajainen
33  */
34 public class ThrowableBinding extends RecordBinding {
35
36         private Class<Throwable> boundClass;
37         private Constructor<Throwable> constructor;
38         private Constructor<Throwable> constructorNoArgs;
39         
40         public ThrowableBinding( Class<Throwable> classToBindTo ) throws BindingConstructionException {
41                 this.boundClass = classToBindTo;
42                 type = new RecordType();
43                 RecordType rt = (RecordType) type;
44                 rt.addComponent("detailMessage", new OptionalType( Datatypes.STRING ));         
45                 this.componentBindings = new Binding[1];
46                 this.componentBindings[0] = new OptionalBindingDefault( Bindings.STRING );
47                 try {
48                         constructor = boundClass.getConstructor( String.class );
49                         constructor.setAccessible( true );
50                 } catch (SecurityException e) {
51                         throw new BindingConstructionException( e );
52                 } catch (NoSuchMethodException e) {
53                         throw new BindingConstructionException( e );
54                 }
55                 
56                 try {
57                         constructorNoArgs = boundClass.getConstructor();
58                         constructorNoArgs.setAccessible( true );
59                 } catch (SecurityException e) {
60                 } catch (NoSuchMethodException e) {
61                 }
62                 
63         }
64
65         @Override
66         public Object getComponent(Object obj, int index) throws BindingException {
67                 if ( index != 0 ) throw new BindingException("Index out of range");
68                 if ( obj instanceof Throwable == false ) throw new BindingException("not Throwable");
69                 Throwable t = (Throwable) obj;
70                 return t.getMessage();
71         }
72
73         @Override
74         public Object create(Object... values) throws BindingException {
75                 if (values.length!=1) throw new BindingException("Invalid argument");
76                 try {
77                         return constructor.newInstance( values );
78                 } catch (IllegalArgumentException e) {
79                         throw new BindingException( e ); 
80                 } catch (InstantiationException e) {
81                         throw new BindingException( e ); 
82                 } catch (IllegalAccessException e) {
83                         throw new BindingException( e ); 
84                 } catch (InvocationTargetException e) {
85                         throw new BindingException( e.getCause() ); 
86                 }
87         }
88
89         @Override
90         public Object createPartial() throws BindingException {
91                 if ( constructorNoArgs!=null ) {
92                         try {
93                                 return constructorNoArgs.newInstance();
94                         } catch (IllegalArgumentException e) {
95                                 throw new BindingException( e );
96                         } catch (InstantiationException e) {
97                                 throw new BindingException( e );
98                         } catch (IllegalAccessException e) {
99                                 throw new BindingException( e );
100                         } catch (InvocationTargetException e) {
101                                 throw new BindingException( e.getCause() );
102                         }
103                 }
104                 
105                 try {
106                         return constructor.newInstance((Object[]) null);
107                 } catch (IllegalArgumentException e) {
108                         throw new BindingException( e );
109                 } catch (InstantiationException e) {
110                         throw new BindingException( e );
111                 } catch (IllegalAccessException e) {
112                         throw new BindingException( e );
113                 } catch (InvocationTargetException e) {
114                         throw new BindingException( e.getCause() );
115                 }
116         }
117
118         @Override
119         public void setComponents(Object obj, Object... value)
120                         throws BindingException {
121 //              if ( value.length != 1 ) throw new BindingException("Array out of range");
122 //              if ( obj instanceof Throwable == false ) throw new BindingException("not Throwable");
123 //              Throwable t = (Throwable) obj;
124                 throw new BindingException("Cannot set message to Throwable");
125         }
126
127         @Override
128         public void setComponent(Object obj, int index, Object value)
129                         throws BindingException {
130                 throw new BindingException("Cannot set message to Throwable");
131         }
132
133         @Override
134         public boolean isInstance(Object obj) {
135                 return obj instanceof Throwable;
136         }
137
138         @Override
139         protected boolean baseEquals(Object obj) {
140                 ThrowableBinding o = (ThrowableBinding)obj;
141                 return super.baseEquals(obj) && o.boundClass.equals(boundClass);
142         }
143         
144         @Override
145         public int baseHashCode() {
146                 return super.baseHashCode() + 13 * boundClass.hashCode();
147         }
148 }