1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20 import java.lang.reflect.InvocationTargetException;
21
22 /***
23 * <p>Implementation of <code>DynaBean</code> that wraps a standard JavaBean
24 * instance, so that DynaBean APIs can be used to access its properties,
25 * though this implementation allows type conversion to occur when properties are set.
26 * This means that (say) Strings can be passed in as values in setter methods and
27 * this DynaBean will convert them to the correct primitive data types.</p>
28 *
29 * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation does not
30 * support the <code>contains()</code> and <code>remove()</code> methods.</p>
31 *
32 * @author James Strachan
33 * @version $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
34 */
35
36 public class ConvertingWrapDynaBean extends WrapDynaBean {
37
38
39
40 /***
41 * Construct a new <code>DynaBean</code> associated with the specified
42 * JavaBean instance.
43 *
44 * @param instance JavaBean instance to be wrapped
45 */
46 public ConvertingWrapDynaBean(Object instance) {
47
48 super(instance);
49
50 }
51
52
53 /***
54 * Set the value of the property with the specified name
55 * performing any type conversions if necessary. So this method
56 * can accept String values for primitive numeric data types for example.
57 *
58 * @param name Name of the property whose value is to be set
59 * @param value Value to which this property is to be set
60 *
61 * @exception IllegalArgumentException if there are any problems
62 * copying the property.
63 */
64 public void set(String name, Object value) {
65
66 try {
67 BeanUtils.copyProperty(instance, name, value);
68 } catch (InvocationTargetException ite) {
69 Throwable cause = ite.getTargetException();
70 throw new IllegalArgumentException
71 ("Error setting property '" + name +
72 "' nested exception - " + cause);
73 } catch (Throwable t) {
74 throw new IllegalArgumentException
75 ("Error setting property '" + name +
76 "', exception - " + t);
77 }
78
79 }
80 }