1. /*
  2. * Copyright 2002-2004 the original author or authors.
  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.springframework.beans;
  17. import java.beans.PropertyDescriptor;
  18. import java.beans.PropertyEditor;
  19. import java.util.Map;
  20. /**
  21. * The central interface of Spring's low-level JavaBeans infrastructure.
  22. * Typically not directly used by application code but rather implicitly
  23. * via a BeanFactory or a DataBinder.
  24. *
  25. * <p>To be implemented by classes that can manipulate Java beans.
  26. * Implementing classes have the ability to get and set property values
  27. * (individually or in bulk), get property descriptors and query the
  28. * readability and writability of properties.
  29. *
  30. * <p>This interface supports <b>nested properties</b> enabling the setting
  31. * of properties on subproperties to an unlimited depth.
  32. *
  33. * <p>If a property update causes an exception, a PropertyVetoException will be
  34. * thrown. Bulk updates continue after exceptions are encountered, throwing an
  35. * exception wrapping <b>all</b> exceptions encountered during the update.
  36. *
  37. * <p>BeanWrapper implementations can be used repeatedly, with their "target"
  38. * or wrapped object changed.
  39. *
  40. * @author Rod Johnson
  41. * @since 13 April 2001
  42. * @version $Id: BeanWrapper.java,v 1.15 2004/06/03 00:08:26 jhoeller Exp $
  43. * @see org.springframework.beans.factory.BeanFactory
  44. * @see org.springframework.validation.DataBinder
  45. */
  46. public interface BeanWrapper {
  47. /**
  48. * Path separator for nested properties.
  49. * Follows normal Java conventions: getFoo().getBar() would be "foo.bar".
  50. */
  51. String NESTED_PROPERTY_SEPARATOR = ".";
  52. /**
  53. * Marker that indicates the start of a property key for an
  54. * indexed or mapped property like "person.addresses[0]".
  55. */
  56. String PROPERTY_KEY_PREFIX = "[";
  57. /**
  58. * Marker that indicates the end of a property key for an
  59. * indexed or mapped property like "person.addresses[0]".
  60. */
  61. String PROPERTY_KEY_SUFFIX = "]";
  62. /**
  63. * Change the wrapped object. Implementations are required
  64. * to allow the type of the wrapped object to change.
  65. * @param obj wrapped object that we are manipulating
  66. */
  67. void setWrappedInstance(Object obj);
  68. /**
  69. * Return the bean wrapped by this object (cannot be null).
  70. * @return the bean wrapped by this object
  71. */
  72. Object getWrappedInstance();
  73. /**
  74. * Convenience method to return the class of the wrapped object.
  75. * @return the class of the wrapped object
  76. */
  77. Class getWrappedClass();
  78. /**
  79. * Register the given custom property editor for all properties of the
  80. * given type.
  81. * @param requiredType type of the property
  82. * @param propertyEditor editor to register
  83. */
  84. void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor);
  85. /**
  86. * Register the given custom property editor for the given type and
  87. * property, or for all properties of the given type.
  88. * @param requiredType type of the property, can be null if a property is
  89. * given but should be specified in any case for consistency checking
  90. * @param propertyPath path of the property (name or nested path), or
  91. * null if registering an editor for all properties of the given type
  92. * @param propertyEditor editor to register
  93. */
  94. void registerCustomEditor(Class requiredType, String propertyPath, PropertyEditor propertyEditor);
  95. /**
  96. * Find a custom property editor for the given type and property.
  97. * @param requiredType type of the property, can be null if a property is
  98. * given but should be specified in any case for consistency checking
  99. * @param propertyPath path of the property (name or nested path), or
  100. * null if looking for an editor for all properties of the given type
  101. * @return the registered editor, or null if none
  102. */
  103. PropertyEditor findCustomEditor(Class requiredType, String propertyPath);
  104. /**
  105. * Get the value of a property.
  106. * @param propertyName name of the property to get the value of
  107. * @return the value of the property.
  108. * @throws FatalBeanException if there is no such property, if the property
  109. * isn't readable, or if the property getter throws an exception.
  110. */
  111. Object getPropertyValue(String propertyName) throws BeansException;
  112. /**
  113. * Set a property value. This method is provided for convenience only.
  114. * The setPropertyValue(PropertyValue) method is more powerful.
  115. * @param propertyName name of the property to set value of
  116. * @param value the new value
  117. */
  118. void setPropertyValue(String propertyName, Object value) throws BeansException;
  119. /**
  120. * Update a property value.
  121. * <b>This is the preferred way to update an individual property.</b>
  122. * @param pv object containing new property value
  123. */
  124. void setPropertyValue(PropertyValue pv) throws BeansException;
  125. /**
  126. * Perform a bulk update from a Map.
  127. * <p>Bulk updates from PropertyValues are more powerful: This method is
  128. * provided for convenience. Behaviour will be identical to that of
  129. * the setPropertyValues(PropertyValues) method.
  130. * @param map Map to take properties from. Contains property value objects,
  131. * keyed by property name
  132. */
  133. void setPropertyValues(Map map) throws BeansException;
  134. /**
  135. * The preferred way to perform a bulk update.
  136. * <p>Note that performing a bulk update differs from performing a single update,
  137. * in that an implementation of this class will continue to update properties
  138. * if a <b>recoverable</b> error (such as a vetoed property change or a type mismatch,
  139. * but <b>not</b> an invalid fieldname or the like) is encountered, throwing a
  140. * PropertyAccessExceptionsException containing all the individual errors.
  141. * This exception can be examined later to see all binding errors.
  142. * Properties that were successfully updated stay changed.
  143. * <p>Does not allow unknown fields.
  144. * Equivalent to setPropertyValues(pvs, false, null).
  145. * @param pvs PropertyValues to set on the target object
  146. */
  147. void setPropertyValues(PropertyValues pvs) throws BeansException;
  148. /**
  149. * Perform a bulk update with full control over behavior.
  150. * Note that performing a bulk update differs from performing a single update,
  151. * in that an implementation of this class will continue to update properties
  152. * if a <b>recoverable</b> error (such as a vetoed property change or a type mismatch,
  153. * but <b>not</b> an invalid fieldname or the like) is encountered, throwing a
  154. * PropertyAccessExceptionsException containing all the individual errors.
  155. * This exception can be examined later to see all binding errors.
  156. * Properties that were successfully updated stay changed.
  157. * <p>Does not allow unknown fields.
  158. * @param pvs PropertyValues to set on the target object
  159. * @param ignoreUnknown should we ignore unknown values (not found in the bean)
  160. */
  161. void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown)
  162. throws BeansException;
  163. /**
  164. * Get the PropertyDescriptors identified on this object
  165. * (standard JavaBeans introspection).
  166. * @return the PropertyDescriptors identified on this object
  167. */
  168. PropertyDescriptor[] getPropertyDescriptors() throws BeansException;
  169. /**
  170. * Get the property descriptor for a particular property.
  171. * @param propertyName property to check status for
  172. * @return the property descriptor for the particular property
  173. * @throws InvalidPropertyException if there is no such property
  174. */
  175. PropertyDescriptor getPropertyDescriptor(String propertyName) throws BeansException;
  176. /**
  177. * Determine the property type for a particular property, either checking
  178. * the property descriptor or checking the value in case of an indexed or
  179. * mapped element.
  180. * @param propertyName property to check status for
  181. * @return the property type for the particular property, or null if not
  182. * determinable (can only happen with an indexed or mapped element)
  183. * @throws InvalidPropertyException if there is no such property
  184. */
  185. Class getPropertyType(String propertyName) throws BeansException;
  186. /**
  187. * Return whether this property is readable.
  188. * Returns false if the property doesn't exist.
  189. * @param propertyName property to check status for
  190. * @return whether this property is readable
  191. */
  192. boolean isReadableProperty(String propertyName) throws BeansException;
  193. /**
  194. * Return whether this property is writable.
  195. * Returns false if the property doesn't exist.
  196. * @param propertyName property to check status for
  197. * @return whether this property is writable
  198. */
  199. boolean isWritableProperty(String propertyName) throws BeansException;
  200. }