View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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: 926529 $ $Date: 2010-03-23 07:44:24 -0400 (Tue, 23 Mar 2010) $
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              IllegalArgumentException iae = new IllegalArgumentException
75                      ("Error setting property '" + name +
76                                "', exception - " + t);
77              BeanUtils.initCause(iae, t);
78              throw iae;
79          }
80  
81      }
82  }