X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Freflection%2FRecordClassBinding.java;h=cf6bcba195f3f1c9fdad4dc7cba527bb4e9dfd0a;hb=refs%2Fchanges%2F38%2F238%2F2;hp=9bbb257cb8a42bdd5d00f958caf96d3f4a4c2077;hpb=75785e1454fbc60973848c72e2ed698d2cda5ce7;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/RecordClassBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/RecordClassBinding.java index 9bbb257cb..cf6bcba19 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/RecordClassBinding.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/reflection/RecordClassBinding.java @@ -1,32 +1,32 @@ -/******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.databoard.binding.reflection; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.lang.reflect.Type; - -import org.simantics.databoard.binding.Binding; -import org.simantics.databoard.binding.BooleanBinding; -import org.simantics.databoard.binding.ByteBinding; -import org.simantics.databoard.binding.DoubleBinding; -import org.simantics.databoard.binding.FloatBinding; -import org.simantics.databoard.binding.IntegerBinding; -import org.simantics.databoard.binding.LongBinding; -import org.simantics.databoard.binding.error.BindingConstructionException; -import org.simantics.databoard.binding.error.BindingException; -import org.simantics.databoard.type.RecordType; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.lang.reflect.Type; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.BooleanBinding; +import org.simantics.databoard.binding.ByteBinding; +import org.simantics.databoard.binding.DoubleBinding; +import org.simantics.databoard.binding.FloatBinding; +import org.simantics.databoard.binding.IntegerBinding; +import org.simantics.databoard.binding.LongBinding; +import org.simantics.databoard.binding.error.BindingConstructionException; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.type.RecordType; /** * ReflectionRecordBinding binding of a Record Type and Java Class. @@ -45,9 +45,9 @@ import org.simantics.databoard.type.RecordType; * - All fields are set with getter/setter */ class RecordClassBinding extends ClassBinding { - - private static final Object[] NO_ARGS = new Object[0]; - + + private static final Object[] NO_ARGS = new Object[0]; + /** * Create a Reflection Record Bindings. * @@ -58,28 +58,28 @@ class RecordClassBinding extends ClassBinding { * @throws BindingConstructionException */ public RecordClassBinding(ClassInfo ci, RecordType type) - { - super(ci); - if ( ci == null ) throw new IllegalArgumentException("null ClassInfo"); - this.type = type; - componentBindings = new Binding[ ci.fields.length ]; - } - + { + super(ci); + if ( ci == null ) throw new IllegalArgumentException("null ClassInfo"); + this.type = type; + componentBindings = new Binding[ ci.fields.length ]; + } + @Override public Object createPartial() throws BindingException { try { if (ci.noArgsConstructor!=null) return ci.noArgsConstructor.newInstance(); - else if (ci.argsConstructor!=null) { - - // Create default values - Object values[] = new Object[ci.fields.length]; - for (int i=0; i cl = f.getType(); - if ( cl==boolean.class || cl==Boolean.class ) { - f.setBoolean(r, z); - } else { - BooleanBinding b = (BooleanBinding) getComponentBinding(index); - setComponent(r, index, b.create(z)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public boolean getBoolean(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==boolean.class || cl==Boolean.class ) return f.getBoolean(r); - Object o = getComponent(r, index); - BooleanBinding b = (BooleanBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public void setByte(Object r, int index, byte x) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f.getType(); - if ( cl==byte.class || cl==Byte.class ) { - f.setByte(r, x); - } else { - ByteBinding b = (ByteBinding) getComponentBinding(index); - setComponent(r, index, b.create(x)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public byte getByte(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==byte.class || cl==Byte.class ) return f.getByte(r); - Object o = getComponent(r, index); - ByteBinding b = (ByteBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public void setInt(Object r, int index, int x) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f.getType(); - if ( cl==int.class || cl==Integer.class ) { - f.setInt(r, x); - } else { - IntegerBinding b = (IntegerBinding) getComponentBinding(index); - setComponent(r, index, b.create(x)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public int getInt(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==int.class || cl==Integer.class ) return f.getInt(r); - Object o = getComponent(r, index); - IntegerBinding b = (IntegerBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public void setLong(Object r, int index, long x) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f.getType(); - if ( cl==long.class || cl==Long.class ) { - f.setLong(r, x); - } else { - LongBinding b = (LongBinding) getComponentBinding(index); - setComponent(r, index, b.create(x)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public long getLong(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==long.class || cl==Long.class ) return f.getLong(r); - Object o = getComponent(r, index); - LongBinding b = (LongBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public void setFloat(Object r, int index, float x) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f.getType(); - if ( cl==float.class || cl==Float.class ) { - f.setFloat(r, x); - } else { - FloatBinding b = (FloatBinding) getComponentBinding(index); - setComponent(r, index, b.create(x)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public float getFloat(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==float.class || cl==Float.class ) return f.getFloat(r); - Object o = getComponent(r, index); - FloatBinding b = (FloatBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public void setDouble(Object r, int index, double x) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f.getType(); - if ( cl==double.class || cl==Double.class ) { - f.setDouble(r, x); - } else { - DoubleBinding b = (DoubleBinding) getComponentBinding(index); - setComponent(r, index, b.create(x)); - } - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - - public double getDouble(Object r, int index) throws BindingException - { - try { - Field f = ci.fields[index]; - Class cl = f==null?null:f.getType(); - if ( cl==double.class || cl==Double.class ) return f.getDouble(r); - Object o = getComponent(r, index); - DoubleBinding b = (DoubleBinding) getComponentBinding(index); - return b.getValue_(o); - } catch (IllegalArgumentException e) { - throw new BindingException(e); - } catch (IllegalAccessException e) { - throw new BindingException(e); - } - } - + } + + public void setBoolean(Object r, int index, boolean z) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==boolean.class || cl==Boolean.class ) { + f.setBoolean(r, z); + } else { + BooleanBinding b = (BooleanBinding) getComponentBinding(index); + setComponent(r, index, b.create(z)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public boolean getBoolean(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==boolean.class || cl==Boolean.class ) return f.getBoolean(r); + Object o = getComponent(r, index); + BooleanBinding b = (BooleanBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public void setByte(Object r, int index, byte x) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==byte.class || cl==Byte.class ) { + f.setByte(r, x); + } else { + ByteBinding b = (ByteBinding) getComponentBinding(index); + setComponent(r, index, b.create(x)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public byte getByte(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==byte.class || cl==Byte.class ) return f.getByte(r); + Object o = getComponent(r, index); + ByteBinding b = (ByteBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public void setInt(Object r, int index, int x) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==int.class || cl==Integer.class ) { + f.setInt(r, x); + } else { + IntegerBinding b = (IntegerBinding) getComponentBinding(index); + setComponent(r, index, b.create(x)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public int getInt(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==int.class || cl==Integer.class ) return f.getInt(r); + Object o = getComponent(r, index); + IntegerBinding b = (IntegerBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public void setLong(Object r, int index, long x) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==long.class || cl==Long.class ) { + f.setLong(r, x); + } else { + LongBinding b = (LongBinding) getComponentBinding(index); + setComponent(r, index, b.create(x)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public long getLong(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==long.class || cl==Long.class ) return f.getLong(r); + Object o = getComponent(r, index); + LongBinding b = (LongBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public void setFloat(Object r, int index, float x) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==float.class || cl==Float.class ) { + f.setFloat(r, x); + } else { + FloatBinding b = (FloatBinding) getComponentBinding(index); + setComponent(r, index, b.create(x)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public float getFloat(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==float.class || cl==Float.class ) return f.getFloat(r); + Object o = getComponent(r, index); + FloatBinding b = (FloatBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public void setDouble(Object r, int index, double x) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f.getType(); + if ( cl==double.class || cl==Double.class ) { + f.setDouble(r, x); + } else { + DoubleBinding b = (DoubleBinding) getComponentBinding(index); + setComponent(r, index, b.create(x)); + } + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + + public double getDouble(Object r, int index) throws BindingException + { + try { + Field f = ci.fields[index]; + Class cl = f==null?null:f.getType(); + if ( cl==double.class || cl==Double.class ) return f.getDouble(r); + Object o = getComponent(r, index); + DoubleBinding b = (DoubleBinding) getComponentBinding(index); + return b.getValue_(o); + } catch (IllegalArgumentException e) { + throw new BindingException(e); + } catch (IllegalAccessException e) { + throw new BindingException(e); + } + } + }