01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
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: * Construct a new <code>DynaBean</code> associated with the specified
40: * JavaBean instance.
41: *
42: * @param instance JavaBean instance to be wrapped
43: */
44: public ConvertingWrapDynaBean(Object instance) {
45:
46: super (instance);
47:
48: }
49:
50: /**
51: * Set the value of the property with the specified name
52: * performing any type conversions if necessary. So this method
53: * can accept String values for primitive numeric data types for example.
54: *
55: * @param name Name of the property whose value is to be set
56: * @param value Value to which this property is to be set
57: *
58: * @exception IllegalArgumentException if there are any problems
59: * copying the property.
60: */
61: public void set(String name, Object value) {
62:
63: try {
64: BeanUtils.copyProperty(instance, name, value);
65: } catch (InvocationTargetException ite) {
66: Throwable cause = ite.getTargetException();
67: throw new IllegalArgumentException(
68: "Error setting property '" + name
69: + "' nested exception - " + cause);
70: } catch (Throwable t) {
71: throw new IllegalArgumentException(
72: "Error setting property '" + name
73: + "', exception - " + t);
74: }
75:
76: }
77: }
|