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.reflection;
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.FloatBinding;
16 import org.simantics.databoard.binding.LongBinding;
17 import org.simantics.databoard.binding.error.BindingConstructionException;
18 import org.simantics.databoard.binding.error.BindingException;
19 import org.simantics.databoard.type.NumberType;
20 import org.simantics.databoard.type.RecordType;
22 public class MyBinding extends ClassBinding {
24 public MyBinding(RecordType type) throws BindingConstructionException {
25 super( ClassInfo.getInfo(MyClass.class) );
30 public Object getComponent(Object obj, int index) throws BindingException {
31 MyClass x = (MyClass) obj;
33 case 0: return x.field0;
34 case 1: return x.field1;
35 case 2: return x.field2;
36 case 3: return x.field3;
37 case 4: return x.field4;
38 case 5: return x.field5;
39 case 6: return x.field6;
40 case 7: return x.field7;
41 case 8: return x.field8;
42 case 9: return x.field9;
43 case 10: return x.field10;
44 case 11: return x.field11;
45 case 12: return x.field12;
46 case 13: return x.field13;
47 case 14: return x.field14;
48 case 15: return x.field15;
49 case 16: return x.field16;
50 default: throw new BindingException("Illegal field index");
54 public Object getComponent2(Object obj, int index) throws BindingException {
55 MyClass x = (MyClass) obj;
57 case 0: return x.getField0();
58 case 1: return x.getField1();
59 case 2: return x.getField2();
60 case 3: return x.getField3();
61 case 4: return x.getField4();
62 case 5: return x.getField5();
63 case 6: return x.getField6();
64 case 7: return x.getField7();
65 case 8: return x.getField8();
66 case 9: return x.getField9();
67 case 10: return x.getField10();
68 case 11: return x.getField11();
69 case 12: return x.getField12();
70 case 13: return x.getField13();
71 case 14: return x.getField14();
72 case 15: return x.getField15();
73 case 16: return x.getField16();
74 default: throw new BindingException("Illegal field index");
79 public Object create(Object... values) throws BindingException {
80 // return new MyClass(
82 // (Integer) values[1],
86 // (Boolean) values[5],
87 // (Double) values[6],
88 // (NumberType) values[7],
89 // (byte[]) values[8],
91 // (short[]) values[10],
92 // (long[]) values[11],
93 // (float[]) values[12],
94 // (boolean[]) values[13],
95 // (double[]) values[14],
97 // (Integer) values[16]
102 public Object create2(Object... values) throws BindingException {
103 MyClass x = new MyClass(this);
104 setComponents(x, values);
109 public Object createPartial() throws BindingException {
110 return new MyClass(this);
113 public Object createPartial2() throws BindingException {
114 Object values[] = new Object[15];
115 for (int i=0; i<values.length; i++) {
116 Binding fb = componentBindings[i];
117 values[i] = fb.createDefault();
119 return create(values);
123 public void setComponents(Object obj, Object... value)
124 throws BindingException {
125 MyClass x = (MyClass) obj;
127 x.field0 = (Byte) value[0];
128 x.field1 = (Integer) value[1];
129 x.field2 = (Short) value[2];
130 x.field3 = (Long) value[3];
131 x.field4 = (Float) value[4];
132 x.field5 = (Boolean) value[5];
133 x.field6 = (Double) value[6];
134 x.field7 = (NumberType) value[7];
135 x.field8 = (byte[]) value[8];
136 x.field9 = (int[]) value[9];
137 x.field10 = (short[]) value[10];
138 x.field11 = (long[]) value[11];
139 x.field12 = (float[]) value[12];
140 x.field13 = (boolean[]) value[13];
141 x.field14 = (double[]) value[14];
142 x.field15 = value[15];
143 x.field16 = (Integer) value[16];
146 public void setComponents2(Object obj, Object... value)
147 throws BindingException {
148 MyClass x = (MyClass) obj;
150 x.setField0((Byte) value[0]);
151 x.setField1((Integer) value[1]);
152 x.setField2((Short) value[2]);
153 x.setField3((Long) value[3]);
154 x.setField4((Float) value[4]);
155 x.setField5((Boolean) value[5]);
156 x.setField6((Double) value[6]);
157 x.setField7((NumberType) value[7]);
158 x.setField8((byte[]) value[8]);
159 x.setField9((int[]) value[9]);
160 x.setField10((short[]) value[10]);
161 x.setField11((long[]) value[11]);
162 x.setField12((float[]) value[12]);
163 x.setField13((boolean[]) value[13]);
164 x.setField14((double[]) value[14]);
165 x.setField15(value[15]);
166 x.setField16((Integer)value[16]);
170 public void setComponent(Object obj, int index, Object value)
171 throws BindingException {
172 MyClass x = (MyClass) obj;
174 case 0: x.field0 = (Byte) value;
175 case 1: x.field1 = (Integer) value;
176 case 2: x.field2 = (Short) value;
177 case 3: x.field3 = (Long) value;
178 case 4: x.field4 = (Float) value;
179 case 5: x.field5 = (Boolean) value;
180 case 6: x.field6 = (Double) value;
181 case 7: x.field7 = (NumberType) value;
182 case 8: x.field8 = (byte[]) value;
183 case 9: x.field9 = (int[]) value;
184 case 10: x.field10 = (short[]) value;
185 case 11: x.field11 = (long[]) value;
186 case 12: x.field12 = (float[]) value;
187 case 13: x.field13 = (boolean[]) value;
188 case 14: x.field14 = (double[]) value;
189 case 15: x.field15 = value;
190 default: throw new BindingException("Illegal field index");
194 public void setComponent2(Object obj, int index, Object value)
195 throws BindingException {
196 MyClass x = (MyClass) obj;
198 case 0: x.setField0( (Byte) value );
199 case 1: x.setField1( (Integer) value );
200 case 2: x.setField2( (Short) value );
201 case 3: x.setField3( (Long) value );
202 case 4: x.setField4( (Float) value );
203 case 5: x.setField5( (Boolean) value );
204 case 6: x.setField6( (Double) value );
205 case 7: x.setField7( (NumberType) value );
206 case 8: x.setField8( (byte[]) value );
207 case 9: x.setField9( (int[]) value );
208 case 10: x.setField10( (short[]) value );
209 case 11: x.setField11( (long[]) value );
210 case 12: x.setField12( (float[]) value );
211 case 13: x.setField13( (boolean[]) value );
212 case 14: x.setField14( (double[]) value );
213 case 15: x.setField15( value );
214 default: throw new BindingException("Illegal field index");
219 public boolean isInstance(Object obj) {
220 return obj instanceof MyClass;
224 public boolean isImmutable() {
228 public void setBoolean(Object r, int index, boolean z) throws BindingException
230 MyClass x = (MyClass) r;
232 case 5: x.field5 = z; break;
233 case 19: x.field19 = z; break;
234 default: throw new BindingException("Field is not boolean");
238 public void setBoolean2(Object r, int index, boolean z) throws BindingException
240 MyClass x = (MyClass) r;
242 case 5: x.field5 = z; break;
243 case 19: x.field19 = z; break;
244 default: throw new BindingException("Field is not boolean");
248 public void setBoolean3(Object r, int index, boolean z) throws BindingException
250 MyClass x = (MyClass) r;
251 if ( index==5 ) x.field5 = z;
252 else if ( index==19 ) x.field19 = z;
253 //default: throw new BindingException("Field is not boolean");
257 public boolean getBoolean(Object r, int index) throws BindingException
259 MyClass x = (MyClass) r;
261 case 5: return x.field5;
262 case 19: return x.getField5();
263 default: throw new BindingException("Field is not boolean");
267 public void setByte(Object r, int index, byte z) throws BindingException
269 MyClass x = (MyClass) r;
271 case 6: x.field6 = z; break;
272 default: throw new BindingException("Field is not boolean");
276 public byte getByte(Object r, int index) throws BindingException
279 return ci.fields[index].getByte(r);
280 } catch (IllegalArgumentException e) {
281 throw new BindingException(e);
282 } catch (IllegalAccessException e) {
283 throw new BindingException(e);
287 public void setInt(Object r, int index, int x) throws BindingException
290 ci.fields[index].setInt(r, x);
291 } catch (IllegalArgumentException e) {
292 throw new BindingException(e);
293 } catch (IllegalAccessException e) {
294 throw new BindingException(e);
298 public int getInt(Object r, int index) throws BindingException
301 return ci.fields[index].getInt(r);
302 } catch (IllegalArgumentException e) {
303 throw new BindingException(e);
304 } catch (IllegalAccessException e) {
305 throw new BindingException(e);
309 public void setLong(Object r, int index, long z) throws BindingException
311 MyClass x = (MyClass) r;
313 case 6: x.field3 = z; break;
314 case 8: x.setField3(z); break;
315 default: setComponent(r, index, ((LongBinding)componentBindings[index]).create(z));
319 public long getLong(Object r, int index) throws BindingException
321 MyClass x = (MyClass) r;
323 case 3: return x.field3;
324 case 4: return x.field3;
325 default: return ((LongBinding)componentBindings[index]).getValue_( getComponent(r, index) );
329 public void setFloat(Object r, int index, float z) throws BindingException
331 MyClass x = (MyClass) r;
333 // case 6: x.field4 = z; break;
335 default: setComponent(r, index, ((FloatBinding)componentBindings[index]).create(z));
339 public float getFloat(Object r, int index) throws BindingException
342 return ci.fields[index].getFloat(r);
343 } catch (IllegalArgumentException e) {
344 throw new BindingException(e);
345 } catch (IllegalAccessException e) {
346 throw new BindingException(e);
350 public void setDouble(Object r, int index, double z) throws BindingException
352 MyClass x = (MyClass) r;
354 case 6: x.field6 = z; break;
356 default: throw new BindingException("Field is not boolean");
360 public double getDouble(Object r, int index) throws BindingException
362 MyClass x = (MyClass) r;
364 // case 3: return x.field6;
365 case 4: return x.getField6_();
366 case 5: return x.field16;
367 default: throw new BindingException("Field is not boolean");