]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ThrowableBinding.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / ThrowableBinding.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ThrowableBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/ThrowableBinding.java
new file mode 100644 (file)
index 0000000..b55b9d0
--- /dev/null
@@ -0,0 +1,148 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.binding.impl;\r
+\r
+import java.lang.reflect.Constructor;\r
+import java.lang.reflect.InvocationTargetException;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Datatypes;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.RecordBinding;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.OptionalType;\r
+import org.simantics.databoard.type.RecordType;\r
+\r
+/**\r
+ * This class Binds throwables to the following record: \r
+ *   { detailMessage : Optional(String) }\r
+ *   \r
+ * The bound throwable class must have constructor( String ).\r
+ *\r
+ * @author toni.kalajainen\r
+ */\r
+public class ThrowableBinding extends RecordBinding {\r
+\r
+       private Class<Throwable> boundClass;\r
+       private Constructor<Throwable> constructor;\r
+       private Constructor<Throwable> constructorNoArgs;\r
+       \r
+       public ThrowableBinding( Class<Throwable> classToBindTo ) throws BindingConstructionException {\r
+               this.boundClass = classToBindTo;\r
+               type = new RecordType();\r
+               RecordType rt = (RecordType) type;\r
+               rt.addComponent("detailMessage", new OptionalType( Datatypes.STRING ));         \r
+               this.componentBindings = new Binding[1];\r
+               this.componentBindings[0] = new OptionalBindingDefault( Bindings.STRING );\r
+               try {\r
+                       constructor = boundClass.getConstructor( String.class );\r
+                       constructor.setAccessible( true );\r
+               } catch (SecurityException e) {\r
+                       throw new BindingConstructionException( e );\r
+               } catch (NoSuchMethodException e) {\r
+                       throw new BindingConstructionException( e );\r
+               }\r
+               \r
+               try {\r
+                       constructorNoArgs = boundClass.getConstructor();\r
+                       constructorNoArgs.setAccessible( true );\r
+               } catch (SecurityException e) {\r
+               } catch (NoSuchMethodException e) {\r
+               }\r
+               \r
+       }\r
+\r
+       @Override\r
+       public Object getComponent(Object obj, int index) throws BindingException {\r
+               if ( index != 0 ) throw new BindingException("Index out of range");\r
+               if ( obj instanceof Throwable == false ) throw new BindingException("not Throwable");\r
+               Throwable t = (Throwable) obj;\r
+               return t.getMessage();\r
+       }\r
+\r
+       @Override\r
+       public Object create(Object... values) throws BindingException {\r
+               if (values.length!=1) throw new BindingException("Invalid argument");\r
+               try {\r
+                       return constructor.newInstance( values );\r
+               } catch (IllegalArgumentException e) {\r
+                       throw new BindingException( e ); \r
+               } catch (InstantiationException e) {\r
+                       throw new BindingException( e ); \r
+               } catch (IllegalAccessException e) {\r
+                       throw new BindingException( e ); \r
+               } catch (InvocationTargetException e) {\r
+                       throw new BindingException( e ); \r
+               }\r
+       }\r
+\r
+       @Override\r
+       public Object createPartial() throws BindingException {\r
+               if ( constructorNoArgs!=null ) {\r
+                       try {\r
+                               return constructorNoArgs.newInstance();\r
+                       } catch (IllegalArgumentException e) {\r
+                               throw new BindingException( e );\r
+                       } catch (InstantiationException e) {\r
+                               throw new BindingException( e );\r
+                       } catch (IllegalAccessException e) {\r
+                               throw new BindingException( e );\r
+                       } catch (InvocationTargetException e) {\r
+                               throw new BindingException( e );\r
+                       }\r
+               }\r
+               \r
+               try {\r
+                       return constructor.newInstance((Object[]) null);\r
+               } catch (IllegalArgumentException e) {\r
+                       throw new BindingException( e );\r
+               } catch (InstantiationException e) {\r
+                       throw new BindingException( e );\r
+               } catch (IllegalAccessException e) {\r
+                       throw new BindingException( e );\r
+               } catch (InvocationTargetException e) {\r
+                       throw new BindingException( e );\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public void setComponents(Object obj, Object... value)\r
+                       throws BindingException {\r
+//             if ( value.length != 1 ) throw new BindingException("Array out of range");\r
+//             if ( obj instanceof Throwable == false ) throw new BindingException("not Throwable");\r
+//             Throwable t = (Throwable) obj;\r
+               throw new BindingException("Cannot set message to Throwable");\r
+       }\r
+\r
+       @Override\r
+       public void setComponent(Object obj, int index, Object value)\r
+                       throws BindingException {\r
+               throw new BindingException("Cannot set message to Throwable");\r
+       }\r
+\r
+       @Override\r
+       public boolean isInstance(Object obj) {\r
+               return obj instanceof Throwable;\r
+       }\r
+\r
+       @Override\r
+       protected boolean baseEquals(Object obj) {\r
+               ThrowableBinding o = (ThrowableBinding)obj;\r
+               return super.baseEquals(obj) && o.boundClass.equals(boundClass);\r
+       }\r
+       \r
+       @Override\r
+       public int baseHashCode() {\r
+               return super.baseHashCode() + 13 * boundClass.hashCode();\r
+       }\r
+}\r