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