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