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.beans.PropertyDescriptor;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import java.util.Map;
  21. import org.apache.commons.collections.FastHashMap;
  22. /**
  23. * <p>Utility methods for using Java Reflection APIs to facilitate generic
  24. * property getter and setter operations on Java objects.</p>
  25. *
  26. * <p>The implementations for these methods are provided by <code>PropertyUtilsBean</code>.
  27. * For more details see {@link PropertyUtilsBean}.</p>
  28. *
  29. * @author Craig R. McClanahan
  30. * @author Ralph Schaer
  31. * @author Chris Audley
  32. * @author Rey François
  33. * @author Gregor Raıman
  34. * @author Jan Sorensen
  35. * @author Scott Sanders
  36. * @version $Revision: 1.42.2.1 $ $Date: 2004/07/27 21:31:00 $
  37. * @see PropertyUtilsBean
  38. */
  39. public class PropertyUtils {
  40. // ----------------------------------------------------- Manifest Constants
  41. /**
  42. * The delimiter that preceeds the zero-relative subscript for an
  43. * indexed reference.
  44. */
  45. public static final char INDEXED_DELIM = '[';
  46. /**
  47. * The delimiter that follows the zero-relative subscript for an
  48. * indexed reference.
  49. */
  50. public static final char INDEXED_DELIM2 = ']';
  51. /**
  52. * The delimiter that preceeds the key of a mapped property.
  53. */
  54. public static final char MAPPED_DELIM = '(';
  55. /**
  56. * The delimiter that follows the key of a mapped property.
  57. */
  58. public static final char MAPPED_DELIM2 = ')';
  59. /**
  60. * The delimiter that separates the components of a nested reference.
  61. */
  62. public static final char NESTED_DELIM = '.';
  63. // ------------------------------------------------------- Static Variables
  64. /**
  65. * The debugging detail level for this component.
  66. * @deprecated The <code>debug</code> static property is no longer used
  67. */
  68. private static int debug = 0;
  69. /**
  70. * @deprecated The <code>debug</code> static property is no longer used
  71. */
  72. public static int getDebug() {
  73. return (debug);
  74. }
  75. /**
  76. * @deprecated The <code>debug</code> static property is no longer used
  77. */
  78. public static void setDebug(int newDebug) {
  79. debug = newDebug;
  80. }
  81. // --------------------------------------------------------- Public Methods
  82. /**
  83. * Clear any cached property descriptors information for all classes
  84. * loaded by any class loaders. This is useful in cases where class
  85. * loaders are thrown away to implement class reloading.
  86. *
  87. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  88. *
  89. * @see PropertyUtilsBean#clearDescriptors
  90. */
  91. public static void clearDescriptors() {
  92. PropertyUtilsBean.getInstance().clearDescriptors();
  93. }
  94. /**
  95. * <p>Copy property values from the "origin" bean to the "destination" bean
  96. * for all cases where the property names are the same (even though the
  97. * actual getter and setter methods might have been customized via
  98. * <code>BeanInfo</code> classes).</p>
  99. *
  100. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  101. *
  102. * @see PropertyUtilsBean#copyProperties
  103. */
  104. public static void copyProperties(Object dest, Object orig)
  105. throws IllegalAccessException, InvocationTargetException,
  106. NoSuchMethodException {
  107. PropertyUtilsBean.getInstance().copyProperties(dest, orig);
  108. }
  109. /**
  110. * <p>Return the entire set of properties for which the specified bean
  111. * provides a read method.</p>
  112. *
  113. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  114. *
  115. * @see PropertyUtilsBean#describe
  116. */
  117. public static Map describe(Object bean)
  118. throws IllegalAccessException, InvocationTargetException,
  119. NoSuchMethodException {
  120. return (PropertyUtilsBean.getInstance().describe(bean));
  121. }
  122. /**
  123. * <p>Return the value of the specified indexed property of the specified
  124. * bean, with no type conversions.</p>
  125. *
  126. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  127. *
  128. * @see PropertyUtilsBean#getIndexedProperty(Object,String)
  129. */
  130. public static Object getIndexedProperty(Object bean, String name)
  131. throws IllegalAccessException, InvocationTargetException,
  132. NoSuchMethodException {
  133. return (PropertyUtilsBean.getInstance().getIndexedProperty(bean, name));
  134. }
  135. /**
  136. * <p>Return the value of the specified indexed property of the specified
  137. * bean, with no type conversions.</p>
  138. *
  139. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  140. *
  141. * @see PropertyUtilsBean#getIndexedProperty(Object,String, int)
  142. */
  143. public static Object getIndexedProperty(Object bean,
  144. String name, int index)
  145. throws IllegalAccessException, InvocationTargetException,
  146. NoSuchMethodException {
  147. return (PropertyUtilsBean.getInstance().getIndexedProperty(bean, name, index));
  148. }
  149. /**
  150. * <p>Return the value of the specified mapped property of the
  151. * specified bean, with no type conversions.</p>
  152. *
  153. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  154. *
  155. * @see PropertyUtilsBean#getMappedProperty(Object,String)
  156. */
  157. public static Object getMappedProperty(Object bean, String name)
  158. throws IllegalAccessException, InvocationTargetException,
  159. NoSuchMethodException {
  160. return (PropertyUtilsBean.getInstance().getMappedProperty(bean, name));
  161. }
  162. /**
  163. * <p>Return the value of the specified mapped property of the specified
  164. * bean, with no type conversions.</p>
  165. *
  166. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  167. *
  168. * @see PropertyUtilsBean#getMappedProperty(Object,String, String)
  169. */
  170. public static Object getMappedProperty(Object bean,
  171. String name, String key)
  172. throws IllegalAccessException, InvocationTargetException,
  173. NoSuchMethodException {
  174. return PropertyUtilsBean.getInstance().getMappedProperty(bean, name, key);
  175. }
  176. /**
  177. * <p>Return the mapped property descriptors for this bean class.</p>
  178. *
  179. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  180. *
  181. * @see PropertyUtilsBean#getMappedPropertyDescriptors(Class)
  182. * @deprecated This method should not be exposed
  183. */
  184. public static FastHashMap getMappedPropertyDescriptors(Class beanClass) {
  185. return PropertyUtilsBean.getInstance().getMappedPropertyDescriptors(beanClass);
  186. }
  187. /**
  188. * <p>Return the mapped property descriptors for this bean.</p>
  189. *
  190. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  191. *
  192. * @see PropertyUtilsBean#getMappedPropertyDescriptors(Object)
  193. * @deprecated This method should not be exposed
  194. */
  195. public static FastHashMap getMappedPropertyDescriptors(Object bean) {
  196. return PropertyUtilsBean.getInstance().getMappedPropertyDescriptors(bean);
  197. }
  198. /**
  199. * <p>Return the value of the (possibly nested) property of the specified
  200. * name, for the specified bean, with no type conversions.</p>
  201. *
  202. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  203. *
  204. * @see PropertyUtilsBean#getNestedProperty
  205. */
  206. public static Object getNestedProperty(Object bean, String name)
  207. throws IllegalAccessException, InvocationTargetException,
  208. NoSuchMethodException {
  209. return PropertyUtilsBean.getInstance().getNestedProperty(bean, name);
  210. }
  211. /**
  212. * <p>Return the value of the specified property of the specified bean,
  213. * no matter which property reference format is used, with no
  214. * type conversions.</p>
  215. *
  216. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  217. *
  218. * @see PropertyUtilsBean#getProperty
  219. */
  220. public static Object getProperty(Object bean, String name)
  221. throws IllegalAccessException, InvocationTargetException,
  222. NoSuchMethodException {
  223. return (PropertyUtilsBean.getInstance().getProperty(bean, name));
  224. }
  225. /**
  226. * <p>Retrieve the property descriptor for the specified property of the
  227. * specified bean, or return <code>null</code> if there is no such
  228. * descriptor.</p>
  229. *
  230. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  231. *
  232. * @see PropertyUtilsBean#getPropertyDescriptor
  233. */
  234. public static PropertyDescriptor getPropertyDescriptor(Object bean,
  235. String name)
  236. throws IllegalAccessException, InvocationTargetException,
  237. NoSuchMethodException {
  238. return PropertyUtilsBean.getInstance().getPropertyDescriptor(bean, name);
  239. }
  240. /**
  241. * <p>Retrieve the property descriptors for the specified class,
  242. * introspecting and caching them the first time a particular bean class
  243. * is encountered.</p>
  244. *
  245. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  246. *
  247. * @see PropertyUtilsBean#getPropertyDescriptors(Class)
  248. */
  249. public static PropertyDescriptor[]
  250. getPropertyDescriptors(Class beanClass) {
  251. return PropertyUtilsBean.getInstance().getPropertyDescriptors(beanClass);
  252. }
  253. /**
  254. * <p>Retrieve the property descriptors for the specified bean,
  255. * introspecting and caching them the first time a particular bean class
  256. * is encountered.</p>
  257. *
  258. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  259. *
  260. * @see PropertyUtilsBean#getPropertyDescriptors(Object)
  261. */
  262. public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
  263. return PropertyUtilsBean.getInstance().getPropertyDescriptors(bean);
  264. }
  265. /**
  266. * <p>Return the Java Class repesenting the property editor class that has
  267. * been registered for this property (if any).</p>
  268. *
  269. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  270. *
  271. * @see PropertyUtilsBean#getPropertyEditorClass(Object,String)
  272. */
  273. public static Class getPropertyEditorClass(Object bean, String name)
  274. throws IllegalAccessException, InvocationTargetException,
  275. NoSuchMethodException {
  276. return PropertyUtilsBean.getInstance().getPropertyEditorClass(bean, name);
  277. }
  278. /**
  279. * <p>Return the Java Class representing the property type of the specified
  280. * property, or <code>null</code> if there is no such property for the
  281. * specified bean.</p>
  282. *
  283. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  284. *
  285. * @see PropertyUtilsBean#getPropertyType
  286. */
  287. public static Class getPropertyType(Object bean, String name)
  288. throws IllegalAccessException, InvocationTargetException,
  289. NoSuchMethodException {
  290. return PropertyUtilsBean.getInstance().getPropertyType(bean, name);
  291. }
  292. /**
  293. * <p>Return an accessible property getter method for this property,
  294. * if there is one; otherwise return <code>null</code>.</p>
  295. *
  296. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  297. *
  298. * @see PropertyUtilsBean#getReadMethod
  299. */
  300. public static Method getReadMethod(PropertyDescriptor descriptor) {
  301. return (PropertyUtilsBean.getInstance().getReadMethod(descriptor));
  302. }
  303. /**
  304. * <p>Return the value of the specified simple property of the specified
  305. * bean, with no type conversions.</p>
  306. *
  307. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  308. *
  309. * @see PropertyUtilsBean#getSimpleProperty
  310. */
  311. public static Object getSimpleProperty(Object bean, String name)
  312. throws IllegalAccessException, InvocationTargetException,
  313. NoSuchMethodException {
  314. return PropertyUtilsBean.getInstance().getSimpleProperty(bean, name);
  315. }
  316. /**
  317. * <p>Return an accessible property setter method for this property,
  318. * if there is one; otherwise return <code>null</code>.</p>
  319. *
  320. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  321. *
  322. * @see PropertyUtilsBean#getWriteMethod
  323. */
  324. public static Method getWriteMethod(PropertyDescriptor descriptor) {
  325. return PropertyUtilsBean.getInstance().getWriteMethod(descriptor);
  326. }
  327. /**
  328. * <p>Return <code>true</code> if the specified property name identifies
  329. * a readable property on the specified bean; otherwise, return
  330. * <code>false</code>.</p>
  331. *
  332. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  333. *
  334. * @see PropertyUtilsBean#isReadable
  335. * @since BeanUtils 1.6
  336. */
  337. public static boolean isReadable(Object bean, String name) {
  338. return PropertyUtilsBean.getInstance().isReadable(bean, name);
  339. }
  340. /**
  341. * <p>Return <code>true</code> if the specified property name identifies
  342. * a writeable property on the specified bean; otherwise, return
  343. * <code>false</code>.</p>
  344. *
  345. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  346. *
  347. * @see PropertyUtilsBean#isWriteable
  348. * @since BeanUtils 1.6
  349. */
  350. public static boolean isWriteable(Object bean, String name) {
  351. return PropertyUtilsBean.getInstance().isWriteable(bean, name);
  352. }
  353. /**
  354. * <p>Sets the value of the specified indexed property of the specified
  355. * bean, with no type conversions.</p>
  356. *
  357. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  358. *
  359. * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
  360. */
  361. public static void setIndexedProperty(Object bean, String name,
  362. Object value)
  363. throws IllegalAccessException, InvocationTargetException,
  364. NoSuchMethodException {
  365. PropertyUtilsBean.getInstance().setIndexedProperty(bean, name, value);
  366. }
  367. /**
  368. * <p>Sets the value of the specified indexed property of the specified
  369. * bean, with no type conversions.</p>
  370. *
  371. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  372. *
  373. * @see PropertyUtilsBean#setIndexedProperty(Object, String, Object)
  374. */
  375. public static void setIndexedProperty(Object bean, String name,
  376. int index, Object value)
  377. throws IllegalAccessException, InvocationTargetException,
  378. NoSuchMethodException {
  379. PropertyUtilsBean.getInstance().setIndexedProperty(bean, name, index, value);
  380. }
  381. /**
  382. * <p>Sets the value of the specified mapped property of the
  383. * specified bean, with no type conversions.</p>
  384. *
  385. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  386. *
  387. * @see PropertyUtilsBean#setMappedProperty(Object, String, Object)
  388. */
  389. public static void setMappedProperty(Object bean, String name,
  390. Object value)
  391. throws IllegalAccessException, InvocationTargetException,
  392. NoSuchMethodException {
  393. PropertyUtilsBean.getInstance().setMappedProperty(bean, name, value);
  394. }
  395. /**
  396. * <p>Sets the value of the specified mapped property of the specified
  397. * bean, with no type conversions.</p>
  398. *
  399. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  400. *
  401. * @see PropertyUtilsBean#setMappedProperty(Object, String, String, Object)
  402. */
  403. public static void setMappedProperty(Object bean, String name,
  404. String key, Object value)
  405. throws IllegalAccessException, InvocationTargetException,
  406. NoSuchMethodException {
  407. PropertyUtilsBean.getInstance().setMappedProperty(bean, name, key, value);
  408. }
  409. /**
  410. * <p>Sets the value of the (possibly nested) property of the specified
  411. * name, for the specified bean, with no type conversions.</p>
  412. *
  413. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  414. *
  415. * @see PropertyUtilsBean#setNestedProperty
  416. */
  417. public static void setNestedProperty(Object bean,
  418. String name, Object value)
  419. throws IllegalAccessException, InvocationTargetException,
  420. NoSuchMethodException {
  421. PropertyUtilsBean.getInstance().setNestedProperty(bean, name, value);
  422. }
  423. /**
  424. * <p>Set the value of the specified property of the specified bean,
  425. * no matter which property reference format is used, with no
  426. * type conversions.</p>
  427. *
  428. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  429. *
  430. * @see PropertyUtilsBean#setProperty
  431. */
  432. public static void setProperty(Object bean, String name, Object value)
  433. throws IllegalAccessException, InvocationTargetException,
  434. NoSuchMethodException {
  435. PropertyUtilsBean.getInstance().setProperty(bean, name, value);
  436. }
  437. /**
  438. * <p>Set the value of the specified simple property of the specified bean,
  439. * with no type conversions.</p>
  440. *
  441. * <p>For more details see <code>PropertyUtilsBean</code>.</p>
  442. *
  443. * @see PropertyUtilsBean#setSimpleProperty
  444. */
  445. public static void setSimpleProperty(Object bean,
  446. String name, Object value)
  447. throws IllegalAccessException, InvocationTargetException,
  448. NoSuchMethodException {
  449. PropertyUtilsBean.getInstance().setSimpleProperty(bean, name, value);
  450. }
  451. }