    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.beanutils2;
    019
    020import java.lang.reflect.InvocationTargetException;
    021
    022/**
    023 * <p>
    024 * Implements {@link DynaBean} to wrap a standard JavaBean instance, so that DynaBean APIs can be used to access its properties, though this implementation
    025 * 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
    026 * convert them to the correct primitive data types.
    027 * </p>
    028 *
    029 * <p>
    030 * <strong>IMPLEMENTATION NOTE</strong> - This implementation does not support the {@code contains()</code> and <code>remove()} methods.
    031 * </p>
    032 */
    033public class ConvertingWrapDynaBean extends WrapDynaBean {
    034
    035    private static final long serialVersionUID = 1L;
    036
    037    /**
    038     * Constructs a new {@code DynaBean} associated with the specified JavaBean instance.
    039     *
    040     * @param instance JavaBean instance to be wrapped
    041     */
    042    public ConvertingWrapDynaBean(final Object instance) {
    043        super(instance);
    044    }
    045
    046    /**
    047     * Sets the value of the property with the specified name performing any type conversions if necessary. So this method can accept String values for
    048     * primitive numeric data types for example.
    049     *
    050     * @param name  Name of the property whose value is to be set
    051     * @param value Value to which this property is to be set
    052     * @throws IllegalArgumentException if there are any problems copying the property.
    053     */
    054    @Override
    055    public void set(final String name, final Object value) {
    056        try {
    057            BeanUtils.copyProperty(instance, name, value);
    058        } catch (final InvocationTargetException ite) {
    059            final Throwable cause = ite.getTargetException();
    060            throw new IllegalArgumentException("Error setting property '" + name + "' nested exception - " + cause);
    061        } catch (final Throwable t) {
    062            throw new IllegalArgumentException("Error setting property '" + name + "', exception - " + t, t);
    063        }
    064
    065    }
    066}