1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.io.Serializable ; 22 import java.lang.reflect.Array ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 28 42 43 public class BasicDynaBean implements DynaBean, Serializable { 44 45 46 48 49 55 public BasicDynaBean(DynaClass dynaClass) { 56 57 super(); 58 this.dynaClass = dynaClass; 59 60 } 61 62 63 65 66 70 protected DynaClass dynaClass = null; 71 72 73 76 protected HashMap values = new HashMap (); 77 78 79 81 82 92 public boolean contains(String name, String key) { 93 94 Object value = values.get(name); 95 if (value == null) { 96 throw new NullPointerException 97 ("No mapped value for '" + name + "(" + key + ")'"); 98 } else if (value instanceof Map ) { 99 return (((Map ) value).containsKey(key)); 100 } else { 101 throw new IllegalArgumentException 102 ("Non-mapped property for '" + name + "(" + key + ")'"); 103 } 104 105 } 106 107 108 116 public Object get(String name) { 117 118 Object value = values.get(name); 120 if (value != null) { 121 return (value); 122 } 123 124 Class type = getDynaProperty(name).getType(); 126 if (!type.isPrimitive()) { 127 return (value); 128 } 129 130 if (type == Boolean.TYPE) { 132 return (Boolean.FALSE); 133 } else if (type == Byte.TYPE) { 134 return (new Byte ((byte) 0)); 135 } else if (type == Character.TYPE) { 136 return (new Character ((char) 0)); 137 } else if (type == Double.TYPE) { 138 return (new Double ((double) 0.0)); 139 } else if (type == Float.TYPE) { 140 return (new Float ((float) 0.0)); 141 } else if (type == Integer.TYPE) { 142 return (new Integer ((int) 0)); 143 } else if (type == Long.TYPE) { 144 return (new Long ((int) 0)); 145 } else if (type == Short.TYPE) { 146 return (new Short ((short) 0)); 147 } else { 148 return (null); 149 } 150 151 } 152 153 154 169 public Object get(String name, int index) { 170 171 Object value = values.get(name); 172 if (value == null) { 173 throw new NullPointerException 174 ("No indexed value for '" + name + "[" + index + "]'"); 175 } else if (value.getClass().isArray()) { 176 return (Array.get(value, index)); 177 } else if (value instanceof List ) { 178 return ((List ) value).get(index); 179 } else { 180 throw new IllegalArgumentException 181 ("Non-indexed property for '" + name + "[" + index + "]'"); 182 } 183 184 } 185 186 187 199 public Object get(String name, String key) { 200 201 Object value = values.get(name); 202 if (value == null) { 203 throw new NullPointerException 204 ("No mapped value for '" + name + "(" + key + ")'"); 205 } else if (value instanceof Map ) { 206 return (((Map ) value).get(key)); 207 } else { 208 throw new IllegalArgumentException 209 ("Non-mapped property for '" + name + "(" + key + ")'"); 210 } 211 212 } 213 214 215 219 public DynaClass getDynaClass() { 220 221 return (this.dynaClass); 222 223 } 224 225 226 237 public void remove(String name, String key) { 238 239 Object value = values.get(name); 240 if (value == null) { 241 throw new NullPointerException 242 ("No mapped value for '" + name + "(" + key + ")'"); 243 } else if (value instanceof Map ) { 244 ((Map ) value).remove(key); 245 } else { 246 throw new IllegalArgumentException 247 ("Non-mapped property for '" + name + "(" + key + ")'"); 248 } 249 250 } 251 252 253 266 public void set(String name, Object value) { 267 268 DynaProperty descriptor = getDynaProperty(name); 269 if (value == null) { 270 if (descriptor.getType().isPrimitive()) { 271 throw new NullPointerException 272 ("Primitive value for '" + name + "'"); 273 } 274 } else if (!isAssignable(descriptor.getType(), value.getClass())) { 275 throw new ConversionException 276 ("Cannot assign value of type '" + 277 value.getClass().getName() + 278 "' to property '" + name + "' of type '" + 279 descriptor.getType().getName() + "'"); 280 } 281 values.put(name, value); 282 283 } 284 285 286 302 public void set(String name, int index, Object value) { 303 304 Object prop = values.get(name); 305 if (prop == null) { 306 throw new NullPointerException 307 ("No indexed value for '" + name + "[" + index + "]'"); 308 } else if (prop.getClass().isArray()) { 309 Array.set(prop, index, value); 310 } else if (prop instanceof List ) { 311 try { 312 ((List ) prop).set(index, value); 313 } catch (ClassCastException e) { 314 throw new ConversionException(e.getMessage()); 315 } 316 } else { 317 throw new IllegalArgumentException 318 ("Non-indexed property for '" + name + "[" + index + "]'"); 319 } 320 321 } 322 323 324 338 public void set(String name, String key, Object value) { 339 340 Object prop = values.get(name); 341 if (prop == null) { 342 throw new NullPointerException 343 ("No mapped value for '" + name + "(" + key + ")'"); 344 } else if (prop instanceof Map ) { 345 ((Map ) prop).put(key, value); 346 } else { 347 throw new IllegalArgumentException 348 ("Non-mapped property for '" + name + "(" + key + ")'"); 349 } 350 351 } 352 353 354 356 357 365 protected DynaProperty getDynaProperty(String name) { 366 367 DynaProperty descriptor = getDynaClass().getDynaProperty(name); 368 if (descriptor == null) { 369 throw new IllegalArgumentException 370 ("Invalid property name '" + name + "'"); 371 } 372 return (descriptor); 373 374 } 375 376 377 383 protected boolean isAssignable(Class dest, Class source) { 384 385 if (dest.isAssignableFrom(source) || 386 ((dest == Boolean.TYPE) && (source == Boolean .class)) || 387 ((dest == Byte.TYPE) && (source == Byte .class)) || 388 ((dest == Character.TYPE) && (source == Character .class)) || 389 ((dest == Double.TYPE) && (source == Double .class)) || 390 ((dest == Float.TYPE) && (source == Float .class)) || 391 ((dest == Integer.TYPE) && (source == Integer .class)) || 392 ((dest == Long.TYPE) && (source == Long .class)) || 393 ((dest == Short.TYPE) && (source == Short .class))) { 394 return (true); 395 } else { 396 return (false); 397 } 398 399 } 400 401 402 } 403 | Popular Tags |