ConvertingWrapDynaBean.java

  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. package org.apache.commons.beanutils2;

  18. import java.lang.reflect.InvocationTargetException;

  19. /**
  20.  * <p>
  21.  * Implements {@link DynaBean} to wrap a standard JavaBean instance, so that DynaBean APIs can be used to access its properties, though this implementation
  22.  * allows type conversion to occur when properties are set. This means that (say) Strings can be passed in as values in setter methods and this DynaBean will
  23.  * convert them to the correct primitive data types.
  24.  * </p>
  25.  *
  26.  * <p>
  27.  * <strong>IMPLEMENTATION NOTE</strong> - This implementation does not support the {@code contains()</code> and <code>remove()} methods.
  28.  * </p>
  29.  */
  30. public class ConvertingWrapDynaBean extends WrapDynaBean {

  31.     private static final long serialVersionUID = 1L;

  32.     /**
  33.      * Constructs a new {@code DynaBean} associated with the specified JavaBean instance.
  34.      *
  35.      * @param instance JavaBean instance to be wrapped
  36.      */
  37.     public ConvertingWrapDynaBean(final Object instance) {
  38.         super(instance);
  39.     }

  40.     /**
  41.      * Sets the value of the property with the specified name performing any type conversions if necessary. So this method can accept String values for
  42.      * primitive numeric data types for example.
  43.      *
  44.      * @param name  Name of the property whose value is to be set
  45.      * @param value Value to which this property is to be set
  46.      * @throws IllegalArgumentException if there are any problems copying the property.
  47.      */
  48.     @Override
  49.     public void set(final String name, final Object value) {
  50.         try {
  51.             BeanUtils.copyProperty(instance, name, value);
  52.         } catch (final InvocationTargetException ite) {
  53.             final Throwable cause = ite.getTargetException();
  54.             throw new IllegalArgumentException("Error setting property '" + name + "' nested exception - " + cause);
  55.         } catch (final Throwable t) {
  56.             throw new IllegalArgumentException("Error setting property '" + name + "', exception - " + t, t);
  57.         }

  58.     }
  59. }