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