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.beanutils; 019 020import java.lang.reflect.InvocationTargetException; 021 022/** 023 * <p>Implements <code>DynaBean</code> to wrap a standard JavaBean 024 * instance, so that DynaBean APIs can be used to access its properties, 025 * though this implementation allows type conversion to occur when properties are set. 026 * This means that (say) Strings can be passed in as values in setter methods and 027 * this DynaBean will convert them to the correct primitive data types.</p> 028 * 029 * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation does not 030 * support the <code>contains()</code> and <code>remove()</code> methods.</p> 031 * 032 */ 033 034public class ConvertingWrapDynaBean extends WrapDynaBean { 035 036 private static final long serialVersionUID = 1L; 037 038 /** 039 * Construct a new <code>DynaBean</code> associated with the specified 040 * JavaBean instance. 041 * 042 * @param instance JavaBean instance to be wrapped 043 */ 044 public ConvertingWrapDynaBean(final Object instance) { 045 046 super(instance); 047 048 } 049 050 /** 051 * Set the value of the property with the specified name 052 * performing any type conversions if necessary. So this method 053 * can accept String values for primitive numeric data types for example. 054 * 055 * @param name Name of the property whose value is to be set 056 * @param value Value to which this property is to be set 057 * @throws IllegalArgumentException if there are any problems 058 * copying the property. 059 */ 060 @Override 061 public void set(final String name, final Object value) { 062 try { 063 BeanUtils.copyProperty(instance, name, value); 064 } catch (final InvocationTargetException ite) { 065 final Throwable cause = ite.getTargetException(); 066 throw new IllegalArgumentException("Error setting property '" + name + "' nested exception - " + cause); 067 } catch (final Throwable t) { 068 throw new IllegalArgumentException("Error setting property '" + name + "', exception - " + t, t); 069 } 070 } 071}