1 3 package jodd.bean; 4 5 import java.lang.reflect.Method ; 6 import java.lang.reflect.Array ; 7 import java.lang.reflect.Field ; 8 import java.util.Map ; 9 import java.util.List ; 10 11 39 public class BeanUtil { 40 41 43 47 protected static void resolveNestedProperties(BeanProperty beanProperty, boolean forced) { 48 String name = beanProperty.name; 49 int dotNdx; 50 while ((dotNdx = name.indexOf('.')) != -1) { 51 beanProperty.last = false; 52 beanProperty.name = name.substring(0, dotNdx); 53 beanProperty.setBean(getIndexProperty(beanProperty, true, forced)); 54 name = name.substring(dotNdx + 1); 55 } 56 beanProperty.last = true; 57 beanProperty.name = name; 58 } 59 60 protected static boolean resolveExistingNestedProperties(BeanProperty beanProperty) { 61 String name = beanProperty.name; 62 int dotNdx; 63 while ((dotNdx = name.indexOf('.')) != -1) { 64 beanProperty.last = false; 65 String temp = beanProperty.name = name.substring(0, dotNdx); 66 if (hasIndexProperty(beanProperty, true) == false) { 67 return false; 68 } 69 beanProperty.name = temp; 70 beanProperty.setBean(getIndexProperty(beanProperty, true, false)); 71 name = name.substring(dotNdx + 1); 72 } 73 beanProperty.last = true; 74 beanProperty.name = name; 75 return true; 76 } 77 78 79 81 84 public static boolean hasSimpleProperty(Object bean, String property, boolean suppressSecurity) { 85 return hasSimpleProperty(new BeanProperty(bean, property), suppressSecurity); 86 } 87 public static boolean hasSimpleProperty(BeanProperty bp, boolean suppressSecurity) { 88 if (bp.bean == null) { 89 return false; 90 } 91 92 Method method = bp.cd.getBeanGetter(bp.name, suppressSecurity); 94 if (method != null) { 95 return true; 96 } 97 98 Field field = bp.cd.getField(bp.name, suppressSecurity); 100 if (field != null) { 101 return true; 102 } 103 104 if (bp.cd.isMap()) { 106 Map map = (Map ) bp.bean; 107 if (map.containsKey(bp.name) == true) { 108 return true; 109 } 110 } 111 112 return false; 113 } 114 115 116 117 120 public static Object getSimpleProperty(Object bean, String property, boolean suppressSecurity) { 121 return getSimpleProperty(new BeanProperty(bean, property), suppressSecurity); 122 } 123 124 protected static Object getSimpleProperty(BeanProperty bp, boolean suppressSecurity) { 125 126 Method method = bp.cd.getBeanGetter(bp.name, suppressSecurity); 128 if (method != null) { 129 return BeanPropertyUtil.invokeGetter(bp.bean, method); 130 } 131 132 Field field = bp.cd.getField(bp.name, suppressSecurity); 134 if (field != null) { 135 return BeanPropertyUtil.getField(bp.bean, field); 136 } 137 138 if (bp.cd.isMap()) { 140 Map map = (Map ) bp.bean; 141 if (map.containsKey(bp.name) == false) { 142 throw new BeanException("Unable to read simple map property '" + bp + '\''); 143 } 144 return map.get(bp.name); 145 } 146 147 throw new BeanException("Unable to read simple property '" + bp + '\''); 149 } 150 151 154 public static Object getSimplePropertyForced(Object bean, String property, boolean suppressSecurity) { 155 return getSimplePropertyForced(new BeanProperty(bean, property), suppressSecurity); 156 } 157 158 protected static Object getSimplePropertyForced(BeanProperty bp, boolean suppressSecurity) { 159 160 Method method = bp.cd.getBeanGetter(bp.name, suppressSecurity); 162 if (method != null) { 163 Object result = BeanPropertyUtil.invokeGetter(bp.bean, method); 164 if (result == null) { 165 result = BeanPropertyUtil.createBeanProperty(bp); 166 } 167 return result; 168 } 169 Field field = bp.cd.getField(bp.name, suppressSecurity); 171 if (field != null) { 172 Object result = BeanPropertyUtil.getField(bp.bean, field); 173 if (result == null) { 174 result = BeanPropertyUtil.createBeanProperty(bp); 175 } 176 return result; 177 } 178 179 if (bp.cd.isMap()) { 181 Map map = (Map ) bp.bean; 182 if (map.containsKey(bp.name) == false) { 183 return BeanPropertyUtil.createMapProperty(map, bp.name); 184 } 185 return map.get(bp.name); 186 } 187 188 throw new BeanException("Unable to read forced simple property '" + bp + '\''); 190 } 191 192 193 public static void setSimpleProperty(Object bean, String property, Object value, boolean suppressSecurity) { 194 setSimpleProperty(new BeanProperty(bean, property), value, suppressSecurity); 195 } 196 197 public static boolean setSimplePropertySilent(Object bean, String property, Object value, boolean suppressSecurity) { 198 return setSimplePropertySilent(new BeanProperty(bean, property), value, suppressSecurity); 199 } 200 201 204 protected static boolean setSimplePropertySilent(BeanProperty bp, Object value, boolean suppressSecurity) { 205 206 Method method = bp.cd.getBeanSetter(bp.name, suppressSecurity); 208 if (method != null) { 209 return BeanPropertyUtil.invokeSetterSilent(bp.bean, method, value); 210 } 211 212 Field field = bp.cd.getField(bp.name, suppressSecurity); 214 if (field != null) { 215 return BeanPropertyUtil.setFieldSilent(bp.bean, field, value); 216 } 217 218 if (bp.cd.isMap() == true) { 220 ((Map ) bp.bean).put(bp.name, value); 221 return true; 222 } 223 224 return false; 225 } 226 227 230 protected static void setSimpleProperty(BeanProperty bp, Object value, boolean suppressSecurity) { 231 232 Method method = bp.cd.getBeanSetter(bp.name, suppressSecurity); 234 if (method != null) { 235 BeanPropertyUtil.invokeSetter(bp.bean, method, value); 236 return; 237 } 238 239 Field field = bp.cd.getField(bp.name, suppressSecurity); 241 if (field != null) { 242 BeanPropertyUtil.setField(bp.bean, field, value); 243 return; 244 } 245 246 if (bp.cd.isMap() == true) { 248 ((Map ) bp.bean).put(bp.name, value); 249 return; 250 } 251 252 throw new BeanException("Unable to set property '" + bp + '\''); 253 } 254 255 256 257 258 260 public static boolean hasIndexProperty(Object bean, String property, boolean suppressSecurity) { 261 return hasIndexProperty(new BeanProperty(bean, property), suppressSecurity); 262 } 263 264 protected static boolean hasIndexProperty(BeanProperty bp, boolean suppressSecurity) { 265 266 if (bp.bean == null) { 267 return false; 268 } 269 String indexString = BeanPropertyUtil.extractIndex(bp); 270 271 if (indexString == null) { 272 return hasSimpleProperty(bp, suppressSecurity); 273 } 274 275 Object resultBean = getSimpleProperty(bp, suppressSecurity); 276 277 if (resultBean == null) { 278 return false; 279 } 280 281 if (resultBean.getClass().isArray() == true) { 283 int index = 0; 284 try { 285 index = Integer.parseInt(indexString); 286 } catch (NumberFormatException nfex) { 287 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 288 } 289 return (index >= 0) && (index < Array.getLength(resultBean)); 290 } 291 292 if (resultBean instanceof List ) { 294 int index = 0; 295 try { 296 index = Integer.parseInt(indexString); 297 } catch (NumberFormatException nfex) { 298 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 299 } 300 return (index >= 0) && (index < ((List )resultBean).size()); 301 } 302 if (resultBean instanceof Map ) { 303 return ((Map )resultBean).containsKey(indexString); 304 } 305 306 return false; 308 } 309 310 311 public static Object getIndexProperty(Object bean, String property, boolean suppressSecurity, boolean forced) { 312 return getIndexProperty(new BeanProperty(bean, property), suppressSecurity, forced); 313 } 314 315 319 protected static Object getIndexProperty(BeanProperty bp, boolean suppressSecurity, boolean forced) { 320 String indexString = BeanPropertyUtil.extractIndex(bp); 321 322 Object resultBean = 323 forced == true ? 324 getSimplePropertyForced(bp, suppressSecurity) : 325 getSimpleProperty(bp, suppressSecurity); 326 327 if (indexString == null) { 328 return resultBean; } 330 331 if (resultBean == null) { 332 throw new BeanException("Value of index property '" + bp + "' is null."); 333 } 334 335 if (resultBean.getClass().isArray() == true) { 337 int index = 0; 338 try { 339 index = Integer.parseInt(indexString); 340 } catch (NumberFormatException nfex) { 341 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 342 } 343 if (forced == true) { 344 return BeanPropertyUtil.arrayForcedGet(bp, resultBean, index); 345 } else { 346 return Array.get(resultBean, index); 347 } 348 } 349 350 if (resultBean instanceof List ) { 352 int index = 0; 353 try { 354 index = Integer.parseInt(indexString); 355 } catch (NumberFormatException nfex) { 356 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 357 } 358 if (forced == true) { 359 return BeanPropertyUtil.listForcedGet(bp, (List )resultBean, index); 360 } else { 361 return ((List )resultBean).get(index); 362 } 363 } 364 if (resultBean instanceof Map ) { 365 if (forced == true) { 366 return BeanPropertyUtil.mapForcedGet(bp, (Map )resultBean, indexString); 367 } else { 368 return ((Map )resultBean).get(indexString); 369 } 370 } 371 372 throw new BeanException("Unable to read inner indexed property '" + bp + '\''); 374 } 375 376 377 378 public static void setIndexProperty(Object bean, String property, Object value, boolean suppressSecurity, boolean forced) { 379 setIndexProperty(new BeanProperty(bean, property), value, suppressSecurity, forced, false); 380 } 381 382 public static boolean setIndexPropertySilent(Object bean, String property, Object value, boolean suppressSecurity, boolean forced) { 383 return setIndexProperty(new BeanProperty(bean, property), value, suppressSecurity, forced, true); 384 } 385 386 389 protected static boolean setIndexProperty(BeanProperty beanProperty, Object value, boolean suppressSecurity, boolean forced, boolean silent) { 390 String indexString = BeanPropertyUtil.extractIndex(beanProperty); 391 392 if (indexString == null) { 393 if (silent == true) { 394 return setSimplePropertySilent(beanProperty, value, suppressSecurity); 395 } 396 setSimpleProperty(beanProperty, value, suppressSecurity); 397 return true; 398 } 399 400 Object nextBean = 402 forced == true ? 403 getSimplePropertyForced(beanProperty, suppressSecurity) : 404 getSimpleProperty(beanProperty, suppressSecurity); 405 406 if (nextBean.getClass().isArray() == true) { 408 int index = 0; 409 try { 410 index = Integer.parseInt(indexString); 411 } catch (NumberFormatException nfex) { 412 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 413 } 414 if (forced == true) { 415 BeanPropertyUtil.arrayForcedSet(beanProperty, nextBean, index, value); 416 } else { 417 Array.set(nextBean, index, value); 418 } 419 return true; 420 } 421 422 if (nextBean instanceof List ) { 423 int index = 0; 424 try { 425 index = Integer.parseInt(indexString); 426 } catch (NumberFormatException nfex) { 427 throw new BeanException("Provided index '" + index + "' is not a number.", nfex); 428 } 429 if (forced == true) { 430 BeanPropertyUtil.listForcedSet((List ) nextBean, index, value); 431 } else { 432 ((List ) nextBean).add(index, value); 433 } 434 return true; 435 } 436 if (nextBean instanceof Map ) { 437 ((Map ) nextBean).put(indexString, value); 438 return true; 439 } 440 if (silent == true) { 441 return false; 442 } 443 throw new BeanException("Unable to set index property '" + beanProperty + '\''); 444 } 445 446 447 449 452 public static void setProperty(Object bean, String name, Object value) { 453 BeanProperty beanProperty = new BeanProperty(bean, name); 454 resolveNestedProperties(beanProperty, false); 455 setIndexProperty(beanProperty, value, false, false, false); 456 } 457 460 public static boolean setPropertySilent(Object bean, String name, Object value) { 461 BeanProperty beanProperty = new BeanProperty(bean, name); 462 resolveNestedProperties(beanProperty, false); 463 return setIndexProperty(beanProperty, value, false, false, true); 464 } 465 466 469 public static void setPropertyForced(Object bean, String name, Object value) { 470 BeanProperty beanProperty = new BeanProperty(bean, name); 471 resolveNestedProperties(beanProperty, true); 472 setIndexProperty(beanProperty, value, false, true, false); 473 } 474 public static boolean setPropertyForcedSilent(Object bean, String name, Object value) { 475 BeanProperty beanProperty = new BeanProperty(bean, name); 476 resolveNestedProperties(beanProperty, true); 477 return setIndexProperty(beanProperty, value, false, true, true); 478 } 479 480 483 public static void setDeclaredProperty(Object bean, String name, Object value) { 484 BeanProperty beanProperty = new BeanProperty(bean, name); 485 resolveNestedProperties(beanProperty, false); 486 setIndexProperty(beanProperty, value, true, false, false); 487 } 488 public static boolean setDeclaredPropertySilent(Object bean, String name, Object value) { 489 BeanProperty beanProperty = new BeanProperty(bean, name); 490 resolveNestedProperties(beanProperty, false); 491 return setIndexProperty(beanProperty, value, true, false, true); 492 } 493 496 public static void setDeclaredPropertyForced(Object bean, String name, Object value) { 497 BeanProperty beanProperty = new BeanProperty(bean, name); 498 resolveNestedProperties(beanProperty, true); 499 setIndexProperty(beanProperty, value, true, true, false); 500 } 501 public static boolean setDeclaredPropertyForcedSilent(Object bean, String name, Object value) { 502 BeanProperty beanProperty = new BeanProperty(bean, name); 503 resolveNestedProperties(beanProperty, true); 504 return setIndexProperty(beanProperty, value, true, true, true); 505 } 506 507 508 510 513 public static Object getProperty(Object bean, String name) { 514 BeanProperty beanProperty = new BeanProperty(bean, name); 515 resolveNestedProperties(beanProperty, false); 516 return getIndexProperty(beanProperty, false, false); 517 } 518 521 public static Object getDeclaredProperty(Object bean, String name) { 522 BeanProperty beanProperty = new BeanProperty(bean, name); 523 resolveNestedProperties(beanProperty, false); 524 return getIndexProperty(beanProperty, true, false); 525 } 526 527 528 530 public static boolean hasProperty(Object bean, String name) { 531 BeanProperty beanProperty = new BeanProperty(bean, name); 532 if (resolveExistingNestedProperties(beanProperty) == false) { 533 return false; 534 } 535 return hasIndexProperty(beanProperty, false); 536 } 537 538 public static boolean hasDeclaredProperty(Object bean, String name) { 539 BeanProperty beanProperty = new BeanProperty(bean, name); 540 if (resolveExistingNestedProperties(beanProperty) == false) { 541 return false; 542 } 543 return hasIndexProperty(beanProperty, true); 544 } 545 546 } 547 | Popular Tags |