| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.commons.beanutils; |
| 19 | |
|
| 20 | |
|
| 21 | |
import java.beans.BeanInfo; |
| 22 | |
import java.beans.IndexedPropertyDescriptor; |
| 23 | |
import java.beans.IntrospectionException; |
| 24 | |
import java.beans.Introspector; |
| 25 | |
import java.beans.PropertyDescriptor; |
| 26 | |
import java.lang.reflect.Array; |
| 27 | |
import java.lang.reflect.InvocationTargetException; |
| 28 | |
import java.lang.reflect.Method; |
| 29 | |
import java.util.HashMap; |
| 30 | |
import java.util.Iterator; |
| 31 | |
import java.util.List; |
| 32 | |
import java.util.Map; |
| 33 | |
|
| 34 | |
import org.apache.commons.beanutils.expression.DefaultResolver; |
| 35 | |
import org.apache.commons.beanutils.expression.Resolver; |
| 36 | |
import org.apache.commons.collections.FastHashMap; |
| 37 | |
import org.apache.commons.logging.Log; |
| 38 | |
import org.apache.commons.logging.LogFactory; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
public class PropertyUtilsBean { |
| 104 | |
|
| 105 | 193 | private Resolver resolver = new DefaultResolver(); |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
protected static PropertyUtilsBean getInstance() { |
| 114 | 1003 | return BeanUtilsBean.getInstance().getPropertyUtils(); |
| 115 | |
} |
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | 193 | private WeakFastHashMap descriptorsCache = null; |
| 124 | 193 | private WeakFastHashMap mappedDescriptorsCache = null; |
| 125 | 1 | private static final Class[] EMPTY_CLASS_PARAMETERS = new Class[0]; |
| 126 | 1 | private static final Class[] LIST_CLASS_PARAMETER = new Class[] {java.util.List.class}; |
| 127 | |
|
| 128 | |
|
| 129 | 1 | private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; |
| 130 | |
|
| 131 | |
|
| 132 | 193 | private Log log = LogFactory.getLog(PropertyUtils.class); |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 193 | public PropertyUtilsBean() { |
| 138 | 193 | descriptorsCache = new WeakFastHashMap(); |
| 139 | 193 | descriptorsCache.setFast(true); |
| 140 | 193 | mappedDescriptorsCache = new WeakFastHashMap(); |
| 141 | 193 | mappedDescriptorsCache.setFast(true); |
| 142 | 193 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
public Resolver getResolver() { |
| 162 | 474 | return resolver; |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
public void setResolver(Resolver resolver) { |
| 179 | 0 | if (resolver == null) { |
| 180 | 0 | this.resolver = new DefaultResolver(); |
| 181 | |
} else { |
| 182 | 0 | this.resolver = resolver; |
| 183 | |
} |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public void clearDescriptors() { |
| 192 | |
|
| 193 | 32 | descriptorsCache.clear(); |
| 194 | 32 | mappedDescriptorsCache.clear(); |
| 195 | 32 | Introspector.flushCaches(); |
| 196 | |
|
| 197 | 32 | } |
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
public void copyProperties(Object dest, Object orig) |
| 232 | |
throws IllegalAccessException, InvocationTargetException, |
| 233 | |
NoSuchMethodException { |
| 234 | |
|
| 235 | 7 | if (dest == null) { |
| 236 | 0 | throw new IllegalArgumentException |
| 237 | |
("No destination bean specified"); |
| 238 | |
} |
| 239 | 7 | if (orig == null) { |
| 240 | 0 | throw new IllegalArgumentException("No origin bean specified"); |
| 241 | |
} |
| 242 | |
|
| 243 | 7 | if (orig instanceof DynaBean) { |
| 244 | 2 | DynaProperty[] origDescriptors = |
| 245 | |
((DynaBean) orig).getDynaClass().getDynaProperties(); |
| 246 | 29 | for (int i = 0; i < origDescriptors.length; i++) { |
| 247 | 27 | String name = origDescriptors[i].getName(); |
| 248 | 27 | if (isReadable(orig, name) && isWriteable(dest, name)) { |
| 249 | |
try { |
| 250 | 20 | Object value = ((DynaBean) orig).get(name); |
| 251 | 20 | if (dest instanceof DynaBean) { |
| 252 | 20 | ((DynaBean) dest).set(name, value); |
| 253 | |
} else { |
| 254 | 0 | setSimpleProperty(dest, name, value); |
| 255 | |
} |
| 256 | 0 | } catch (NoSuchMethodException e) { |
| 257 | 0 | if (log.isDebugEnabled()) { |
| 258 | 0 | log.debug("Error writing to '" + name + "' on class '" + dest.getClass() + "'", e); |
| 259 | |
} |
| 260 | 20 | } |
| 261 | |
} |
| 262 | |
} |
| 263 | 2 | } else if (orig instanceof Map) { |
| 264 | 3 | Iterator entries = ((Map) orig).entrySet().iterator(); |
| 265 | 22 | while (entries.hasNext()) { |
| 266 | 19 | Map.Entry entry = (Map.Entry) entries.next(); |
| 267 | 19 | String name = (String)entry.getKey(); |
| 268 | 19 | if (isWriteable(dest, name)) { |
| 269 | |
try { |
| 270 | 18 | if (dest instanceof DynaBean) { |
| 271 | 9 | ((DynaBean) dest).set(name, entry.getValue()); |
| 272 | |
} else { |
| 273 | 9 | setSimpleProperty(dest, name, entry.getValue()); |
| 274 | |
} |
| 275 | 0 | } catch (NoSuchMethodException e) { |
| 276 | 0 | if (log.isDebugEnabled()) { |
| 277 | 0 | log.debug("Error writing to '" + name + "' on class '" + dest.getClass() + "'", e); |
| 278 | |
} |
| 279 | 18 | } |
| 280 | |
} |
| 281 | 19 | } |
| 282 | 3 | } else { |
| 283 | 2 | PropertyDescriptor[] origDescriptors = |
| 284 | |
getPropertyDescriptors(orig); |
| 285 | 32 | for (int i = 0; i < origDescriptors.length; i++) { |
| 286 | 31 | String name = origDescriptors[i].getName(); |
| 287 | 31 | if (isReadable(orig, name) && isWriteable(dest, name)) { |
| 288 | |
try { |
| 289 | 2 | Object value = getSimpleProperty(orig, name); |
| 290 | 2 | if (dest instanceof DynaBean) { |
| 291 | 0 | ((DynaBean) dest).set(name, value); |
| 292 | |
} else { |
| 293 | 2 | setSimpleProperty(dest, name, value); |
| 294 | |
} |
| 295 | 1 | } catch (NoSuchMethodException e) { |
| 296 | 1 | if (log.isDebugEnabled()) { |
| 297 | 0 | log.debug("Error writing to '" + name + "' on class '" + dest.getClass() + "'", e); |
| 298 | |
} |
| 299 | 0 | } |
| 300 | |
} |
| 301 | |
} |
| 302 | |
} |
| 303 | |
|
| 304 | 6 | } |
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
public Map describe(Object bean) |
| 327 | |
throws IllegalAccessException, InvocationTargetException, |
| 328 | |
NoSuchMethodException { |
| 329 | |
|
| 330 | 3 | if (bean == null) { |
| 331 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 332 | |
} |
| 333 | 3 | Map description = new HashMap(); |
| 334 | 3 | if (bean instanceof DynaBean) { |
| 335 | 2 | DynaProperty[] descriptors = |
| 336 | |
((DynaBean) bean).getDynaClass().getDynaProperties(); |
| 337 | 42 | for (int i = 0; i < descriptors.length; i++) { |
| 338 | 40 | String name = descriptors[i].getName(); |
| 339 | 40 | description.put(name, getProperty(bean, name)); |
| 340 | |
} |
| 341 | 2 | } else { |
| 342 | 1 | PropertyDescriptor[] descriptors = |
| 343 | |
getPropertyDescriptors(bean); |
| 344 | 30 | for (int i = 0; i < descriptors.length; i++) { |
| 345 | 29 | String name = descriptors[i].getName(); |
| 346 | 29 | if (descriptors[i].getReadMethod() != null) { |
| 347 | 25 | description.put(name, getProperty(bean, name)); |
| 348 | |
} |
| 349 | |
} |
| 350 | |
} |
| 351 | 3 | return (description); |
| 352 | |
|
| 353 | |
} |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | |
|
| 369 | |
|
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | |
public Object getIndexedProperty(Object bean, String name) |
| 381 | |
throws IllegalAccessException, InvocationTargetException, |
| 382 | |
NoSuchMethodException { |
| 383 | |
|
| 384 | 153 | if (bean == null) { |
| 385 | 4 | throw new IllegalArgumentException("No bean specified"); |
| 386 | |
} |
| 387 | 149 | if (name == null) { |
| 388 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 389 | |
bean.getClass() + "'"); |
| 390 | |
} |
| 391 | |
|
| 392 | |
|
| 393 | 149 | int index = -1; |
| 394 | |
try { |
| 395 | 149 | index = resolver.getIndex(name); |
| 396 | 0 | } catch (IllegalArgumentException e) { |
| 397 | 0 | throw new IllegalArgumentException("Invalid indexed property '" + |
| 398 | |
name + "' on bean class '" + bean.getClass() + "' " + |
| 399 | |
e.getMessage()); |
| 400 | 149 | } |
| 401 | 149 | if (index < 0) { |
| 402 | 4 | throw new IllegalArgumentException("Invalid indexed property '" + |
| 403 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 404 | |
} |
| 405 | |
|
| 406 | |
|
| 407 | 145 | name = resolver.getProperty(name); |
| 408 | |
|
| 409 | |
|
| 410 | 145 | return (getIndexedProperty(bean, name, index)); |
| 411 | |
|
| 412 | |
} |
| 413 | |
|
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
|
| 436 | |
|
| 437 | |
public Object getIndexedProperty(Object bean, |
| 438 | |
String name, int index) |
| 439 | |
throws IllegalAccessException, InvocationTargetException, |
| 440 | |
NoSuchMethodException { |
| 441 | |
|
| 442 | 286 | if (bean == null) { |
| 443 | 4 | throw new IllegalArgumentException("No bean specified"); |
| 444 | |
} |
| 445 | 282 | if (name == null || name.length() == 0) { |
| 446 | 29 | if (bean.getClass().isArray()) { |
| 447 | 11 | return Array.get(bean, index); |
| 448 | 18 | } else if (bean instanceof List) { |
| 449 | 10 | return ((List)bean).get(index); |
| 450 | |
} |
| 451 | |
} |
| 452 | 261 | if (name == null) { |
| 453 | 4 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 454 | |
bean.getClass() + "'"); |
| 455 | |
} |
| 456 | |
|
| 457 | |
|
| 458 | 257 | if (bean instanceof DynaBean) { |
| 459 | 78 | DynaProperty descriptor = |
| 460 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 461 | 78 | if (descriptor == null) { |
| 462 | 2 | throw new NoSuchMethodException("Unknown property '" + |
| 463 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 464 | |
} |
| 465 | 76 | return (((DynaBean) bean).get(name, index)); |
| 466 | |
} |
| 467 | |
|
| 468 | |
|
| 469 | 179 | PropertyDescriptor descriptor = |
| 470 | |
getPropertyDescriptor(bean, name); |
| 471 | 179 | if (descriptor == null) { |
| 472 | 3 | throw new NoSuchMethodException("Unknown property '" + |
| 473 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 474 | |
} |
| 475 | |
|
| 476 | |
|
| 477 | 176 | if (descriptor instanceof IndexedPropertyDescriptor) { |
| 478 | 93 | Method readMethod = ((IndexedPropertyDescriptor) descriptor). |
| 479 | |
getIndexedReadMethod(); |
| 480 | 93 | readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod); |
| 481 | 93 | if (readMethod != null) { |
| 482 | 91 | Object[] subscript = new Object[1]; |
| 483 | 91 | subscript[0] = new Integer(index); |
| 484 | |
try { |
| 485 | 91 | return (invokeMethod(readMethod,bean, subscript)); |
| 486 | 8 | } catch (InvocationTargetException e) { |
| 487 | 8 | if (e.getTargetException() instanceof |
| 488 | |
IndexOutOfBoundsException) { |
| 489 | 8 | throw (IndexOutOfBoundsException) |
| 490 | |
e.getTargetException(); |
| 491 | |
} else { |
| 492 | 0 | throw e; |
| 493 | |
} |
| 494 | |
} |
| 495 | |
} |
| 496 | |
} |
| 497 | |
|
| 498 | |
|
| 499 | 85 | Method readMethod = getReadMethod(bean.getClass(), descriptor); |
| 500 | 85 | if (readMethod == null) { |
| 501 | 2 | throw new NoSuchMethodException("Property '" + name + "' has no " + |
| 502 | |
"getter method on bean class '" + bean.getClass() + "'"); |
| 503 | |
} |
| 504 | |
|
| 505 | |
|
| 506 | 83 | Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); |
| 507 | 83 | if (!value.getClass().isArray()) { |
| 508 | 35 | if (!(value instanceof java.util.List)) { |
| 509 | 0 | throw new IllegalArgumentException("Property '" + name + |
| 510 | |
"' is not indexed on bean class '" + bean.getClass() + "'"); |
| 511 | |
} else { |
| 512 | |
|
| 513 | 35 | return ((java.util.List) value).get(index); |
| 514 | |
} |
| 515 | |
} else { |
| 516 | |
|
| 517 | |
try { |
| 518 | 48 | return (Array.get(value, index)); |
| 519 | 6 | } catch (ArrayIndexOutOfBoundsException e) { |
| 520 | 6 | throw new ArrayIndexOutOfBoundsException("Index: " + |
| 521 | |
index + ", Size: " + Array.getLength(value) + |
| 522 | |
" for property '" + name + "'"); |
| 523 | |
} |
| 524 | |
} |
| 525 | |
|
| 526 | |
} |
| 527 | |
|
| 528 | |
|
| 529 | |
|
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
|
| 538 | |
|
| 539 | |
|
| 540 | |
|
| 541 | |
|
| 542 | |
|
| 543 | |
|
| 544 | |
|
| 545 | |
|
| 546 | |
|
| 547 | |
|
| 548 | |
public Object getMappedProperty(Object bean, String name) |
| 549 | |
throws IllegalAccessException, InvocationTargetException, |
| 550 | |
NoSuchMethodException { |
| 551 | |
|
| 552 | 58 | if (bean == null) { |
| 553 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 554 | |
} |
| 555 | 56 | if (name == null) { |
| 556 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 557 | |
bean.getClass() + "'"); |
| 558 | |
} |
| 559 | |
|
| 560 | |
|
| 561 | 56 | String key = null; |
| 562 | |
try { |
| 563 | 56 | key = resolver.getKey(name); |
| 564 | 0 | } catch (IllegalArgumentException e) { |
| 565 | 0 | throw new IllegalArgumentException |
| 566 | |
("Invalid mapped property '" + name + |
| 567 | |
"' on bean class '" + bean.getClass() + "' " + e.getMessage()); |
| 568 | 56 | } |
| 569 | 56 | if (key == null) { |
| 570 | 2 | throw new IllegalArgumentException("Invalid mapped property '" + |
| 571 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 572 | |
} |
| 573 | |
|
| 574 | |
|
| 575 | 54 | name = resolver.getProperty(name); |
| 576 | |
|
| 577 | |
|
| 578 | 54 | return (getMappedProperty(bean, name, key)); |
| 579 | |
|
| 580 | |
} |
| 581 | |
|
| 582 | |
|
| 583 | |
|
| 584 | |
|
| 585 | |
|
| 586 | |
|
| 587 | |
|
| 588 | |
|
| 589 | |
|
| 590 | |
|
| 591 | |
|
| 592 | |
|
| 593 | |
|
| 594 | |
|
| 595 | |
|
| 596 | |
|
| 597 | |
|
| 598 | |
|
| 599 | |
public Object getMappedProperty(Object bean, |
| 600 | |
String name, String key) |
| 601 | |
throws IllegalAccessException, InvocationTargetException, |
| 602 | |
NoSuchMethodException { |
| 603 | |
|
| 604 | 82 | if (bean == null) { |
| 605 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 606 | |
} |
| 607 | 80 | if (name == null) { |
| 608 | 2 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 609 | |
bean.getClass() + "'"); |
| 610 | |
} |
| 611 | 78 | if (key == null) { |
| 612 | 2 | throw new IllegalArgumentException("No key specified for property '" + |
| 613 | |
name + "' on bean class " + bean.getClass() + "'"); |
| 614 | |
} |
| 615 | |
|
| 616 | |
|
| 617 | 76 | if (bean instanceof DynaBean) { |
| 618 | 19 | DynaProperty descriptor = |
| 619 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 620 | 19 | if (descriptor == null) { |
| 621 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 622 | |
name + "'+ on bean class '" + bean.getClass() + "'"); |
| 623 | |
} |
| 624 | 18 | return (((DynaBean) bean).get(name, key)); |
| 625 | |
} |
| 626 | |
|
| 627 | 57 | Object result = null; |
| 628 | |
|
| 629 | |
|
| 630 | 57 | PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); |
| 631 | 57 | if (descriptor == null) { |
| 632 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 633 | |
name + "'+ on bean class '" + bean.getClass() + "'"); |
| 634 | |
} |
| 635 | |
|
| 636 | 56 | if (descriptor instanceof MappedPropertyDescriptor) { |
| 637 | |
|
| 638 | 42 | Method readMethod = ((MappedPropertyDescriptor) descriptor). |
| 639 | |
getMappedReadMethod(); |
| 640 | 42 | readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod); |
| 641 | 42 | if (readMethod != null) { |
| 642 | 40 | Object[] keyArray = new Object[1]; |
| 643 | 40 | keyArray[0] = key; |
| 644 | 40 | result = invokeMethod(readMethod, bean, keyArray); |
| 645 | 40 | } else { |
| 646 | 2 | throw new NoSuchMethodException("Property '" + name + |
| 647 | |
"' has no mapped getter method on bean class '" + |
| 648 | |
bean.getClass() + "'"); |
| 649 | |
} |
| 650 | 40 | } else { |
| 651 | |
|
| 652 | 14 | Method readMethod = getReadMethod(bean.getClass(), descriptor); |
| 653 | 14 | if (readMethod != null) { |
| 654 | 14 | Object invokeResult = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); |
| 655 | |
|
| 656 | 14 | if (invokeResult instanceof java.util.Map) { |
| 657 | 14 | result = ((java.util.Map)invokeResult).get(key); |
| 658 | |
} |
| 659 | 14 | } else { |
| 660 | 0 | throw new NoSuchMethodException("Property '" + name + |
| 661 | |
"' has no mapped getter method on bean class '" + |
| 662 | |
bean.getClass() + "'"); |
| 663 | |
} |
| 664 | |
} |
| 665 | 54 | return result; |
| 666 | |
|
| 667 | |
} |
| 668 | |
|
| 669 | |
|
| 670 | |
|
| 671 | |
|
| 672 | |
|
| 673 | |
|
| 674 | |
|
| 675 | |
|
| 676 | |
|
| 677 | |
|
| 678 | |
|
| 679 | |
public FastHashMap getMappedPropertyDescriptors(Class beanClass) { |
| 680 | |
|
| 681 | 189 | if (beanClass == null) { |
| 682 | 0 | return null; |
| 683 | |
} |
| 684 | |
|
| 685 | |
|
| 686 | 189 | return (FastHashMap) mappedDescriptorsCache.get(beanClass); |
| 687 | |
|
| 688 | |
} |
| 689 | |
|
| 690 | |
|
| 691 | |
|
| 692 | |
|
| 693 | |
|
| 694 | |
|
| 695 | |
|
| 696 | |
|
| 697 | |
|
| 698 | |
|
| 699 | |
|
| 700 | |
public FastHashMap getMappedPropertyDescriptors(Object bean) { |
| 701 | |
|
| 702 | 183 | if (bean == null) { |
| 703 | 0 | return null; |
| 704 | |
} |
| 705 | 183 | return (getMappedPropertyDescriptors(bean.getClass())); |
| 706 | |
|
| 707 | |
} |
| 708 | |
|
| 709 | |
|
| 710 | |
|
| 711 | |
|
| 712 | |
|
| 713 | |
|
| 714 | |
|
| 715 | |
|
| 716 | |
|
| 717 | |
|
| 718 | |
|
| 719 | |
|
| 720 | |
|
| 721 | |
|
| 722 | |
|
| 723 | |
|
| 724 | |
|
| 725 | |
|
| 726 | |
|
| 727 | |
|
| 728 | |
|
| 729 | |
public Object getNestedProperty(Object bean, String name) |
| 730 | |
throws IllegalAccessException, InvocationTargetException, |
| 731 | |
NoSuchMethodException { |
| 732 | |
|
| 733 | 440 | if (bean == null) { |
| 734 | 3 | throw new IllegalArgumentException("No bean specified"); |
| 735 | |
} |
| 736 | 437 | if (name == null) { |
| 737 | 2 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 738 | |
bean.getClass() + "'"); |
| 739 | |
} |
| 740 | |
|
| 741 | |
|
| 742 | 514 | while (resolver.hasNested(name)) { |
| 743 | 84 | String next = resolver.next(name); |
| 744 | 84 | Object nestedBean = null; |
| 745 | 84 | if (bean instanceof Map) { |
| 746 | 1 | nestedBean = getPropertyOfMapBean((Map) bean, next); |
| 747 | 83 | } else if (resolver.isMapped(next)) { |
| 748 | 13 | nestedBean = getMappedProperty(bean, next); |
| 749 | 70 | } else if (resolver.isIndexed(next)) { |
| 750 | 20 | nestedBean = getIndexedProperty(bean, next); |
| 751 | |
} else { |
| 752 | 50 | nestedBean = getSimpleProperty(bean, next); |
| 753 | |
} |
| 754 | 84 | if (nestedBean == null) { |
| 755 | 5 | throw new NestedNullException |
| 756 | |
("Null property value for '" + name + |
| 757 | |
"' on bean class '" + bean.getClass() + "'"); |
| 758 | |
} |
| 759 | 79 | bean = nestedBean; |
| 760 | 79 | name = resolver.remove(name); |
| 761 | 79 | } |
| 762 | |
|
| 763 | 430 | if (bean instanceof Map) { |
| 764 | 25 | bean = getPropertyOfMapBean((Map) bean, name); |
| 765 | 405 | } else if (resolver.isMapped(name)) { |
| 766 | 24 | bean = getMappedProperty(bean, name); |
| 767 | 381 | } else if (resolver.isIndexed(name)) { |
| 768 | 40 | bean = getIndexedProperty(bean, name); |
| 769 | |
} else { |
| 770 | 341 | bean = getSimpleProperty(bean, name); |
| 771 | |
} |
| 772 | 412 | return bean; |
| 773 | |
|
| 774 | |
} |
| 775 | |
|
| 776 | |
|
| 777 | |
|
| 778 | |
|
| 779 | |
|
| 780 | |
|
| 781 | |
|
| 782 | |
|
| 783 | |
|
| 784 | |
|
| 785 | |
|
| 786 | |
|
| 787 | |
|
| 788 | |
|
| 789 | |
|
| 790 | |
|
| 791 | |
|
| 792 | |
|
| 793 | |
|
| 794 | |
|
| 795 | |
|
| 796 | |
|
| 797 | |
|
| 798 | |
|
| 799 | |
|
| 800 | |
protected Object getPropertyOfMapBean(Map bean, String propertyName) |
| 801 | |
throws IllegalArgumentException, IllegalAccessException, |
| 802 | |
InvocationTargetException, NoSuchMethodException { |
| 803 | |
|
| 804 | 22 | if (resolver.isMapped(propertyName)) { |
| 805 | 7 | String name = resolver.getProperty(propertyName); |
| 806 | 7 | if (name == null || name.length() == 0) { |
| 807 | 7 | propertyName = resolver.getKey(propertyName); |
| 808 | |
} |
| 809 | |
} |
| 810 | |
|
| 811 | 22 | if (resolver.isIndexed(propertyName) || |
| 812 | |
resolver.isMapped(propertyName)) { |
| 813 | 1 | throw new IllegalArgumentException( |
| 814 | |
"Indexed or mapped properties are not supported on" |
| 815 | |
+ " objects of type Map: " + propertyName); |
| 816 | |
} |
| 817 | |
|
| 818 | 21 | return bean.get(propertyName); |
| 819 | |
} |
| 820 | |
|
| 821 | |
|
| 822 | |
|
| 823 | |
|
| 824 | |
|
| 825 | |
|
| 826 | |
|
| 827 | |
|
| 828 | |
|
| 829 | |
|
| 830 | |
|
| 831 | |
|
| 832 | |
|
| 833 | |
|
| 834 | |
|
| 835 | |
|
| 836 | |
|
| 837 | |
|
| 838 | |
|
| 839 | |
|
| 840 | |
|
| 841 | |
|
| 842 | |
public Object getProperty(Object bean, String name) |
| 843 | |
throws IllegalAccessException, InvocationTargetException, |
| 844 | |
NoSuchMethodException { |
| 845 | |
|
| 846 | 316 | return (getNestedProperty(bean, name)); |
| 847 | |
|
| 848 | |
} |
| 849 | |
|
| 850 | |
|
| 851 | |
|
| 852 | |
|
| 853 | |
|
| 854 | |
|
| 855 | |
|
| 856 | |
|
| 857 | |
|
| 858 | |
|
| 859 | |
|
| 860 | |
|
| 861 | |
|
| 862 | |
|
| 863 | |
|
| 864 | |
|
| 865 | |
|
| 866 | |
|
| 867 | |
|
| 868 | |
|
| 869 | |
|
| 870 | |
|
| 871 | |
|
| 872 | |
|
| 873 | |
|
| 874 | |
|
| 875 | |
|
| 876 | |
|
| 877 | |
public PropertyDescriptor getPropertyDescriptor(Object bean, |
| 878 | |
String name) |
| 879 | |
throws IllegalAccessException, InvocationTargetException, |
| 880 | |
NoSuchMethodException { |
| 881 | |
|
| 882 | 1991 | if (bean == null) { |
| 883 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 884 | |
} |
| 885 | 1989 | if (name == null) { |
| 886 | 1 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 887 | |
bean.getClass() + "'"); |
| 888 | |
} |
| 889 | |
|
| 890 | |
|
| 891 | 1989 | while (resolver.hasNested(name)) { |
| 892 | 1 | String next = resolver.next(name); |
| 893 | 1 | Object nestedBean = getProperty(bean, next); |
| 894 | 1 | if (nestedBean == null) { |
| 895 | 0 | throw new NestedNullException |
| 896 | |
("Null property value for '" + next + |
| 897 | |
"' on bean class '" + bean.getClass() + "'"); |
| 898 | |
} |
| 899 | 1 | bean = nestedBean; |
| 900 | 1 | name = resolver.remove(name); |
| 901 | 1 | } |
| 902 | |
|
| 903 | |
|
| 904 | 1988 | name = resolver.getProperty(name); |
| 905 | |
|
| 906 | |
|
| 907 | |
|
| 908 | 1988 | if (name == null) { |
| 909 | 0 | return (null); |
| 910 | |
} |
| 911 | |
|
| 912 | 1988 | PropertyDescriptor[] descriptors = getPropertyDescriptors(bean); |
| 913 | 1988 | if (descriptors != null) { |
| 914 | |
|
| 915 | 28075 | for (int i = 0; i < descriptors.length; i++) { |
| 916 | 27892 | if (name.equals(descriptors[i].getName())) { |
| 917 | 1805 | return (descriptors[i]); |
| 918 | |
} |
| 919 | |
} |
| 920 | |
} |
| 921 | |
|
| 922 | 183 | PropertyDescriptor result = null; |
| 923 | 183 | FastHashMap mappedDescriptors = |
| 924 | |
getMappedPropertyDescriptors(bean); |
| 925 | 183 | if (mappedDescriptors == null) { |
| 926 | 21 | mappedDescriptors = new FastHashMap(); |
| 927 | 21 | mappedDescriptors.setFast(true); |
| 928 | 21 | mappedDescriptorsCache.put(bean.getClass(), mappedDescriptors); |
| 929 | |
} |
| 930 | 183 | result = (PropertyDescriptor) mappedDescriptors.get(name); |
| 931 | 183 | if (result == null) { |
| 932 | |
|
| 933 | |
try { |
| 934 | 73 | result = new MappedPropertyDescriptor(name, bean.getClass()); |
| 935 | 53 | } catch (IntrospectionException ie) { |
| 936 | |
|
| 937 | |
|
| 938 | |
|
| 939 | 20 | } |
| 940 | 73 | if (result != null) { |
| 941 | 20 | mappedDescriptors.put(name, result); |
| 942 | |
} |
| 943 | |
} |
| 944 | |
|
| 945 | 183 | return result; |
| 946 | |
|
| 947 | |
} |
| 948 | |
|
| 949 | |
|
| 950 | |
|
| 951 | |
|
| 952 | |
|
| 953 | |
|
| 954 | |
|
| 955 | |
|
| 956 | |
|
| 957 | |
|
| 958 | |
|
| 959 | |
|
| 960 | |
|
| 961 | |
|
| 962 | |
public PropertyDescriptor[] |
| 963 | |
getPropertyDescriptors(Class beanClass) { |
| 964 | |
|
| 965 | 2020 | if (beanClass == null) { |
| 966 | 1 | throw new IllegalArgumentException("No bean class specified"); |
| 967 | |
} |
| 968 | |
|
| 969 | |
|
| 970 | 2019 | PropertyDescriptor[] descriptors = null; |
| 971 | 2019 | descriptors = |
| 972 | |
(PropertyDescriptor[]) descriptorsCache.get(beanClass); |
| 973 | 2019 | if (descriptors != null) { |
| 974 | 1825 | return (descriptors); |
| 975 | |
} |
| 976 | |
|
| 977 | |
|
| 978 | 194 | BeanInfo beanInfo = null; |
| 979 | |
try { |
| 980 | 194 | beanInfo = Introspector.getBeanInfo(beanClass); |
| 981 | 0 | } catch (IntrospectionException e) { |
| 982 | 0 | return (new PropertyDescriptor[0]); |
| 983 | 194 | } |
| 984 | 194 | descriptors = beanInfo.getPropertyDescriptors(); |
| 985 | 194 | if (descriptors == null) { |
| 986 | 0 | descriptors = new PropertyDescriptor[0]; |
| 987 | |
} |
| 988 | |
|
| 989 | |
|
| 990 | |
|
| 991 | |
|
| 992 | |
|
| 993 | |
|
| 994 | |
|
| 995 | |
|
| 996 | |
|
| 997 | |
|
| 998 | |
|
| 999 | |
|
| 1000 | |
|
| 1001 | |
|
| 1002 | |
|
| 1003 | |
|
| 1004 | |
|
| 1005 | |
|
| 1006 | |
|
| 1007 | |
|
| 1008 | 3802 | for (int i = 0; i < descriptors.length; i++) { |
| 1009 | 3608 | if (descriptors[i] instanceof IndexedPropertyDescriptor) { |
| 1010 | 555 | IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor)descriptors[i]; |
| 1011 | 555 | String propName = descriptor.getName().substring(0, 1).toUpperCase() + |
| 1012 | |
descriptor.getName().substring(1); |
| 1013 | |
|
| 1014 | 555 | if (descriptor.getReadMethod() == null) { |
| 1015 | 355 | String methodName = descriptor.getIndexedReadMethod() != null |
| 1016 | |
? descriptor.getIndexedReadMethod().getName() |
| 1017 | |
: "get" + propName; |
| 1018 | 355 | Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, |
| 1019 | |
methodName, |
| 1020 | |
EMPTY_CLASS_PARAMETERS); |
| 1021 | 355 | if (readMethod != null) { |
| 1022 | |
try { |
| 1023 | 2 | descriptor.setReadMethod(readMethod); |
| 1024 | 0 | } catch(Exception e) { |
| 1025 | 0 | log.error("Error setting indexed property read method", e); |
| 1026 | 2 | } |
| 1027 | |
} |
| 1028 | |
} |
| 1029 | 555 | if (descriptor.getWriteMethod() == null) { |
| 1030 | 355 | String methodName = descriptor.getIndexedWriteMethod() != null |
| 1031 | |
? descriptor.getIndexedWriteMethod().getName() |
| 1032 | |
: "set" + propName; |
| 1033 | 355 | Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, |
| 1034 | |
methodName, |
| 1035 | |
LIST_CLASS_PARAMETER); |
| 1036 | 355 | if (writeMethod == null) { |
| 1037 | 354 | Method[] methods = beanClass.getMethods(); |
| 1038 | 25002 | for (int j = 0; j < methods.length; j++) { |
| 1039 | 24649 | if (methods[j].getName().equals(methodName)) { |
| 1040 | 239 | Class[] parameterTypes = methods[j].getParameterTypes(); |
| 1041 | 239 | if (parameterTypes.length == 1 && |
| 1042 | |
List.class.isAssignableFrom(parameterTypes[0])) { |
| 1043 | 1 | writeMethod = methods[j]; |
| 1044 | 1 | break; |
| 1045 | |
} |
| 1046 | |
} |
| 1047 | |
} |
| 1048 | |
} |
| 1049 | 355 | if (writeMethod != null) { |
| 1050 | |
try { |
| 1051 | 2 | descriptor.setWriteMethod(writeMethod); |
| 1052 | 0 | } catch(Exception e) { |
| 1053 | 0 | log.error("Error setting indexed property write method", e); |
| 1054 | 2 | } |
| 1055 | |
} |
| 1056 | |
} |
| 1057 | |
} |
| 1058 | |
} |
| 1059 | |
|
| 1060 | |
try { |
| 1061 | 194 | IntrospectorUtils.setNonSyntheticReadWriteMethods(beanClass, descriptors); |
| 1062 | 0 | } catch (NoSuchMethodException e) { |
| 1063 | 0 | log.warn("unable to read/write methods for synthetic methods", e); |
| 1064 | 0 | } catch (IntrospectionException e) { |
| 1065 | 0 | log.warn("unable to read/write methods for synthetic methods", e); |
| 1066 | 194 | } |
| 1067 | |
|
| 1068 | 194 | descriptorsCache.put(beanClass, descriptors); |
| 1069 | 194 | return (descriptors); |
| 1070 | |
|
| 1071 | |
} |
| 1072 | |
|
| 1073 | |
|
| 1074 | |
|
| 1075 | |
|
| 1076 | |
|
| 1077 | |
|
| 1078 | |
|
| 1079 | |
|
| 1080 | |
|
| 1081 | |
|
| 1082 | |
|
| 1083 | |
|
| 1084 | |
|
| 1085 | |
|
| 1086 | |
public PropertyDescriptor[] getPropertyDescriptors(Object bean) { |
| 1087 | |
|
| 1088 | 2011 | if (bean == null) { |
| 1089 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 1090 | |
} |
| 1091 | 2011 | return (getPropertyDescriptors(bean.getClass())); |
| 1092 | |
|
| 1093 | |
} |
| 1094 | |
|
| 1095 | |
|
| 1096 | |
|
| 1097 | |
|
| 1098 | |
|
| 1099 | |
|
| 1100 | |
|
| 1101 | |
|
| 1102 | |
|
| 1103 | |
|
| 1104 | |
|
| 1105 | |
|
| 1106 | |
|
| 1107 | |
|
| 1108 | |
|
| 1109 | |
|
| 1110 | |
|
| 1111 | |
|
| 1112 | |
|
| 1113 | |
|
| 1114 | |
|
| 1115 | |
|
| 1116 | |
|
| 1117 | |
|
| 1118 | |
|
| 1119 | |
|
| 1120 | |
|
| 1121 | |
|
| 1122 | |
|
| 1123 | |
|
| 1124 | |
|
| 1125 | |
|
| 1126 | |
public Class getPropertyEditorClass(Object bean, String name) |
| 1127 | |
throws IllegalAccessException, InvocationTargetException, |
| 1128 | |
NoSuchMethodException { |
| 1129 | |
|
| 1130 | 0 | if (bean == null) { |
| 1131 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 1132 | |
} |
| 1133 | 0 | if (name == null) { |
| 1134 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1135 | |
bean.getClass() + "'"); |
| 1136 | |
} |
| 1137 | |
|
| 1138 | 0 | PropertyDescriptor descriptor = |
| 1139 | |
getPropertyDescriptor(bean, name); |
| 1140 | 0 | if (descriptor != null) { |
| 1141 | 0 | return (descriptor.getPropertyEditorClass()); |
| 1142 | |
} else { |
| 1143 | 0 | return (null); |
| 1144 | |
} |
| 1145 | |
|
| 1146 | |
} |
| 1147 | |
|
| 1148 | |
|
| 1149 | |
|
| 1150 | |
|
| 1151 | |
|
| 1152 | |
|
| 1153 | |
|
| 1154 | |
|
| 1155 | |
|
| 1156 | |
|
| 1157 | |
|
| 1158 | |
|
| 1159 | |
|
| 1160 | |
|
| 1161 | |
|
| 1162 | |
|
| 1163 | |
|
| 1164 | |
|
| 1165 | |
|
| 1166 | |
|
| 1167 | |
|
| 1168 | |
|
| 1169 | |
|
| 1170 | |
|
| 1171 | |
|
| 1172 | |
|
| 1173 | |
|
| 1174 | |
public Class getPropertyType(Object bean, String name) |
| 1175 | |
throws IllegalAccessException, InvocationTargetException, |
| 1176 | |
NoSuchMethodException { |
| 1177 | |
|
| 1178 | 43 | if (bean == null) { |
| 1179 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 1180 | |
} |
| 1181 | 43 | if (name == null) { |
| 1182 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1183 | |
bean.getClass() + "'"); |
| 1184 | |
} |
| 1185 | |
|
| 1186 | |
|
| 1187 | 70 | while (resolver.hasNested(name)) { |
| 1188 | 28 | String next = resolver.next(name); |
| 1189 | 28 | Object nestedBean = getProperty(bean, next); |
| 1190 | 28 | if (nestedBean == null) { |
| 1191 | 1 | throw new NestedNullException |
| 1192 | |
("Null property value for '" + next + |
| 1193 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1194 | |
} |
| 1195 | 27 | bean = nestedBean; |
| 1196 | 27 | name = resolver.remove(name); |
| 1197 | 27 | } |
| 1198 | |
|
| 1199 | |
|
| 1200 | 42 | name = resolver.getProperty(name); |
| 1201 | |
|
| 1202 | |
|
| 1203 | 42 | if (bean instanceof DynaBean) { |
| 1204 | 4 | DynaProperty descriptor = |
| 1205 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 1206 | 4 | if (descriptor == null) { |
| 1207 | 0 | return (null); |
| 1208 | |
} |
| 1209 | 4 | Class type = descriptor.getType(); |
| 1210 | 4 | if (type == null) { |
| 1211 | 0 | return (null); |
| 1212 | 4 | } else if (type.isArray()) { |
| 1213 | 0 | return (type.getComponentType()); |
| 1214 | |
} else { |
| 1215 | 4 | return (type); |
| 1216 | |
} |
| 1217 | |
} |
| 1218 | |
|
| 1219 | 38 | PropertyDescriptor descriptor = |
| 1220 | |
getPropertyDescriptor(bean, name); |
| 1221 | 38 | if (descriptor == null) { |
| 1222 | 0 | return (null); |
| 1223 | 38 | } else if (descriptor instanceof IndexedPropertyDescriptor) { |
| 1224 | 6 | return (((IndexedPropertyDescriptor) descriptor). |
| 1225 | |
getIndexedPropertyType()); |
| 1226 | 32 | } else if (descriptor instanceof MappedPropertyDescriptor) { |
| 1227 | 4 | return (((MappedPropertyDescriptor) descriptor). |
| 1228 | |
getMappedPropertyType()); |
| 1229 | |
} else { |
| 1230 | 28 | return (descriptor.getPropertyType()); |
| 1231 | |
} |
| 1232 | |
|
| 1233 | |
} |
| 1234 | |
|
| 1235 | |
|
| 1236 | |
|
| 1237 | |
|
| 1238 | |
|
| 1239 | |
|
| 1240 | |
|
| 1241 | |
|
| 1242 | |
|
| 1243 | |
|
| 1244 | |
|
| 1245 | |
public Method getReadMethod(PropertyDescriptor descriptor) { |
| 1246 | |
|
| 1247 | 49 | return (MethodUtils.getAccessibleMethod(descriptor.getReadMethod())); |
| 1248 | |
|
| 1249 | |
} |
| 1250 | |
|
| 1251 | |
|
| 1252 | |
|
| 1253 | |
|
| 1254 | |
|
| 1255 | |
|
| 1256 | |
|
| 1257 | |
|
| 1258 | |
|
| 1259 | |
|
| 1260 | |
|
| 1261 | |
|
| 1262 | |
Method getReadMethod(Class clazz, PropertyDescriptor descriptor) { |
| 1263 | 782 | return (MethodUtils.getAccessibleMethod(clazz, descriptor.getReadMethod())); |
| 1264 | |
} |
| 1265 | |
|
| 1266 | |
|
| 1267 | |
|
| 1268 | |
|
| 1269 | |
|
| 1270 | |
|
| 1271 | |
|
| 1272 | |
|
| 1273 | |
|
| 1274 | |
|
| 1275 | |
|
| 1276 | |
|
| 1277 | |
|
| 1278 | |
|
| 1279 | |
|
| 1280 | |
|
| 1281 | |
|
| 1282 | |
|
| 1283 | |
|
| 1284 | |
|
| 1285 | |
|
| 1286 | |
public Object getSimpleProperty(Object bean, String name) |
| 1287 | |
throws IllegalAccessException, InvocationTargetException, |
| 1288 | |
NoSuchMethodException { |
| 1289 | |
|
| 1290 | 556 | if (bean == null) { |
| 1291 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 1292 | |
} |
| 1293 | 554 | if (name == null) { |
| 1294 | 3 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1295 | |
bean.getClass() + "'"); |
| 1296 | |
} |
| 1297 | |
|
| 1298 | |
|
| 1299 | 551 | if (resolver.hasNested(name)) { |
| 1300 | 2 | throw new IllegalArgumentException |
| 1301 | |
("Nested property names are not allowed: Property '" + |
| 1302 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1303 | 549 | } else if (resolver.isIndexed(name)) { |
| 1304 | 2 | throw new IllegalArgumentException |
| 1305 | |
("Indexed property names are not allowed: Property '" + |
| 1306 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1307 | 547 | } else if (resolver.isMapped(name)) { |
| 1308 | 0 | throw new IllegalArgumentException |
| 1309 | |
("Mapped property names are not allowed: Property '" + |
| 1310 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1311 | |
} |
| 1312 | |
|
| 1313 | |
|
| 1314 | 547 | if (bean instanceof DynaBean) { |
| 1315 | 107 | DynaProperty descriptor = |
| 1316 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 1317 | 107 | if (descriptor == null) { |
| 1318 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 1319 | |
name + "' on dynaclass '" + |
| 1320 | |
((DynaBean) bean).getDynaClass() + "'" ); |
| 1321 | |
} |
| 1322 | 106 | return (((DynaBean) bean).get(name)); |
| 1323 | |
} |
| 1324 | |
|
| 1325 | |
|
| 1326 | 440 | PropertyDescriptor descriptor = |
| 1327 | |
getPropertyDescriptor(bean, name); |
| 1328 | 440 | if (descriptor == null) { |
| 1329 | 8 | throw new NoSuchMethodException("Unknown property '" + |
| 1330 | |
name + "' on class '" + bean.getClass() + "'" ); |
| 1331 | |
} |
| 1332 | 432 | Method readMethod = getReadMethod(bean.getClass(), descriptor); |
| 1333 | 432 | if (readMethod == null) { |
| 1334 | 12 | throw new NoSuchMethodException("Property '" + name + |
| 1335 | |
"' has no getter method in class '" + bean.getClass() + "'"); |
| 1336 | |
} |
| 1337 | |
|
| 1338 | |
|
| 1339 | 420 | Object value = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); |
| 1340 | 420 | return (value); |
| 1341 | |
|
| 1342 | |
} |
| 1343 | |
|
| 1344 | |
|
| 1345 | |
|
| 1346 | |
|
| 1347 | |
|
| 1348 | |
|
| 1349 | |
|
| 1350 | |
|
| 1351 | |
|
| 1352 | |
|
| 1353 | |
|
| 1354 | |
public Method getWriteMethod(PropertyDescriptor descriptor) { |
| 1355 | |
|
| 1356 | 39 | return (MethodUtils.getAccessibleMethod(descriptor.getWriteMethod())); |
| 1357 | |
|
| 1358 | |
} |
| 1359 | |
|
| 1360 | |
|
| 1361 | |
|
| 1362 | |
|
| 1363 | |
|
| 1364 | |
|
| 1365 | |
|
| 1366 | |
|
| 1367 | |
|
| 1368 | |
|
| 1369 | |
|
| 1370 | |
|
| 1371 | |
Method getWriteMethod(Class clazz, PropertyDescriptor descriptor) { |
| 1372 | 560 | return (MethodUtils.getAccessibleMethod(clazz, descriptor.getWriteMethod())); |
| 1373 | |
} |
| 1374 | |
|
| 1375 | |
|
| 1376 | |
|
| 1377 | |
|
| 1378 | |
|
| 1379 | |
|
| 1380 | |
|
| 1381 | |
|
| 1382 | |
|
| 1383 | |
|
| 1384 | |
|
| 1385 | |
|
| 1386 | |
|
| 1387 | |
|
| 1388 | |
|
| 1389 | |
|
| 1390 | |
|
| 1391 | |
public boolean isReadable(Object bean, String name) { |
| 1392 | |
|
| 1393 | |
|
| 1394 | 228 | if (bean == null) { |
| 1395 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 1396 | |
} |
| 1397 | 228 | if (name == null) { |
| 1398 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1399 | |
bean.getClass() + "'"); |
| 1400 | |
} |
| 1401 | |
|
| 1402 | |
|
| 1403 | 237 | while (resolver.hasNested(name)) { |
| 1404 | 10 | String next = resolver.next(name); |
| 1405 | 10 | Object nestedBean = null; |
| 1406 | |
try { |
| 1407 | 10 | nestedBean = getProperty(bean, next); |
| 1408 | 0 | } catch (IllegalAccessException e) { |
| 1409 | 0 | return false; |
| 1410 | 0 | } catch (InvocationTargetException e) { |
| 1411 | 0 | return false; |
| 1412 | 0 | } catch (NoSuchMethodException e) { |
| 1413 | 0 | return false; |
| 1414 | 10 | } |
| 1415 | 10 | if (nestedBean == null) { |
| 1416 | 1 | throw new NestedNullException |
| 1417 | |
("Null property value for '" + next + |
| 1418 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1419 | |
} |
| 1420 | 9 | bean = nestedBean; |
| 1421 | 9 | name = resolver.remove(name); |
| 1422 | 9 | } |
| 1423 | |
|
| 1424 | |
|
| 1425 | 227 | name = resolver.getProperty(name); |
| 1426 | |
|
| 1427 | |
|
| 1428 | |
|
| 1429 | 227 | if (bean instanceof WrapDynaBean) { |
| 1430 | 20 | bean = ((WrapDynaBean)bean).getInstance(); |
| 1431 | |
} |
| 1432 | |
|
| 1433 | |
|
| 1434 | 227 | if (bean instanceof DynaBean) { |
| 1435 | |
|
| 1436 | 84 | return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null); |
| 1437 | |
} else { |
| 1438 | |
try { |
| 1439 | 143 | PropertyDescriptor desc = |
| 1440 | |
getPropertyDescriptor(bean, name); |
| 1441 | 143 | if (desc != null) { |
| 1442 | 143 | Method readMethod = getReadMethod(bean.getClass(), desc); |
| 1443 | 143 | if (readMethod == null) { |
| 1444 | 36 | if (desc instanceof IndexedPropertyDescriptor) { |
| 1445 | 20 | readMethod = ((IndexedPropertyDescriptor) desc).getIndexedReadMethod(); |
| 1446 | 16 | } else if (desc instanceof MappedPropertyDescriptor) { |
| 1447 | 8 | readMethod = ((MappedPropertyDescriptor) desc).getMappedReadMethod(); |
| 1448 | |
} |
| 1449 | 36 | readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod); |
| 1450 | |
} |
| 1451 | 143 | return (readMethod != null); |
| 1452 | |
} else { |
| 1453 | 0 | return (false); |
| 1454 | |
} |
| 1455 | 0 | } catch (IllegalAccessException e) { |
| 1456 | 0 | return (false); |
| 1457 | 0 | } catch (InvocationTargetException e) { |
| 1458 | 0 | return (false); |
| 1459 | 0 | } catch (NoSuchMethodException e) { |
| 1460 | 0 | return (false); |
| 1461 | |
} |
| 1462 | |
} |
| 1463 | |
|
| 1464 | |
} |
| 1465 | |
|
| 1466 | |
|
| 1467 | |
|
| 1468 | |
|
| 1469 | |
|
| 1470 | |
|
| 1471 | |
|
| 1472 | |
|
| 1473 | |
|
| 1474 | |
|
| 1475 | |
|
| 1476 | |
|
| 1477 | |
|
| 1478 | |
|
| 1479 | |
|
| 1480 | |
|
| 1481 | |
|
| 1482 | |
public boolean isWriteable(Object bean, String name) { |
| 1483 | |
|
| 1484 | |
|
| 1485 | 268 | if (bean == null) { |
| 1486 | 0 | throw new IllegalArgumentException("No bean specified"); |
| 1487 | |
} |
| 1488 | 268 | if (name == null) { |
| 1489 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1490 | |
bean.getClass() + "'"); |
| 1491 | |
} |
| 1492 | |
|
| 1493 | |
|
| 1494 | 277 | while (resolver.hasNested(name)) { |
| 1495 | 10 | String next = resolver.next(name); |
| 1496 | 10 | Object nestedBean = null; |
| 1497 | |
try { |
| 1498 | 10 | nestedBean = getProperty(bean, next); |
| 1499 | 0 | } catch (IllegalAccessException e) { |
| 1500 | 0 | return false; |
| 1501 | 0 | } catch (InvocationTargetException e) { |
| 1502 | 0 | return false; |
| 1503 | 0 | } catch (NoSuchMethodException e) { |
| 1504 | 0 | return false; |
| 1505 | 10 | } |
| 1506 | 10 | if (nestedBean == null) { |
| 1507 | 1 | throw new NestedNullException |
| 1508 | |
("Null property value for '" + next + |
| 1509 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1510 | |
} |
| 1511 | 9 | bean = nestedBean; |
| 1512 | 9 | name = resolver.remove(name); |
| 1513 | 9 | } |
| 1514 | |
|
| 1515 | |
|
| 1516 | 267 | name = resolver.getProperty(name); |
| 1517 | |
|
| 1518 | |
|
| 1519 | |
|
| 1520 | 267 | if (bean instanceof WrapDynaBean) { |
| 1521 | 8 | bean = ((WrapDynaBean)bean).getInstance(); |
| 1522 | |
} |
| 1523 | |
|
| 1524 | |
|
| 1525 | 267 | if (bean instanceof DynaBean) { |
| 1526 | |
|
| 1527 | 90 | return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null); |
| 1528 | |
} else { |
| 1529 | |
try { |
| 1530 | 177 | PropertyDescriptor desc = |
| 1531 | |
getPropertyDescriptor(bean, name); |
| 1532 | 177 | if (desc != null) { |
| 1533 | 151 | Method writeMethod = getWriteMethod(bean.getClass(), desc); |
| 1534 | 151 | if (writeMethod == null) { |
| 1535 | 53 | if (desc instanceof IndexedPropertyDescriptor) { |
| 1536 | 17 | writeMethod = ((IndexedPropertyDescriptor) desc).getIndexedWriteMethod(); |
| 1537 | 36 | } else if (desc instanceof MappedPropertyDescriptor) { |
| 1538 | 10 | writeMethod = ((MappedPropertyDescriptor) desc).getMappedWriteMethod(); |
| 1539 | |
} |
| 1540 | 53 | writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod); |
| 1541 | |
} |
| 1542 | 151 | return (writeMethod != null); |
| 1543 | |
} else { |
| 1544 | 26 | return (false); |
| 1545 | |
} |
| 1546 | 0 | } catch (IllegalAccessException e) { |
| 1547 | 0 | return (false); |
| 1548 | 0 | } catch (InvocationTargetException e) { |
| 1549 | 0 | return (false); |
| 1550 | 0 | } catch (NoSuchMethodException e) { |
| 1551 | 0 | return (false); |
| 1552 | |
} |
| 1553 | |
} |
| 1554 | |
|
| 1555 | |
} |
| 1556 | |
|
| 1557 | |
|
| 1558 | |
|
| 1559 | |
|
| 1560 | |
|
| 1561 | |
|
| 1562 | |
|
| 1563 | |
|
| 1564 | |
|
| 1565 | |
|
| 1566 | |
|
| 1567 | |
|
| 1568 | |
|
| 1569 | |
|
| 1570 | |
|
| 1571 | |
|
| 1572 | |
|
| 1573 | |
|
| 1574 | |
|
| 1575 | |
|
| 1576 | |
|
| 1577 | |
|
| 1578 | |
|
| 1579 | |
|
| 1580 | |
|
| 1581 | |
|
| 1582 | |
|
| 1583 | |
public void setIndexedProperty(Object bean, String name, |
| 1584 | |
Object value) |
| 1585 | |
throws IllegalAccessException, InvocationTargetException, |
| 1586 | |
NoSuchMethodException { |
| 1587 | |
|
| 1588 | 57 | if (bean == null) { |
| 1589 | 4 | throw new IllegalArgumentException("No bean specified"); |
| 1590 | |
} |
| 1591 | 53 | if (name == null) { |
| 1592 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1593 | |
bean.getClass() + "'"); |
| 1594 | |
} |
| 1595 | |
|
| 1596 | |
|
| 1597 | 53 | int index = -1; |
| 1598 | |
try { |
| 1599 | 53 | index = resolver.getIndex(name); |
| 1600 | 0 | } catch (IllegalArgumentException e) { |
| 1601 | 0 | throw new IllegalArgumentException("Invalid indexed property '" + |
| 1602 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1603 | 53 | } |
| 1604 | 53 | if (index < 0) { |
| 1605 | 4 | throw new IllegalArgumentException("Invalid indexed property '" + |
| 1606 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1607 | |
} |
| 1608 | |
|
| 1609 | |
|
| 1610 | 49 | name = resolver.getProperty(name); |
| 1611 | |
|
| 1612 | |
|
| 1613 | 49 | setIndexedProperty(bean, name, index, value); |
| 1614 | |
|
| 1615 | 43 | } |
| 1616 | |
|
| 1617 | |
|
| 1618 | |
|
| 1619 | |
|
| 1620 | |
|
| 1621 | |
|
| 1622 | |
|
| 1623 | |
|
| 1624 | |
|
| 1625 | |
|
| 1626 | |
|
| 1627 | |
|
| 1628 | |
|
| 1629 | |
|
| 1630 | |
|
| 1631 | |
|
| 1632 | |
|
| 1633 | |
|
| 1634 | |
|
| 1635 | |
|
| 1636 | |
|
| 1637 | |
|
| 1638 | |
|
| 1639 | |
|
| 1640 | |
public void setIndexedProperty(Object bean, String name, |
| 1641 | |
int index, Object value) |
| 1642 | |
throws IllegalAccessException, InvocationTargetException, |
| 1643 | |
NoSuchMethodException { |
| 1644 | |
|
| 1645 | 117 | if (bean == null) { |
| 1646 | 4 | throw new IllegalArgumentException("No bean specified"); |
| 1647 | |
} |
| 1648 | 113 | if (name == null || name.length() == 0) { |
| 1649 | 14 | if (bean.getClass().isArray()) { |
| 1650 | 4 | Array.set(bean, index, value); |
| 1651 | 4 | return; |
| 1652 | 10 | } else if (bean instanceof List) { |
| 1653 | 2 | ((List)bean).set(index, value); |
| 1654 | 2 | return; |
| 1655 | |
} |
| 1656 | |
} |
| 1657 | 107 | if (name == null) { |
| 1658 | 4 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1659 | |
bean.getClass() + "'"); |
| 1660 | |
} |
| 1661 | |
|
| 1662 | |
|
| 1663 | 103 | if (bean instanceof DynaBean) { |
| 1664 | 32 | DynaProperty descriptor = |
| 1665 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 1666 | 32 | if (descriptor == null) { |
| 1667 | 2 | throw new NoSuchMethodException("Unknown property '" + |
| 1668 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1669 | |
} |
| 1670 | 30 | ((DynaBean) bean).set(name, index, value); |
| 1671 | 19 | return; |
| 1672 | |
} |
| 1673 | |
|
| 1674 | |
|
| 1675 | 71 | PropertyDescriptor descriptor = |
| 1676 | |
getPropertyDescriptor(bean, name); |
| 1677 | 71 | if (descriptor == null) { |
| 1678 | 3 | throw new NoSuchMethodException("Unknown property '" + |
| 1679 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1680 | |
} |
| 1681 | |
|
| 1682 | |
|
| 1683 | 68 | if (descriptor instanceof IndexedPropertyDescriptor) { |
| 1684 | 32 | Method writeMethod = ((IndexedPropertyDescriptor) descriptor). |
| 1685 | |
getIndexedWriteMethod(); |
| 1686 | 32 | writeMethod = MethodUtils.getAccessibleMethod(bean.getClass(), writeMethod); |
| 1687 | 32 | if (writeMethod != null) { |
| 1688 | 30 | Object[] subscript = new Object[2]; |
| 1689 | 30 | subscript[0] = new Integer(index); |
| 1690 | 30 | subscript[1] = value; |
| 1691 | |
try { |
| 1692 | 30 | if (log.isTraceEnabled()) { |
| 1693 | 0 | String valueClassName = |
| 1694 | |
value == null ? "<null>" |
| 1695 | |
: value.getClass().getName(); |
| 1696 | 0 | log.trace("setSimpleProperty: Invoking method " |
| 1697 | |
+ writeMethod +" with index=" + index |
| 1698 | |
+ ", value=" + value |
| 1699 | |
+ " (class " + valueClassName+ ")"); |
| 1700 | |
} |
| 1701 | 30 | invokeMethod(writeMethod, bean, subscript); |
| 1702 | 6 | } catch (InvocationTargetException e) { |
| 1703 | 6 | if (e.getTargetException() instanceof |
| 1704 | |
IndexOutOfBoundsException) { |
| 1705 | 6 | throw (IndexOutOfBoundsException) |
| 1706 | |
e.getTargetException(); |
| 1707 | |
} else { |
| 1708 | 0 | throw e; |
| 1709 | |
} |
| 1710 | 24 | } |
| 1711 | 24 | return; |
| 1712 | |
} |
| 1713 | |
} |
| 1714 | |
|
| 1715 | |
|
| 1716 | 38 | Method readMethod = getReadMethod(bean.getClass(), descriptor); |
| 1717 | 38 | if (readMethod == null) { |
| 1718 | 2 | throw new NoSuchMethodException("Property '" + name + |
| 1719 | |
"' has no getter method on bean class '" + bean.getClass() + "'"); |
| 1720 | |
} |
| 1721 | |
|
| 1722 | |
|
| 1723 | 36 | Object array = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); |
| 1724 | 36 | if (!array.getClass().isArray()) { |
| 1725 | 5 | if (array instanceof List) { |
| 1726 | |
|
| 1727 | 5 | ((List) array).set(index, value); |
| 1728 | |
} else { |
| 1729 | 0 | throw new IllegalArgumentException("Property '" + name + |
| 1730 | |
"' is not indexed on bean class '" + bean.getClass() + "'"); |
| 1731 | |
} |
| 1732 | |
} else { |
| 1733 | |
|
| 1734 | 31 | Array.set(array, index, value); |
| 1735 | |
} |
| 1736 | |
|
| 1737 | 29 | } |
| 1738 | |
|
| 1739 | |
|
| 1740 | |
|
| 1741 | |
|
| 1742 | |
|
| 1743 | |
|
| 1744 | |
|
| 1745 | |
|
| 1746 | |
|
| 1747 | |
|
| 1748 | |
|
| 1749 | |
|
| 1750 | |
|
| 1751 | |
|
| 1752 | |
|
| 1753 | |
|
| 1754 | |
|
| 1755 | |
|
| 1756 | |
|
| 1757 | |
|
| 1758 | |
|
| 1759 | |
public void setMappedProperty(Object bean, String name, |
| 1760 | |
Object value) |
| 1761 | |
throws IllegalAccessException, InvocationTargetException, |
| 1762 | |
NoSuchMethodException { |
| 1763 | |
|
| 1764 | 27 | if (bean == null) { |
| 1765 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 1766 | |
} |
| 1767 | 25 | if (name == null) { |
| 1768 | 0 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1769 | |
bean.getClass() + "'"); |
| 1770 | |
} |
| 1771 | |
|
| 1772 | |
|
| 1773 | 25 | String key = null; |
| 1774 | |
try { |
| 1775 | 25 | key = resolver.getKey(name); |
| 1776 | 0 | } catch (IllegalArgumentException e) { |
| 1777 | 0 | throw new IllegalArgumentException |
| 1778 | |
("Invalid mapped property '" + name + |
| 1779 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1780 | 25 | } |
| 1781 | 25 | if (key == null) { |
| 1782 | 2 | throw new IllegalArgumentException |
| 1783 | |
("Invalid mapped property '" + name + |
| 1784 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1785 | |
} |
| 1786 | |
|
| 1787 | |
|
| 1788 | 23 | name = resolver.getProperty(name); |
| 1789 | |
|
| 1790 | |
|
| 1791 | 23 | setMappedProperty(bean, name, key, value); |
| 1792 | |
|
| 1793 | 19 | } |
| 1794 | |
|
| 1795 | |
|
| 1796 | |
|
| 1797 | |
|
| 1798 | |
|
| 1799 | |
|
| 1800 | |
|
| 1801 | |
|
| 1802 | |
|
| 1803 | |
|
| 1804 | |
|
| 1805 | |
|
| 1806 | |
|
| 1807 | |
|
| 1808 | |
|
| 1809 | |
|
| 1810 | |
|
| 1811 | |
|
| 1812 | |
public void setMappedProperty(Object bean, String name, |
| 1813 | |
String key, Object value) |
| 1814 | |
throws IllegalAccessException, InvocationTargetException, |
| 1815 | |
NoSuchMethodException { |
| 1816 | |
|
| 1817 | 40 | if (bean == null) { |
| 1818 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 1819 | |
} |
| 1820 | 38 | if (name == null) { |
| 1821 | 2 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1822 | |
bean.getClass() + "'"); |
| 1823 | |
} |
| 1824 | 36 | if (key == null) { |
| 1825 | 2 | throw new IllegalArgumentException("No key specified for property '" + |
| 1826 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1827 | |
} |
| 1828 | |
|
| 1829 | |
|
| 1830 | 34 | if (bean instanceof DynaBean) { |
| 1831 | 9 | DynaProperty descriptor = |
| 1832 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 1833 | 9 | if (descriptor == null) { |
| 1834 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 1835 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1836 | |
} |
| 1837 | 8 | ((DynaBean) bean).set(name, key, value); |
| 1838 | 7 | return; |
| 1839 | |
} |
| 1840 | |
|
| 1841 | |
|
| 1842 | 25 | PropertyDescriptor descriptor = |
| 1843 | |
getPropertyDescriptor(bean, name); |
| 1844 | 25 | if (descriptor == null) { |
| 1845 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 1846 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 1847 | |
} |
| 1848 | |
|
| 1849 | 24 | if (descriptor instanceof MappedPropertyDescriptor) { |
| 1850 | |
|
| 1851 | 20 | Method mappedWriteMethod = |
| 1852 | |
((MappedPropertyDescriptor) descriptor). |
| 1853 | |
getMappedWriteMethod(); |
| 1854 | 20 | mappedWriteMethod = MethodUtils.getAccessibleMethod(bean.getClass(), mappedWriteMethod); |
| 1855 | 20 | if (mappedWriteMethod != null) { |
| 1856 | 18 | Object[] params = new Object[2]; |
| 1857 | 18 | params[0] = key; |
| 1858 | 18 | params[1] = value; |
| 1859 | 18 | if (log.isTraceEnabled()) { |
| 1860 | 0 | String valueClassName = |
| 1861 | |
value == null ? "<null>" : value.getClass().getName(); |
| 1862 | 0 | log.trace("setSimpleProperty: Invoking method " |
| 1863 | |
+ mappedWriteMethod + " with key=" + key |
| 1864 | |
+ ", value=" + value |
| 1865 | |
+ " (class " + valueClassName +")"); |
| 1866 | |
} |
| 1867 | 18 | invokeMethod(mappedWriteMethod, bean, params); |
| 1868 | 18 | } else { |
| 1869 | 2 | throw new NoSuchMethodException |
| 1870 | |
("Property '" + name + "' has no mapped setter method" + |
| 1871 | |
"on bean class '" + bean.getClass() + "'"); |
| 1872 | |
} |
| 1873 | 18 | } else { |
| 1874 | |
|
| 1875 | 4 | Method readMethod = getReadMethod(bean.getClass(), descriptor); |
| 1876 | 4 | if (readMethod != null) { |
| 1877 | 4 | Object invokeResult = invokeMethod(readMethod, bean, EMPTY_OBJECT_ARRAY); |
| 1878 | |
|
| 1879 | 4 | if (invokeResult instanceof java.util.Map) { |
| 1880 | 4 | ((java.util.Map)invokeResult).put(key, value); |
| 1881 | |
} |
| 1882 | 4 | } else { |
| 1883 | 0 | throw new NoSuchMethodException("Property '" + name + |
| 1884 | |
"' has no mapped getter method on bean class '" + |
| 1885 | |
bean.getClass() + "'"); |
| 1886 | |
} |
| 1887 | |
} |
| 1888 | |
|
| 1889 | 22 | } |
| 1890 | |
|
| 1891 | |
|
| 1892 | |
|
| 1893 | |
|
| 1894 | |
|
| 1895 | |
|
| 1896 | |
|
| 1897 | |
|
| 1898 | |
|
| 1899 | |
|
| 1900 | |
|
| 1901 | |
|
| 1902 | |
|
| 1903 | |
|
| 1904 | |
|
| 1905 | |
|
| 1906 | |
|
| 1907 | |
|
| 1908 | |
|
| 1909 | |
|
| 1910 | |
|
| 1911 | |
|
| 1912 | |
|
| 1913 | |
|
| 1914 | |
|
| 1915 | |
|
| 1916 | |
|
| 1917 | |
|
| 1918 | |
|
| 1919 | |
|
| 1920 | |
|
| 1921 | |
|
| 1922 | |
public void setNestedProperty(Object bean, |
| 1923 | |
String name, Object value) |
| 1924 | |
throws IllegalAccessException, InvocationTargetException, |
| 1925 | |
NoSuchMethodException { |
| 1926 | |
|
| 1927 | 310 | if (bean == null) { |
| 1928 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 1929 | |
} |
| 1930 | 308 | if (name == null) { |
| 1931 | 2 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 1932 | |
bean.getClass() + "'"); |
| 1933 | |
} |
| 1934 | |
|
| 1935 | |
|
| 1936 | 344 | while (resolver.hasNested(name)) { |
| 1937 | 40 | String next = resolver.next(name); |
| 1938 | 40 | Object nestedBean = null; |
| 1939 | 40 | if (bean instanceof Map) { |
| 1940 | 1 | nestedBean = getPropertyOfMapBean((Map)bean, next); |
| 1941 | 39 | } else if (resolver.isMapped(next)) { |
| 1942 | 4 | nestedBean = getMappedProperty(bean, next); |
| 1943 | 35 | } else if (resolver.isIndexed(next)) { |
| 1944 | 7 | nestedBean = getIndexedProperty(bean, next); |
| 1945 | |
} else { |
| 1946 | 28 | nestedBean = getSimpleProperty(bean, next); |
| 1947 | |
} |
| 1948 | 40 | if (nestedBean == null) { |
| 1949 | 2 | throw new NestedNullException |
| 1950 | |
("Null property value for '" + name + |
| 1951 | |
"' on bean class '" + bean.getClass() + "'"); |
| 1952 | |
} |
| 1953 | 38 | bean = nestedBean; |
| 1954 | 38 | name = resolver.remove(name); |
| 1955 | 38 | } |
| 1956 | |
|
| 1957 | 304 | if (bean instanceof Map) { |
| 1958 | 13 | setPropertyOfMapBean((Map) bean, name, value); |
| 1959 | 291 | } else if (resolver.isMapped(name)) { |
| 1960 | 19 | setMappedProperty(bean, name, value); |
| 1961 | 272 | } else if (resolver.isIndexed(name)) { |
| 1962 | 34 | setIndexedProperty(bean, name, value); |
| 1963 | |
} else { |
| 1964 | 238 | setSimpleProperty(bean, name, value); |
| 1965 | |
} |
| 1966 | |
|
| 1967 | 282 | } |
| 1968 | |
|
| 1969 | |
|
| 1970 | |
|
| 1971 | |
|
| 1972 | |
|
| 1973 | |
|
| 1974 | |
|
| 1975 | |
|
| 1976 | |
|
| 1977 | |
|
| 1978 | |
|
| 1979 | |
|
| 1980 | |
|
| 1981 | |
|
| 1982 | |
|
| 1983 | |
|
| 1984 | |
|
| 1985 | |
|
| 1986 | |
|
| 1987 | |
|
| 1988 | |
|
| 1989 | |
|
| 1990 | |
|
| 1991 | |
|
| 1992 | |
|
| 1993 | |
|
| 1994 | |
|
| 1995 | |
|
| 1996 | |
|
| 1997 | |
|
| 1998 | |
|
| 1999 | |
|
| 2000 | |
|
| 2001 | |
|
| 2002 | |
|
| 2003 | |
|
| 2004 | |
|
| 2005 | |
|
| 2006 | |
|
| 2007 | |
|
| 2008 | |
|
| 2009 | |
|
| 2010 | |
|
| 2011 | |
|
| 2012 | |
|
| 2013 | |
|
| 2014 | |
|
| 2015 | |
|
| 2016 | |
|
| 2017 | |
|
| 2018 | |
|
| 2019 | |
|
| 2020 | |
|
| 2021 | |
|
| 2022 | |
|
| 2023 | |
|
| 2024 | |
|
| 2025 | |
protected void setPropertyOfMapBean(Map bean, String propertyName, Object value) |
| 2026 | |
throws IllegalArgumentException, IllegalAccessException, |
| 2027 | |
InvocationTargetException, NoSuchMethodException { |
| 2028 | |
|
| 2029 | 11 | if (resolver.isMapped(propertyName)) { |
| 2030 | 6 | String name = resolver.getProperty(propertyName); |
| 2031 | 6 | if (name == null || name.length() == 0) { |
| 2032 | 5 | propertyName = resolver.getKey(propertyName); |
| 2033 | |
} |
| 2034 | |
} |
| 2035 | |
|
| 2036 | 11 | if (resolver.isIndexed(propertyName) || |
| 2037 | |
resolver.isMapped(propertyName)) { |
| 2038 | 1 | throw new IllegalArgumentException( |
| 2039 | |
"Indexed or mapped properties are not supported on" |
| 2040 | |
+ " objects of type Map: " + propertyName); |
| 2041 | |
} |
| 2042 | |
|
| 2043 | 10 | bean.put(propertyName, value); |
| 2044 | 10 | } |
| 2045 | |
|
| 2046 | |
|
| 2047 | |
|
| 2048 | |
|
| 2049 | |
|
| 2050 | |
|
| 2051 | |
|
| 2052 | |
|
| 2053 | |
|
| 2054 | |
|
| 2055 | |
|
| 2056 | |
|
| 2057 | |
|
| 2058 | |
|
| 2059 | |
|
| 2060 | |
|
| 2061 | |
|
| 2062 | |
|
| 2063 | |
|
| 2064 | |
|
| 2065 | |
|
| 2066 | |
|
| 2067 | |
public void setProperty(Object bean, String name, Object value) |
| 2068 | |
throws IllegalAccessException, InvocationTargetException, |
| 2069 | |
NoSuchMethodException { |
| 2070 | |
|
| 2071 | 278 | setNestedProperty(bean, name, value); |
| 2072 | |
|
| 2073 | 259 | } |
| 2074 | |
|
| 2075 | |
|
| 2076 | |
|
| 2077 | |
|
| 2078 | |
|
| 2079 | |
|
| 2080 | |
|
| 2081 | |
|
| 2082 | |
|
| 2083 | |
|
| 2084 | |
|
| 2085 | |
|
| 2086 | |
|
| 2087 | |
|
| 2088 | |
|
| 2089 | |
|
| 2090 | |
|
| 2091 | |
|
| 2092 | |
|
| 2093 | |
|
| 2094 | |
|
| 2095 | |
public void setSimpleProperty(Object bean, |
| 2096 | |
String name, Object value) |
| 2097 | |
throws IllegalAccessException, InvocationTargetException, |
| 2098 | |
NoSuchMethodException { |
| 2099 | |
|
| 2100 | 521 | if (bean == null) { |
| 2101 | 2 | throw new IllegalArgumentException("No bean specified"); |
| 2102 | |
} |
| 2103 | 519 | if (name == null) { |
| 2104 | 2 | throw new IllegalArgumentException("No name specified for bean class '" + |
| 2105 | |
bean.getClass() + "'"); |
| 2106 | |
} |
| 2107 | |
|
| 2108 | |
|
| 2109 | 517 | if (resolver.hasNested(name)) { |
| 2110 | 2 | throw new IllegalArgumentException |
| 2111 | |
("Nested property names are not allowed: Property '" + |
| 2112 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 2113 | 515 | } else if (resolver.isIndexed(name)) { |
| 2114 | 2 | throw new IllegalArgumentException |
| 2115 | |
("Indexed property names are not allowed: Property '" + |
| 2116 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 2117 | 513 | } else if (resolver.isMapped(name)) { |
| 2118 | 0 | throw new IllegalArgumentException |
| 2119 | |
("Mapped property names are not allowed: Property '" + |
| 2120 | |
name + "' on bean class '" + bean.getClass() + "'"); |
| 2121 | |
} |
| 2122 | |
|
| 2123 | |
|
| 2124 | 513 | if (bean instanceof DynaBean) { |
| 2125 | 99 | DynaProperty descriptor = |
| 2126 | |
((DynaBean) bean).getDynaClass().getDynaProperty(name); |
| 2127 | 99 | if (descriptor == null) { |
| 2128 | 1 | throw new NoSuchMethodException("Unknown property '" + |
| 2129 | |
name + "' on dynaclass '" + |
| 2130 | |
((DynaBean) bean).getDynaClass() + "'" ); |
| 2131 | |
} |
| 2132 | 98 | ((DynaBean) bean).set(name, value); |
| 2133 | 97 | return; |
| 2134 | |
} |
| 2135 | |
|
| 2136 | |
|
| 2137 | 414 | PropertyDescriptor descriptor = |
| 2138 | |
getPropertyDescriptor(bean, name); |
| 2139 | 414 | if (descriptor == null) { |
| 2140 | 5 | throw new NoSuchMethodException("Unknown property '" + |
| 2141 | |
name + "' on class '" + bean.getClass() + "'" ); |
| 2142 | |
} |
| 2143 | 409 | Method writeMethod = getWriteMethod(bean.getClass(), descriptor); |
| 2144 | 409 | if (writeMethod == null) { |
| 2145 | 7 | throw new NoSuchMethodException("Property '" + name + |
| 2146 | |
"' has no setter method in class '" + bean.getClass() + "'"); |
| 2147 | |
} |
| 2148 | |
|
| 2149 | |
|
| 2150 | 402 | Object[] values = new Object[1]; |
| 2151 | 402 | values[0] = value; |
| 2152 | 402 | if (log.isTraceEnabled()) { |
| 2153 | 0 | String valueClassName = |
| 2154 | |
value == null ? "<null>" : value.getClass().getName(); |
| 2155 | 0 | log.trace("setSimpleProperty: Invoking method " + writeMethod |
| 2156 | |
+ " with value " + value + " (class " + valueClassName + ")"); |
| 2157 | |
} |
| 2158 | 402 | invokeMethod(writeMethod, bean, values); |
| 2159 | |
|
| 2160 | 391 | } |
| 2161 | |
|
| 2162 | |
|
| 2163 | |
private Object invokeMethod( |
| 2164 | |
Method method, |
| 2165 | |
Object bean, |
| 2166 | |
Object[] values) |
| 2167 | |
throws |
| 2168 | |
IllegalAccessException, |
| 2169 | |
InvocationTargetException { |
| 2170 | 1138 | if(bean == null) { |
| 2171 | 0 | throw new IllegalArgumentException("No bean specified " + |
| 2172 | |
"- this should have been checked before reaching this method"); |
| 2173 | |
} |
| 2174 | |
|
| 2175 | |
try { |
| 2176 | |
|
| 2177 | 1138 | return method.invoke(bean, values); |
| 2178 | |
|
| 2179 | 0 | } catch (NullPointerException cause) { |
| 2180 | |
|
| 2181 | |
|
| 2182 | 0 | String valueString = ""; |
| 2183 | 0 | if (values != null) { |
| 2184 | 0 | for (int i = 0; i < values.length; i++) { |
| 2185 | 0 | if (i>0) { |
| 2186 | 0 | valueString += ", " ; |
| 2187 | |
} |
| 2188 | 0 | if (values[i] == null) { |
| 2189 | 0 | valueString += "<null>"; |
| 2190 | |
} else { |
| 2191 | 0 | valueString += (values[i]).getClass().getName(); |
| 2192 | |
} |
| 2193 | |
} |
| 2194 | |
} |
| 2195 | 0 | String expectedString = ""; |
| 2196 | 0 | Class[] parTypes = method.getParameterTypes(); |
| 2197 | 0 | if (parTypes != null) { |
| 2198 | 0 | for (int i = 0; i < parTypes.length; i++) { |
| 2199 | 0 | if (i > 0) { |
| 2200 | 0 | expectedString += ", "; |
| 2201 | |
} |
| 2202 | 0 | expectedString += parTypes[i].getName(); |
| 2203 | |
} |
| 2204 | |
} |
| 2205 | 0 | IllegalArgumentException e = new IllegalArgumentException( |
| 2206 | |
"Cannot invoke " + method.getDeclaringClass().getName() + "." |
| 2207 | |
+ method.getName() + " on bean class '" + bean.getClass() + |
| 2208 | |
"' - " + cause.getMessage() |
| 2209 | |
|
| 2210 | |
+ " - had objects of type \"" + valueString |
| 2211 | |
+ "\" but expected signature \"" |
| 2212 | |
+ expectedString + "\"" |
| 2213 | |
); |
| 2214 | 0 | if (!BeanUtils.initCause(e, cause)) { |
| 2215 | 0 | log.error("Method invocation failed", cause); |
| 2216 | |
} |
| 2217 | 0 | throw e; |
| 2218 | 11 | } catch (IllegalArgumentException cause) { |
| 2219 | 11 | String valueString = ""; |
| 2220 | 11 | if (values != null) { |
| 2221 | 22 | for (int i = 0; i < values.length; i++) { |
| 2222 | 11 | if (i>0) { |
| 2223 | 0 | valueString += ", " ; |
| 2224 | |
} |
| 2225 | 11 | if (values[i] == null) { |
| 2226 | 2 | valueString += "<null>"; |
| 2227 | |
} else { |
| 2228 | 9 | valueString += (values[i]).getClass().getName(); |
| 2229 | |
} |
| 2230 | |
} |
| 2231 | |
} |
| 2232 | 11 | String expectedString = ""; |
| 2233 | 11 | Class[] parTypes = method.getParameterTypes(); |
| 2234 | 11 | if (parTypes != null) { |
| 2235 | 22 | for (int i = 0; i < parTypes.length; i++) { |
| 2236 | 11 | if (i > 0) { |
| 2237 | 0 | expectedString += ", "; |
| 2238 | |
} |
| 2239 | 11 | expectedString += parTypes[i].getName(); |
| 2240 | |
} |
| 2241 | |
} |
| 2242 | 11 | IllegalArgumentException e = new IllegalArgumentException( |
| 2243 | |
"Cannot invoke " + method.getDeclaringClass().getName() + "." |
| 2244 | |
+ method.getName() + " on bean class '" + bean.getClass() + |
| 2245 | |
"' - " + cause.getMessage() |
| 2246 | |
|
| 2247 | |
+ " - had objects of type \"" + valueString |
| 2248 | |
+ "\" but expected signature \"" |
| 2249 | |
+ expectedString + "\"" |
| 2250 | |
); |
| 2251 | 11 | if (!BeanUtils.initCause(e, cause)) { |
| 2252 | 0 | log.error("Method invocation failed", cause); |
| 2253 | |
} |
| 2254 | 11 | throw e; |
| 2255 | |
|
| 2256 | |
} |
| 2257 | |
} |
| 2258 | |
} |