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 018 package org.apache.commons.beanutils; 019 020 import java.lang.reflect.InvocationTargetException; 021 022 /** 023 * <p>Implementation of <code>DynaBean</code> that wraps 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 * @author James Strachan 033 * @version $Revision: 640259 $ $Date: 2008-03-23 21:31:25 +0000 (Sun, 23 Mar 2008) $ 034 */ 035 036 public class ConvertingWrapDynaBean extends WrapDynaBean { 037 038 039 040 /** 041 * Construct a new <code>DynaBean</code> associated with the specified 042 * JavaBean instance. 043 * 044 * @param instance JavaBean instance to be wrapped 045 */ 046 public ConvertingWrapDynaBean(Object instance) { 047 048 super(instance); 049 050 } 051 052 053 /** 054 * Set the value of the property with the specified name 055 * performing any type conversions if necessary. So this method 056 * can accept String values for primitive numeric data types for example. 057 * 058 * @param name Name of the property whose value is to be set 059 * @param value Value to which this property is to be set 060 * 061 * @exception IllegalArgumentException if there are any problems 062 * copying the property. 063 */ 064 public void set(String name, Object value) { 065 066 try { 067 BeanUtils.copyProperty(instance, name, value); 068 } catch (InvocationTargetException ite) { 069 Throwable cause = ite.getTargetException(); 070 throw new IllegalArgumentException 071 ("Error setting property '" + name + 072 "' nested exception - " + cause); 073 } catch (Throwable t) { 074 IllegalArgumentException iae = new IllegalArgumentException 075 ("Error setting property '" + name + 076 "', exception - " + t); 077 BeanUtils.initCause(iae, t); 078 throw iae; 079 } 080 081 } 082 }