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.io.Serializable;
  18. import java.lang.reflect.Array;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * <p>Minimal implementation of the <code>DynaBean</code> interface. Can be
  24. * used as a convenience base class for more sophisticated implementations.</p>
  25. *
  26. * <p><strong>IMPLEMENTATION NOTE</strong> - Instances of this class that are
  27. * accessed from multiple threads simultaneously need to be synchronized.</p>
  28. *
  29. * <p><strong>IMPLEMENTATION NOTE</strong> - Instances of this class can be
  30. * successfully serialized and deserialized <strong>ONLY</strong> if all
  31. * property values are <code>Serializable</code>.</p>
  32. *
  33. * @author Craig McClanahan
  34. * @version $Revision: 1.11 $ $Date: 2004/02/28 13:18:33 $
  35. */
  36. public class BasicDynaBean implements DynaBean, Serializable {
  37. // ---------------------------------------------------------- Constructors
  38. /**
  39. * Construct a new <code>DynaBean</code> associated with the specified
  40. * <code>DynaClass</code> instance.
  41. *
  42. * @param dynaClass The DynaClass we are associated with
  43. */
  44. public BasicDynaBean(DynaClass dynaClass) {
  45. super();
  46. this.dynaClass = dynaClass;
  47. }
  48. // ---------------------------------------------------- Instance Variables
  49. /**
  50. * The <code>DynaClass</code> "base class" that this DynaBean
  51. * is associated with.
  52. */
  53. protected DynaClass dynaClass = null;
  54. /**
  55. * The set of property values for this DynaBean, keyed by property name.
  56. */
  57. protected HashMap values = new HashMap();
  58. // ------------------------------------------------------ DynaBean Methods
  59. /**
  60. * Does the specified mapped property contain a value for the specified
  61. * key value?
  62. *
  63. * @param name Name of the property to check
  64. * @param key Name of the key to check
  65. *
  66. * @exception IllegalArgumentException if there is no property
  67. * of the specified name
  68. */
  69. public boolean contains(String name, String key) {
  70. Object value = values.get(name);
  71. if (value == null) {
  72. throw new NullPointerException
  73. ("No mapped value for '" + name + "(" + key + ")'");
  74. } else if (value instanceof Map) {
  75. return (((Map) value).containsKey(key));
  76. } else {
  77. throw new IllegalArgumentException
  78. ("Non-mapped property for '" + name + "(" + key + ")'");
  79. }
  80. }
  81. /**
  82. * Return the value of a simple property with the specified name.
  83. *
  84. * @param name Name of the property whose value is to be retrieved
  85. *
  86. * @exception IllegalArgumentException if there is no property
  87. * of the specified name
  88. */
  89. public Object get(String name) {
  90. // Return any non-null value for the specified property
  91. Object value = values.get(name);
  92. if (value != null) {
  93. return (value);
  94. }
  95. // Return a null value for a non-primitive property
  96. Class type = getDynaProperty(name).getType();
  97. if (!type.isPrimitive()) {
  98. return (value);
  99. }
  100. // Manufacture default values for primitive properties
  101. if (type == Boolean.TYPE) {
  102. return (Boolean.FALSE);
  103. } else if (type == Byte.TYPE) {
  104. return (new Byte((byte) 0));
  105. } else if (type == Character.TYPE) {
  106. return (new Character((char) 0));
  107. } else if (type == Double.TYPE) {
  108. return (new Double((double) 0.0));
  109. } else if (type == Float.TYPE) {
  110. return (new Float((float) 0.0));
  111. } else if (type == Integer.TYPE) {
  112. return (new Integer((int) 0));
  113. } else if (type == Long.TYPE) {
  114. return (new Long((int) 0));
  115. } else if (type == Short.TYPE) {
  116. return (new Short((short) 0));
  117. } else {
  118. return (null);
  119. }
  120. }
  121. /**
  122. * Return the value of an indexed property with the specified name.
  123. *
  124. * @param name Name of the property whose value is to be retrieved
  125. * @param index Index of the value to be retrieved
  126. *
  127. * @exception IllegalArgumentException if there is no property
  128. * of the specified name
  129. * @exception IllegalArgumentException if the specified property
  130. * exists, but is not indexed
  131. * @exception IndexOutOfBoundsException if the specified index
  132. * is outside the range of the underlying property
  133. * @exception NullPointerException if no array or List has been
  134. * initialized for this property
  135. */
  136. public Object get(String name, int index) {
  137. Object value = values.get(name);
  138. if (value == null) {
  139. throw new NullPointerException
  140. ("No indexed value for '" + name + "[" + index + "]'");
  141. } else if (value.getClass().isArray()) {
  142. return (Array.get(value, index));
  143. } else if (value instanceof List) {
  144. return ((List) value).get(index);
  145. } else {
  146. throw new IllegalArgumentException
  147. ("Non-indexed property for '" + name + "[" + index + "]'");
  148. }
  149. }
  150. /**
  151. * Return the value of a mapped property with the specified name,
  152. * or <code>null</code> if there is no value for the specified key.
  153. *
  154. * @param name Name of the property whose value is to be retrieved
  155. * @param key Key of the value to be retrieved
  156. *
  157. * @exception IllegalArgumentException if there is no property
  158. * of the specified name
  159. * @exception IllegalArgumentException if the specified property
  160. * exists, but is not mapped
  161. */
  162. public Object get(String name, String key) {
  163. Object value = values.get(name);
  164. if (value == null) {
  165. throw new NullPointerException
  166. ("No mapped value for '" + name + "(" + key + ")'");
  167. } else if (value instanceof Map) {
  168. return (((Map) value).get(key));
  169. } else {
  170. throw new IllegalArgumentException
  171. ("Non-mapped property for '" + name + "(" + key + ")'");
  172. }
  173. }
  174. /**
  175. * Return the <code>DynaClass</code> instance that describes the set of
  176. * properties available for this DynaBean.
  177. */
  178. public DynaClass getDynaClass() {
  179. return (this.dynaClass);
  180. }
  181. /**
  182. * Remove any existing value for the specified key on the
  183. * specified mapped property.
  184. *
  185. * @param name Name of the property for which a value is to
  186. * be removed
  187. * @param key Key of the value to be removed
  188. *
  189. * @exception IllegalArgumentException if there is no property
  190. * of the specified name
  191. */
  192. public void remove(String name, String key) {
  193. Object value = values.get(name);
  194. if (value == null) {
  195. throw new NullPointerException
  196. ("No mapped value for '" + name + "(" + key + ")'");
  197. } else if (value instanceof Map) {
  198. ((Map) value).remove(key);
  199. } else {
  200. throw new IllegalArgumentException
  201. ("Non-mapped property for '" + name + "(" + key + ")'");
  202. }
  203. }
  204. /**
  205. * Set the value of a simple property with the specified name.
  206. *
  207. * @param name Name of the property whose value is to be set
  208. * @param value Value to which this property is to be set
  209. *
  210. * @exception ConversionException if the specified value cannot be
  211. * converted to the type required for this property
  212. * @exception IllegalArgumentException if there is no property
  213. * of the specified name
  214. * @exception NullPointerException if an attempt is made to set a
  215. * primitive property to null
  216. */
  217. public void set(String name, Object value) {
  218. DynaProperty descriptor = getDynaProperty(name);
  219. if (value == null) {
  220. if (descriptor.getType().isPrimitive()) {
  221. throw new NullPointerException
  222. ("Primitive value for '" + name + "'");
  223. }
  224. } else if (!isAssignable(descriptor.getType(), value.getClass())) {
  225. throw new ConversionException
  226. ("Cannot assign value of type '" +
  227. value.getClass().getName() +
  228. "' to property '" + name + "' of type '" +
  229. descriptor.getType().getName() + "'");
  230. }
  231. values.put(name, value);
  232. }
  233. /**
  234. * Set the value of an indexed property with the specified name.
  235. *
  236. * @param name Name of the property whose value is to be set
  237. * @param index Index of the property to be set
  238. * @param value Value to which this property is to be set
  239. *
  240. * @exception ConversionException if the specified value cannot be
  241. * converted to the type required for this property
  242. * @exception IllegalArgumentException if there is no property
  243. * of the specified name
  244. * @exception IllegalArgumentException if the specified property
  245. * exists, but is not indexed
  246. * @exception IndexOutOfBoundsException if the specified index
  247. * is outside the range of the underlying property
  248. */
  249. public void set(String name, int index, Object value) {
  250. Object prop = values.get(name);
  251. if (prop == null) {
  252. throw new NullPointerException
  253. ("No indexed value for '" + name + "[" + index + "]'");
  254. } else if (prop.getClass().isArray()) {
  255. Array.set(prop, index, value);
  256. } else if (prop instanceof List) {
  257. try {
  258. ((List) prop).set(index, value);
  259. } catch (ClassCastException e) {
  260. throw new ConversionException(e.getMessage());
  261. }
  262. } else {
  263. throw new IllegalArgumentException
  264. ("Non-indexed property for '" + name + "[" + index + "]'");
  265. }
  266. }
  267. /**
  268. * Set the value of a mapped property with the specified name.
  269. *
  270. * @param name Name of the property whose value is to be set
  271. * @param key Key of the property to be set
  272. * @param value Value to which this property is to be set
  273. *
  274. * @exception ConversionException if the specified value cannot be
  275. * converted to the type required for this property
  276. * @exception IllegalArgumentException if there is no property
  277. * of the specified name
  278. * @exception IllegalArgumentException if the specified property
  279. * exists, but is not mapped
  280. */
  281. public void set(String name, String key, Object value) {
  282. Object prop = values.get(name);
  283. if (prop == null) {
  284. throw new NullPointerException
  285. ("No mapped value for '" + name + "(" + key + ")'");
  286. } else if (prop instanceof Map) {
  287. ((Map) prop).put(key, value);
  288. } else {
  289. throw new IllegalArgumentException
  290. ("Non-mapped property for '" + name + "(" + key + ")'");
  291. }
  292. }
  293. // ------------------------------------------------------ Protected Methods
  294. /**
  295. * Return the property descriptor for the specified property name.
  296. *
  297. * @param name Name of the property for which to retrieve the descriptor
  298. *
  299. * @exception IllegalArgumentException if this is not a valid property
  300. * name for our DynaClass
  301. */
  302. protected DynaProperty getDynaProperty(String name) {
  303. DynaProperty descriptor = getDynaClass().getDynaProperty(name);
  304. if (descriptor == null) {
  305. throw new IllegalArgumentException
  306. ("Invalid property name '" + name + "'");
  307. }
  308. return (descriptor);
  309. }
  310. /**
  311. * Is an object of the source class assignable to the destination class?
  312. *
  313. * @param dest Destination class
  314. * @param source Source class
  315. */
  316. protected boolean isAssignable(Class dest, Class source) {
  317. if (dest.isAssignableFrom(source) ||
  318. ((dest == Boolean.TYPE) && (source == Boolean.class)) ||
  319. ((dest == Byte.TYPE) && (source == Byte.class)) ||
  320. ((dest == Character.TYPE) && (source == Character.class)) ||
  321. ((dest == Double.TYPE) && (source == Double.class)) ||
  322. ((dest == Float.TYPE) && (source == Float.class)) ||
  323. ((dest == Integer.TYPE) && (source == Integer.class)) ||
  324. ((dest == Long.TYPE) && (source == Long.class)) ||
  325. ((dest == Short.TYPE) && (source == Short.class))) {
  326. return (true);
  327. } else {
  328. return (false);
  329. }
  330. }
  331. }