1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.beanutils;
20
21
22 import java.io.Serializable;
23 import java.lang.reflect.Array;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class BasicDynaBean implements DynaBean, Serializable {
44
45
46
47
48
49
50
51
52
53
54
55 public BasicDynaBean(final DynaClass dynaClass) {
56
57 super();
58 this.dynaClass = dynaClass;
59
60 }
61
62
63
64
65
66
67
68
69
70 protected DynaClass dynaClass = null;
71
72
73
74
75
76 protected HashMap<String, Object> values = new HashMap<String, Object>();
77
78
79 private transient Map<String, Object> mapDecorator;
80
81
82
83
84
85
86
87
88
89
90
91 public Map<String, Object> getMap() {
92
93
94 if (mapDecorator == null) {
95 mapDecorator = new DynaBeanPropertyMapDecorator(this);
96 }
97 return mapDecorator;
98
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public boolean contains(final String name, final String key) {
117
118 final Object value = values.get(name);
119 if (value == null) {
120 throw new NullPointerException
121 ("No mapped value for '" + name + "(" + key + ")'");
122 } else if (value instanceof Map) {
123 return (((Map<?, ?>) value).containsKey(key));
124 } else {
125 throw new IllegalArgumentException
126 ("Non-mapped property for '" + name + "(" + key + ")'");
127 }
128
129 }
130
131
132
133
134
135
136
137
138
139
140
141 public Object get(final String name) {
142
143
144 final Object value = values.get(name);
145 if (value != null) {
146 return (value);
147 }
148
149
150 final Class<?> type = getDynaProperty(name).getType();
151 if (!type.isPrimitive()) {
152 return (value);
153 }
154
155
156 if (type == Boolean.TYPE) {
157 return (Boolean.FALSE);
158 } else if (type == Byte.TYPE) {
159 return (new Byte((byte) 0));
160 } else if (type == Character.TYPE) {
161 return (new Character((char) 0));
162 } else if (type == Double.TYPE) {
163 return (new Double(0.0));
164 } else if (type == Float.TYPE) {
165 return (new Float((float) 0.0));
166 } else if (type == Integer.TYPE) {
167 return (new Integer(0));
168 } else if (type == Long.TYPE) {
169 return (new Long(0));
170 } else if (type == Short.TYPE) {
171 return (new Short((short) 0));
172 } else {
173 return (null);
174 }
175
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public Object get(final String name, final int index) {
196
197 final Object value = values.get(name);
198 if (value == null) {
199 throw new NullPointerException
200 ("No indexed value for '" + name + "[" + index + "]'");
201 } else if (value.getClass().isArray()) {
202 return (Array.get(value, index));
203 } else if (value instanceof List) {
204 return ((List<?>) value).get(index);
205 } else {
206 throw new IllegalArgumentException
207 ("Non-indexed property for '" + name + "[" + index + "]'");
208 }
209
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 public Object get(final String name, final String key) {
227
228 final Object value = values.get(name);
229 if (value == null) {
230 throw new NullPointerException
231 ("No mapped value for '" + name + "(" + key + ")'");
232 } else if (value instanceof Map) {
233 return (((Map<?, ?>) value).get(key));
234 } else {
235 throw new IllegalArgumentException
236 ("Non-mapped property for '" + name + "(" + key + ")'");
237 }
238
239 }
240
241
242
243
244
245
246
247
248 public DynaClass getDynaClass() {
249
250 return (this.dynaClass);
251
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265
266 public void remove(final String name, final String key) {
267
268 final Object value = values.get(name);
269 if (value == null) {
270 throw new NullPointerException
271 ("No mapped value for '" + name + "(" + key + ")'");
272 } else if (value instanceof Map) {
273 ((Map<?, ?>) value).remove(key);
274 } else {
275 throw new IllegalArgumentException
276 ("Non-mapped property for '" + name + "(" + key + ")'");
277 }
278
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295 public void set(final String name, final Object value) {
296
297 final DynaProperty descriptor = getDynaProperty(name);
298 if (value == null) {
299 if (descriptor.getType().isPrimitive()) {
300 throw new NullPointerException
301 ("Primitive value for '" + name + "'");
302 }
303 } else if (!isAssignable(descriptor.getType(), value.getClass())) {
304 throw new ConversionException
305 ("Cannot assign value of type '" +
306 value.getClass().getName() +
307 "' to property '" + name + "' of type '" +
308 descriptor.getType().getName() + "'");
309 }
310 values.put(name, value);
311
312 }
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331 public void set(final String name, final int index, final Object value) {
332
333 final Object prop = values.get(name);
334 if (prop == null) {
335 throw new NullPointerException
336 ("No indexed value for '" + name + "[" + index + "]'");
337 } else if (prop.getClass().isArray()) {
338 Array.set(prop, index, value);
339 } else if (prop instanceof List) {
340 try {
341 @SuppressWarnings("unchecked")
342 final
343
344
345 List<Object> list = (List<Object>) prop;
346 list.set(index, value);
347 } catch (final ClassCastException e) {
348 throw new ConversionException(e.getMessage());
349 }
350 } else {
351 throw new IllegalArgumentException
352 ("Non-indexed property for '" + name + "[" + index + "]'");
353 }
354
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372 public void set(final String name, final String key, final Object value) {
373
374 final Object prop = values.get(name);
375 if (prop == null) {
376 throw new NullPointerException
377 ("No mapped value for '" + name + "(" + key + ")'");
378 } else if (prop instanceof Map) {
379 @SuppressWarnings("unchecked")
380 final
381
382
383 Map<String, Object> map = (Map<String, Object>) prop;
384 map.put(key, value);
385 } else {
386 throw new IllegalArgumentException
387 ("Non-mapped property for '" + name + "(" + key + ")'");
388 }
389
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 protected DynaProperty getDynaProperty(final String name) {
406
407 final DynaProperty descriptor = getDynaClass().getDynaProperty(name);
408 if (descriptor == null) {
409 throw new IllegalArgumentException
410 ("Invalid property name '" + name + "'");
411 }
412 return (descriptor);
413
414 }
415
416
417
418
419
420
421
422
423
424
425 protected boolean isAssignable(final Class<?> dest, final Class<?> source) {
426
427 if (dest.isAssignableFrom(source) ||
428 ((dest == Boolean.TYPE) && (source == Boolean.class)) ||
429 ((dest == Byte.TYPE) && (source == Byte.class)) ||
430 ((dest == Character.TYPE) && (source == Character.class)) ||
431 ((dest == Double.TYPE) && (source == Double.class)) ||
432 ((dest == Float.TYPE) && (source == Float.class)) ||
433 ((dest == Integer.TYPE) && (source == Integer.class)) ||
434 ((dest == Long.TYPE) && (source == Long.class)) ||
435 ((dest == Short.TYPE) && (source == Short.class))) {
436 return (true);
437 } else {
438 return (false);
439 }
440
441 }
442
443
444 }