01: /*
02: * Created on Jun 17, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.lang.reflect.Field;
07: import net.kildenpedersen.reflect.Reflector;
08:
09: /**
10: * @author Nils Kilden-Pedersen
11: */
12: final class SetField extends ResultObjectMember implements Setter {
13:
14: private static Field findField(Class type, String fieldName) {
15: Field field = Reflector.getAccessibleField(type, fieldName);
16: if (field != null) {
17: return field;
18: }
19: throw new ConfigurationException("Unknown field '" + fieldName
20: + "' for " + type.toString());
21: }
22:
23: private ValueType valueType;
24:
25: /**
26: * Constructor.
27: * @param resultObjectType
28: * @param fieldName
29: * @param valueType
30: */
31: SetField(Class resultObjectType, String fieldName) {
32: super (findField(resultObjectType, fieldName));
33: }
34:
35: private Field getField() {
36: return (Field) getMember();
37: }
38:
39: /**
40: * Returns the field type.
41: * @return field type
42: */
43: Class getFieldType() {
44: return getField().getType();
45: }
46:
47: /**
48: * @param valueType Set valueType.
49: */
50: void setValueType(ValueType valueType) {
51: this .valueType = valueType;
52: }
53:
54: /**
55: * Set valueType
56: * @param resultObject
57: * @param row
58: */
59: public final void setValue(Object resultObject, ResultRow row) {
60: Field setField = getField();
61: Object parmValue = this .valueType.getValue(row);
62: try {
63: setField.set(resultObject, parmValue);
64: } catch (IllegalAccessException e) {
65: String msg = "Cannot set field '" + setField.getName()
66: + "' on " + resultObject.getClass();
67: throw new ReflectionException(msg, e);
68: } catch (RuntimeException e) {
69: if (setField.getType().isPrimitive() && parmValue == null) {
70: String msg = "Cannot assign 'null' to primitive field '"
71: + setField.getName()
72: + "' in class '"
73: + Reflector.getClassName(setField
74: .getDeclaringClass()) + "'";
75: throw new ReflectionException(msg, e);
76: }
77: throw e;
78: }
79: }
80: }
|