1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.beanutils;
  17. /**
  18. * <p>Implementation of <code>DynaBean</code> that wraps a standard JavaBean
  19. * instance, so that DynaBean APIs can be used to access its properties,
  20. * though this implementation allows type conversion to occur when properties are set.
  21. * This means that (say) Strings can be passed in as values in setter methods and
  22. * this DynaBean will convert them to the correct primitive data types.</p>
  23. *
  24. * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation does not
  25. * support the <code>contains()</code> and <code>remove()</code> methods.</p>
  26. *
  27. * @author James Strachan
  28. * @version $Revision: 1.3 $ $Date: 2002/01/23 22:35:58 $
  29. */
  30. public class ConvertingWrapDynaBean extends WrapDynaBean {
  31. /**
  32. * Construct a new <code>DynaBean</code> associated with the specified
  33. * JavaBean instance.
  34. *
  35. * @param instance JavaBean instance to be wrapped
  36. */
  37. public ConvertingWrapDynaBean(Object instance) {
  38. super(instance);
  39. }
  40. /**
  41. * Set the value of the property with the specified name
  42. * performing any type conversions if necessary. So this method
  43. * can accept String values for primitive numeric data types for example.
  44. *
  45. * @param name Name of the property whose value is to be set
  46. * @param value Value to which this property is to be set
  47. *
  48. * @exception ConversionException if the specified value cannot be
  49. * converted to the type required for this property
  50. * @exception IllegalArgumentException if there is no property
  51. * of the specified name
  52. * @exception NullPointerException if an attempt is made to set a
  53. * primitive property to null
  54. */
  55. public void set(String name, Object value) {
  56. try {
  57. BeanUtils.copyProperty(instance, name, value);
  58. } catch (Throwable t) {
  59. throw new IllegalArgumentException
  60. ("Property '" + name + "' has no write method");
  61. }
  62. }
  63. }