View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.commons.beanutils;
18  
19  
20  /**
21   * <p>Implementation of <code>DynaBean</code> that wraps a standard JavaBean
22   * instance, so that DynaBean APIs can be used to access its properties,
23   * though this implementation allows type conversion to occur when properties are set.
24   * This means that (say) Strings can be passed in as values in setter methods and
25   * this DynaBean will convert them to the correct primitive data types.</p>
26   *
27   * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation does not
28   * support the <code>contains()</code> and <code>remove()</code> methods.</p>
29   *
30   * @author James Strachan
31   * @version $Revision: 1.3 $ $Date: 2002/01/23 22:35:58 $
32   */
33  
34  public class ConvertingWrapDynaBean extends WrapDynaBean {
35  
36  
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      /**
52       * Set the value of the property with the specified name
53       * performing any type conversions if necessary. So this method
54       * can accept String values for primitive numeric data types for example.
55       *
56       * @param name Name of the property whose value is to be set
57       * @param value Value to which this property is to be set
58       *
59       * @exception ConversionException if the specified value cannot be
60       *  converted to the type required for this property
61       * @exception IllegalArgumentException if there is no property
62       *  of the specified name
63       * @exception NullPointerException if an attempt is made to set a
64       *  primitive property to null
65       */
66      public void set(String name, Object value) {
67  
68          try {
69              BeanUtils.copyProperty(instance, name, value);
70          } catch (Throwable t) {
71              throw new IllegalArgumentException
72                      ("Property '" + name + "' has no write method");
73          }
74  
75      }
76  }