--- /dev/null
+/*******************************************************************************\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.reflection;\r
+\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.FloatBinding;\r
+import org.simantics.databoard.binding.LongBinding;\r
+import org.simantics.databoard.binding.error.BindingConstructionException;\r
+import org.simantics.databoard.binding.error.BindingException;\r
+import org.simantics.databoard.type.NumberType;\r
+import org.simantics.databoard.type.RecordType;\r
+\r
+public class MyBinding extends ClassBinding {\r
+ \r
+ public MyBinding(RecordType type) throws BindingConstructionException {\r
+ super( ClassInfo.getInfo(MyClass.class) );\r
+ this.type = type;\r
+ }\r
+\r
+// @Override\r
+ public Object getComponent(Object obj, int index) throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ switch ( index ) {\r
+ case 0: return x.field0;\r
+ case 1: return x.field1;\r
+ case 2: return x.field2;\r
+ case 3: return x.field3;\r
+ case 4: return x.field4;\r
+ case 5: return x.field5;\r
+ case 6: return x.field6;\r
+ case 7: return x.field7;\r
+ case 8: return x.field8;\r
+ case 9: return x.field9;\r
+ case 10: return x.field10;\r
+ case 11: return x.field11;\r
+ case 12: return x.field12;\r
+ case 13: return x.field13;\r
+ case 14: return x.field14;\r
+ case 15: return x.field15;\r
+ case 16: return x.field16;\r
+ default: throw new BindingException("Illegal field index");\r
+ }\r
+ }\r
+\r
+ public Object getComponent2(Object obj, int index) throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ switch ( index ) {\r
+ case 0: return x.getField0();\r
+ case 1: return x.getField1();\r
+ case 2: return x.getField2();\r
+ case 3: return x.getField3();\r
+ case 4: return x.getField4();\r
+ case 5: return x.getField5();\r
+ case 6: return x.getField6();\r
+ case 7: return x.getField7();\r
+ case 8: return x.getField8();\r
+ case 9: return x.getField9();\r
+ case 10: return x.getField10();\r
+ case 11: return x.getField11();\r
+ case 12: return x.getField12();\r
+ case 13: return x.getField13();\r
+ case 14: return x.getField14();\r
+ case 15: return x.getField15();\r
+ case 16: return x.getField16();\r
+ default: throw new BindingException("Illegal field index");\r
+ }\r
+ }\r
+ \r
+// @Override\r
+ public Object create(Object... values) throws BindingException {\r
+// return new MyClass(\r
+// (Byte) values[0],\r
+// (Integer) values[1],\r
+// (Short) values[2],\r
+// (Long) values[3],\r
+// (Float) values[4],\r
+// (Boolean) values[5],\r
+// (Double) values[6],\r
+// (NumberType) values[7],\r
+// (byte[]) values[8],\r
+// (int[]) values[9],\r
+// (short[]) values[10],\r
+// (long[]) values[11],\r
+// (float[]) values[12],\r
+// (boolean[]) values[13],\r
+// (double[]) values[14],\r
+// values[15],\r
+// (Integer) values[16]\r
+// );\r
+ return null;\r
+ }\r
+ \r
+ public Object create2(Object... values) throws BindingException {\r
+ MyClass x = new MyClass(this);\r
+ setComponents(x, values);\r
+ return x;\r
+ } \r
+ \r
+// @Override\r
+ public Object createPartial() throws BindingException {\r
+ return new MyClass(this);\r
+ }\r
+\r
+ public Object createPartial2() throws BindingException {\r
+ Object values[] = new Object[15];\r
+ for (int i=0; i<values.length; i++) {\r
+ Binding fb = componentBindings[i];\r
+ values[i] = fb.createDefault();\r
+ } \r
+ return create(values);\r
+ }\r
+ \r
+// @Override\r
+ public void setComponents(Object obj, Object... value)\r
+ throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ // 0: Write field\r
+ x.field0 = (Byte) value[0];\r
+ x.field1 = (Integer) value[1];\r
+ x.field2 = (Short) value[2];\r
+ x.field3 = (Long) value[3];\r
+ x.field4 = (Float) value[4];\r
+ x.field5 = (Boolean) value[5];\r
+ x.field6 = (Double) value[6];\r
+ x.field7 = (NumberType) value[7];\r
+ x.field8 = (byte[]) value[8];\r
+ x.field9 = (int[]) value[9];\r
+ x.field10 = (short[]) value[10];\r
+ x.field11 = (long[]) value[11];\r
+ x.field12 = (float[]) value[12];\r
+ x.field13 = (boolean[]) value[13];\r
+ x.field14 = (double[]) value[14];\r
+ x.field15 = value[15];\r
+ x.field16 = (Integer) value[16];\r
+ }\r
+\r
+ public void setComponents2(Object obj, Object... value)\r
+ throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ // Set with setters\r
+ x.setField0((Byte) value[0]);\r
+ x.setField1((Integer) value[1]);\r
+ x.setField2((Short) value[2]);\r
+ x.setField3((Long) value[3]);\r
+ x.setField4((Float) value[4]);\r
+ x.setField5((Boolean) value[5]);\r
+ x.setField6((Double) value[6]);\r
+ x.setField7((NumberType) value[7]);\r
+ x.setField8((byte[]) value[8]);\r
+ x.setField9((int[]) value[9]);\r
+ x.setField10((short[]) value[10]);\r
+ x.setField11((long[]) value[11]);\r
+ x.setField12((float[]) value[12]);\r
+ x.setField13((boolean[]) value[13]);\r
+ x.setField14((double[]) value[14]);\r
+ x.setField15(value[15]);\r
+ x.setField16((Integer)value[16]);\r
+ }\r
+\r
+// @Override\r
+ public void setComponent(Object obj, int index, Object value)\r
+ throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ switch ( index ) {\r
+ case 0: x.field0 = (Byte) value;\r
+ case 1: x.field1 = (Integer) value;\r
+ case 2: x.field2 = (Short) value;\r
+ case 3: x.field3 = (Long) value;\r
+ case 4: x.field4 = (Float) value;\r
+ case 5: x.field5 = (Boolean) value;\r
+ case 6: x.field6 = (Double) value;\r
+ case 7: x.field7 = (NumberType) value;\r
+ case 8: x.field8 = (byte[]) value;\r
+ case 9: x.field9 = (int[]) value;\r
+ case 10: x.field10 = (short[]) value;\r
+ case 11: x.field11 = (long[]) value;\r
+ case 12: x.field12 = (float[]) value;\r
+ case 13: x.field13 = (boolean[]) value;\r
+ case 14: x.field14 = (double[]) value;\r
+ case 15: x.field15 = value;\r
+ default: throw new BindingException("Illegal field index"); \r
+ }\r
+ }\r
+\r
+ public void setComponent2(Object obj, int index, Object value)\r
+ throws BindingException {\r
+ MyClass x = (MyClass) obj;\r
+ switch ( index ) {\r
+ case 0: x.setField0( (Byte) value );\r
+ case 1: x.setField1( (Integer) value );\r
+ case 2: x.setField2( (Short) value );\r
+ case 3: x.setField3( (Long) value );\r
+ case 4: x.setField4( (Float) value );\r
+ case 5: x.setField5( (Boolean) value );\r
+ case 6: x.setField6( (Double) value );\r
+ case 7: x.setField7( (NumberType) value );\r
+ case 8: x.setField8( (byte[]) value );\r
+ case 9: x.setField9( (int[]) value );\r
+ case 10: x.setField10( (short[]) value );\r
+ case 11: x.setField11( (long[]) value );\r
+ case 12: x.setField12( (float[]) value );\r
+ case 13: x.setField13( (boolean[]) value );\r
+ case 14: x.setField14( (double[]) value );\r
+ case 15: x.setField15( value );\r
+ default: throw new BindingException("Illegal field index"); \r
+ }\r
+ }\r
+\r
+// @Override\r
+ public boolean isInstance(Object obj) {\r
+ return obj instanceof MyClass;\r
+ }\r
+ \r
+// @Override\r
+ public boolean isImmutable() {\r
+ return false;\r
+ }\r
+\r
+ public void setBoolean(Object r, int index, boolean z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 5: x.field5 = z; break;\r
+ case 19: x.field19 = z; break;\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+\r
+ public void setBoolean2(Object r, int index, boolean z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 5: x.field5 = z; break;\r
+ case 19: x.field19 = z; break;\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+\r
+ public void setBoolean3(Object r, int index, boolean z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ if ( index==5 ) x.field5 = z; \r
+ else if ( index==19 ) x.field19 = z; \r
+ //default: throw new BindingException("Field is not boolean"); \r
+ \r
+ }\r
+ \r
+ public boolean getBoolean(Object r, int index) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 5: return x.field5;\r
+ case 19: return x.getField5();\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+ \r
+ public void setByte(Object r, int index, byte z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 6: x.field6 = z; break;\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+ \r
+ public byte getByte(Object r, int index) throws BindingException\r
+ {\r
+ try {\r
+ return ci.fields[index].getByte(r);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new BindingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ }\r
+\r
+ public void setInt(Object r, int index, int x) throws BindingException\r
+ {\r
+ try {\r
+ ci.fields[index].setInt(r, x);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new BindingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ }\r
+ \r
+ public int getInt(Object r, int index) throws BindingException\r
+ {\r
+ try {\r
+ return ci.fields[index].getInt(r);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new BindingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ }\r
+ \r
+ public void setLong(Object r, int index, long z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 6: x.field3 = z; break;\r
+ case 8: x.setField3(z); break;\r
+ default: setComponent(r, index, ((LongBinding)componentBindings[index]).create(z)); \r
+ }\r
+ }\r
+ \r
+ public long getLong(Object r, int index) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 3: return x.field3;\r
+ case 4: return x.field3;\r
+ default: return ((LongBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
+ }\r
+ }\r
+ \r
+ public void setFloat(Object r, int index, float z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+// case 6: x.field4 = z; break;\r
+ //case 8: x.; break;\r
+ default: setComponent(r, index, ((FloatBinding)componentBindings[index]).create(z)); \r
+ }\r
+ }\r
+ \r
+ public float getFloat(Object r, int index) throws BindingException\r
+ {\r
+ try {\r
+ return ci.fields[index].getFloat(r);\r
+ } catch (IllegalArgumentException e) {\r
+ throw new BindingException(e);\r
+ } catch (IllegalAccessException e) {\r
+ throw new BindingException(e);\r
+ }\r
+ }\r
+ \r
+ public void setDouble(Object r, int index, double z) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+ case 6: x.field6 = z; break;\r
+ //case 8: x.; break;\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+ \r
+ public double getDouble(Object r, int index) throws BindingException\r
+ {\r
+ MyClass x = (MyClass) r;\r
+ switch ( index ) {\r
+// case 3: return x.field6;\r
+ case 4: return x.getField6_();\r
+ case 5: return x.field16;\r
+ default: throw new BindingException("Field is not boolean"); \r
+ }\r
+ }\r
+ \r
+ \r
+}\r