01: package simpleorm.simplewebapp.core;
02:
03: import java.lang.reflect.Method;
04: import java.util.Collection;
05:
06: /**
07: * Very simple get/set properties etc.
08: * Can be used in examples, reflection not a core requirement of SimpleWebApp!
09: * Not particularly efficient, no caching of method objects etc.
10: * (No need for the complexity of java.beans or commons bean utils.) <p>
11: *
12: */
13: public class WBeanUtils {
14:
15: /**
16: * Updates fields.values with bean properties in bean.
17: * fields parameters is normally <code>getCrudFields().getValues()</code>
18: * Propeties are methods of form getXxx() where Xxx is the field name.
19: * Note silently ignores fields that do not have properties.
20: */
21: public static void retrieveBeanProperties(Object bean,
22: Collection<WField> fields) {
23: for (WField field : fields) {
24: try {
25: retrieveBeanProperty(bean, field);
26: } catch (NoSuchMethodException nsm) {
27: } catch (Exception ex) {
28: throw new WException("While retrieving " + field
29: + " from " + bean, ex);
30: }
31: }
32: }
33:
34: /**
35: * Copies fields.values into bean properties.
36: * @see #retrieveBeanProperties
37: */
38: public static void updateBeanProperties(Object bean,
39: Collection<WField> fields) {
40: for (WField field : fields) {
41: try {
42: updateBeanProperty(bean, field);
43: } catch (NoSuchMethodException nsm) {
44: } catch (Exception ex) {
45: throw new WException(ex);
46: }
47: }
48: }
49:
50: /** set this field's value from the Java bean (ie. setValue(bean.getName()).
51: * Support for getBeanProperties.
52: * Note that there is no real point in calling this directly for an individual property,
53: * better just to explicitly call its getter.
54: */
55: static void retrieveBeanProperty(Object bean, WField field)
56: throws Exception {
57: field.setValue(WBeanUtils.getPropertyValue(bean, field
58: .getDataName()));
59: }
60:
61: /** set the Java bean's property to this field's value (ie. bean.setName(getValue())). */
62: static void updateBeanProperty(Object bean, WField field)
63: throws Exception {
64: WBeanUtils.setPropertyValue(bean, field.getDataName(), field
65: .getValue(), field.getValueClass());
66: }
67:
68: /** get(foo, "bar") returns foo.getBar() */
69: static Object getPropertyValue(Object bean, String name)
70: throws Exception {
71: Method method = propertyMethod(bean, "get", name, null);
72: return method.invoke(bean);
73: }
74:
75: /** Does bean.setName(value).
76: * (clazz parameter is sadly necessary in case value is null.)
77: */
78: static void setPropertyValue(Object bean, String name,
79: Object value, Class clazz) throws Exception {
80: Method method = propertyMethod(bean, "set", name,
81: new Class[] { clazz });
82: method.invoke(bean, value);
83: }
84:
85: static Method propertyMethod(Object bean, String getSet,
86: String name, Class[] params) throws Exception {
87: String mname = getSet + name.substring(0, 1).toUpperCase()
88: + name.substring(1);
89: Class c = bean.getClass();
90: return c.getMethod(mname, params);
91: }
92: }
|