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 * https://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 18 package org.apache.commons.beanutils2; 19 20 import java.lang.reflect.InvocationTargetException; 21 22 /** 23 * <p> 24 * Implements {@link DynaBean} to wrap a standard JavaBean instance, so that DynaBean APIs can be used to access its properties, though this implementation 25 * 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 26 * convert them to the correct primitive data types. 27 * </p> 28 * 29 * <p> 30 * <strong>IMPLEMENTATION NOTE</strong> - This implementation does not support the {@code contains()</code> and <code>remove()} methods. 31 * </p> 32 */ 33 public class ConvertingWrapDynaBean extends WrapDynaBean { 34 35 private static final long serialVersionUID = 1L; 36 37 /** 38 * Constructs a new {@code DynaBean} associated with the specified JavaBean instance. 39 * 40 * @param instance JavaBean instance to be wrapped 41 */ 42 public ConvertingWrapDynaBean(final Object instance) { 43 super(instance); 44 } 45 46 /** 47 * Sets the value of the property with the specified name performing any type conversions if necessary. So this method can accept String values for 48 * primitive numeric data types for example. 49 * 50 * @param name Name of the property whose value is to be set 51 * @param value Value to which this property is to be set 52 * @throws IllegalArgumentException if there are any problems copying the property. 53 */ 54 @Override 55 public void set(final String name, final Object value) { 56 try { 57 BeanUtils.copyProperty(instance, name, value); 58 } catch (final InvocationTargetException ite) { 59 final Throwable cause = ite.getTargetException(); 60 throw new IllegalArgumentException("Error setting property '" + name + "' nested exception - " + cause); 61 } catch (final Throwable t) { 62 throw new IllegalArgumentException("Error setting property '" + name + "', exception - " + t, t); 63 } 64 65 } 66 }