1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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.databoard.binding.impl;
14 import java.lang.reflect.Constructor;
15 import java.lang.reflect.InvocationTargetException;
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;
27 * This class Binds throwables to the following record:
28 * { detailMessage : Optional(String) }
30 * The bound throwable class must have constructor( String ).
32 * @author toni.kalajainen
34 public class ThrowableBinding extends RecordBinding {
36 private Class<Throwable> boundClass;
37 private Constructor<Throwable> constructor;
38 private Constructor<Throwable> constructorNoArgs;
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 );
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 );
57 constructorNoArgs = boundClass.getConstructor();
58 constructorNoArgs.setAccessible( true );
59 } catch (SecurityException e) {
60 } catch (NoSuchMethodException e) {
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();
74 public Object create(Object... values) throws BindingException {
75 if (values.length!=1) throw new BindingException("Invalid argument");
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() );
90 public Object createPartial() throws BindingException {
91 if ( constructorNoArgs!=null ) {
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() );
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() );
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");
128 public void setComponent(Object obj, int index, Object value)
129 throws BindingException {
130 throw new BindingException("Cannot set message to Throwable");
134 public boolean isInstance(Object obj) {
135 return obj instanceof Throwable;
139 protected boolean baseEquals(Object obj) {
140 ThrowableBinding o = (ThrowableBinding)obj;
141 return super.baseEquals(obj) && o.boundClass.equals(boundClass);
145 public int baseHashCode() {
146 return super.baseHashCode() + 13 * boundClass.hashCode();