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   * @version $Id$
33   */
34  
35  public class ConvertingWrapDynaBean extends WrapDynaBean {
36  
37  
38  
39      /**
40       * Construct a new <code>DynaBean</code> associated with the specified
41       * JavaBean instance.
42       *
43       * @param instance JavaBean instance to be wrapped
44       */
45      public ConvertingWrapDynaBean(final Object instance) {
46  
47          super(instance);
48  
49      }
50  
51  
52      /**
53       * Set the value of the property with the specified name
54       * performing any type conversions if necessary. So this method
55       * can accept String values for primitive numeric data types for example.
56       *
57       * @param name Name of the property whose value is to be set
58       * @param value Value to which this property is to be set
59       *
60       * @throws IllegalArgumentException if there are any problems
61       *            copying the property.
62       */
63      @Override
64      public void set(final String name, final Object value) {
65  
66          try {
67              BeanUtils.copyProperty(instance, name, value);
68          } catch (final InvocationTargetException ite) {
69              final Throwable cause = ite.getTargetException();
70              throw new IllegalArgumentException
71                      ("Error setting property '" + name +
72                                "' nested exception - " + cause);
73          } catch (final Throwable t) {
74              final IllegalArgumentException iae = new IllegalArgumentException
75                      ("Error setting property '" + name +
76                                "', exception - " + t);
77              BeanUtils.initCause(iae, t);
78              throw iae;
79          }
80  
81      }
82  }