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. import java.lang.reflect.InvocationTargetException;
  18. import java.util.Map;
  19. import org.apache.commons.collections.FastHashMap;
  20. /**
  21. * <p>Utility methods for populating JavaBeans properties via reflection.</p>
  22. *
  23. * <p>The implementations are provided by {@link BeanUtilsBean}.
  24. * These static utility methods use the default instance.
  25. * More sophisticated behaviour can be provided by using a <code>BeanUtilsBean</code> instance.</p>
  26. *
  27. * @author Craig R. McClanahan
  28. * @author Ralph Schaer
  29. * @author Chris Audley
  30. * @author Rey François
  31. * @author Gregor Raıman
  32. * @version $Revision: 1.40 $ $Date: 2004/02/28 13:18:33 $
  33. * @see BeanUtilsBean
  34. */
  35. public class BeanUtils {
  36. // ------------------------------------------------------ Private Variables
  37. /**
  38. * Dummy collection from the Commons Collections API, to force a
  39. * ClassNotFoundException if commons-collections.jar is not present in the
  40. * runtime classpath, and this class is the first one referenced.
  41. * Otherwise, the ClassNotFoundException thrown by ConvertUtils or
  42. * PropertyUtils can get masked.
  43. */
  44. private static FastHashMap dummy = new FastHashMap();
  45. /**
  46. * The debugging detail level for this component.
  47. * @deprecated BeanUtils now uses commons-logging for all log messages.
  48. * Use your favorite logging tool to configure logging for
  49. * this class.
  50. */
  51. private static int debug = 0;
  52. /**
  53. * @deprecated BeanUtils now uses commons-logging for all log messages.
  54. * Use your favorite logging tool to configure logging for
  55. * this class.
  56. */
  57. public static int getDebug() {
  58. return (debug);
  59. }
  60. /**
  61. * @deprecated BeanUtils now uses commons-logging for all log messages.
  62. * Use your favorite logging tool to configure logging for
  63. * this class.
  64. */
  65. public static void setDebug(int newDebug) {
  66. debug = newDebug;
  67. }
  68. // --------------------------------------------------------- Class Methods
  69. /**
  70. * <p>Clone a bean based on the available property getters and setters,
  71. * even if the bean class itself does not implement Cloneable.</p>
  72. *
  73. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  74. *
  75. * @see BeanUtilsBean#cloneBean
  76. */
  77. public static Object cloneBean(Object bean)
  78. throws IllegalAccessException, InstantiationException,
  79. InvocationTargetException, NoSuchMethodException {
  80. return BeanUtilsBean.getInstance().cloneBean(bean);
  81. }
  82. /**
  83. * <p>Copy property values from the origin bean to the destination bean
  84. * for all cases where the property names are the same.</p>
  85. *
  86. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  87. *
  88. * @see BeanUtilsBean#copyProperties
  89. */
  90. public static void copyProperties(Object dest, Object orig)
  91. throws IllegalAccessException, InvocationTargetException {
  92. BeanUtilsBean.getInstance().copyProperties(dest, orig);
  93. }
  94. /**
  95. * <p>Copy the specified property value to the specified destination bean,
  96. * performing any type conversion that is required.</p>
  97. *
  98. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  99. *
  100. * @see BeanUtilsBean#copyProperty
  101. */
  102. public static void copyProperty(Object bean, String name, Object value)
  103. throws IllegalAccessException, InvocationTargetException {
  104. BeanUtilsBean.getInstance().copyProperty(bean, name, value);
  105. }
  106. /**
  107. * <p>Return the entire set of properties for which the specified bean
  108. * provides a read method.</p>
  109. *
  110. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  111. *
  112. * @see BeanUtilsBean#describe
  113. */
  114. public static Map describe(Object bean)
  115. throws IllegalAccessException, InvocationTargetException,
  116. NoSuchMethodException {
  117. return BeanUtilsBean.getInstance().describe(bean);
  118. }
  119. /**
  120. * <p>Return the value of the specified array property of the specified
  121. * bean, as a String array.</p>
  122. *
  123. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  124. *
  125. * @see BeanUtilsBean#getArrayProperty
  126. */
  127. public static String[] getArrayProperty(Object bean, String name)
  128. throws IllegalAccessException, InvocationTargetException,
  129. NoSuchMethodException {
  130. return BeanUtilsBean.getInstance().getArrayProperty(bean, name);
  131. }
  132. /**
  133. * <p>Return the value of the specified indexed property of the specified
  134. * bean, as a String.</p>
  135. *
  136. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  137. *
  138. * @see BeanUtilsBean#getIndexedProperty(Object, String)
  139. */
  140. public static String getIndexedProperty(Object bean, String name)
  141. throws IllegalAccessException, InvocationTargetException,
  142. NoSuchMethodException {
  143. return BeanUtilsBean.getInstance().getIndexedProperty(bean, name);
  144. }
  145. /**
  146. * Return the value of the specified indexed property of the specified
  147. * bean, as a String. The index is specified as a method parameter and
  148. * must *not* be included in the property name expression
  149. *
  150. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  151. *
  152. * @see BeanUtilsBean#getIndexedProperty(Object, String, int)
  153. */
  154. public static String getIndexedProperty(Object bean,
  155. String name, int index)
  156. throws IllegalAccessException, InvocationTargetException,
  157. NoSuchMethodException {
  158. return BeanUtilsBean.getInstance().getIndexedProperty(bean, name, index);
  159. }
  160. /**
  161. * </p>Return the value of the specified indexed property of the specified
  162. * bean, as a String.</p>
  163. *
  164. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  165. *
  166. * @see BeanUtilsBean#getMappedProperty(Object, String)
  167. */
  168. public static String getMappedProperty(Object bean, String name)
  169. throws IllegalAccessException, InvocationTargetException,
  170. NoSuchMethodException {
  171. return BeanUtilsBean.getInstance().getMappedProperty(bean, name);
  172. }
  173. /**
  174. * </p>Return the value of the specified mapped property of the specified
  175. * bean, as a String.</p>
  176. *
  177. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  178. *
  179. * @see BeanUtilsBean#getMappedProperty(Object, String, String)
  180. */
  181. public static String getMappedProperty(Object bean,
  182. String name, String key)
  183. throws IllegalAccessException, InvocationTargetException,
  184. NoSuchMethodException {
  185. return BeanUtilsBean.getInstance().getMappedProperty(bean, name, key);
  186. }
  187. /**
  188. * <p>Return the value of the (possibly nested) property of the specified
  189. * name, for the specified bean, as a String.</p>
  190. *
  191. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  192. *
  193. * @see BeanUtilsBean#getNestedProperty
  194. */
  195. public static String getNestedProperty(Object bean, String name)
  196. throws IllegalAccessException, InvocationTargetException,
  197. NoSuchMethodException {
  198. return BeanUtilsBean.getInstance().getNestedProperty(bean, name);
  199. }
  200. /**
  201. * <p>Return the value of the specified property of the specified bean,
  202. * no matter which property reference format is used, as a String.</p>
  203. *
  204. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  205. *
  206. * @see BeanUtilsBean#getProperty
  207. */
  208. public static String getProperty(Object bean, String name)
  209. throws IllegalAccessException, InvocationTargetException,
  210. NoSuchMethodException {
  211. return BeanUtilsBean.getInstance().getProperty(bean, name);
  212. }
  213. /**
  214. * <p>Return the value of the specified simple property of the specified
  215. * bean, converted to a String.</p>
  216. *
  217. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  218. *
  219. * @see BeanUtilsBean#getSimpleProperty
  220. */
  221. public static String getSimpleProperty(Object bean, String name)
  222. throws IllegalAccessException, InvocationTargetException,
  223. NoSuchMethodException {
  224. return BeanUtilsBean.getInstance().getSimpleProperty(bean, name);
  225. }
  226. /**
  227. * <p>Populate the JavaBeans properties of the specified bean, based on
  228. * the specified name/value pairs.</p>
  229. *
  230. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  231. *
  232. * @see BeanUtilsBean#populate
  233. */
  234. public static void populate(Object bean, Map properties)
  235. throws IllegalAccessException, InvocationTargetException {
  236. BeanUtilsBean.getInstance().populate(bean, properties);
  237. }
  238. /**
  239. * <p>Set the specified property value, performing type conversions as
  240. * required to conform to the type of the destination property.</p>
  241. *
  242. * <p>For more details see <code>BeanUtilsBean</code>.</p>
  243. *
  244. * @see BeanUtilsBean#setProperty
  245. */
  246. public static void setProperty(Object bean, String name, Object value)
  247. throws IllegalAccessException, InvocationTargetException {
  248. BeanUtilsBean.getInstance().setProperty(bean, name, value);
  249. }
  250. }