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