1 7 package com.inversoft.beans; 8 9 10 import java.beans.Introspector ; 11 import java.lang.reflect.Method ; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 import java.util.StringTokenizer ; 15 16 import com.inversoft.util.ReflectionException; 17 import com.inversoft.util.ReflectionTools; 18 19 20 26 public class JavaBeanTools { 27 28 31 public static final String GET_STRING = "get"; 32 33 36 public static final String SET_STRING = "set"; 37 public static final int GET_LENGTH = GET_STRING.length(); 38 39 43 public static final String IS_STRING = "is"; 44 public static final int IS_LENGTH = IS_STRING.length(); 45 46 49 public static final String HANDLE_STRING = "handle"; 50 public static final int HANDLE_LENGTH = HANDLE_STRING.length(); 51 52 53 private JavaBeanTools() { 54 } 56 57 58 67 public static String getPropertyName(Method method) { 68 String name = method.getName(); 69 70 if (name.startsWith(IS_STRING)) { 72 73 if (name.length() <= IS_LENGTH || 75 Character.isLowerCase(name.charAt(IS_LENGTH))) { 76 return null; 77 } 78 79 return Introspector.decapitalize(name.substring(IS_LENGTH)); 80 81 } else if (name.startsWith(GET_STRING) || name.startsWith(SET_STRING)) { 82 83 if (name.length() <= GET_LENGTH || 85 Character.isLowerCase(name.charAt(GET_LENGTH))) { 86 return null; 87 } 88 89 return Introspector.decapitalize(name.substring(GET_LENGTH)); 90 91 } 92 93 return null; } 95 96 104 public static String capitalize(String lowerCase) { 105 106 char [] chars = lowerCase.toCharArray(); 107 chars[0] = Character.toUpperCase(chars[0]); 108 109 return new String (chars); 110 } 111 112 123 public static String makeGetter(String propertyName) { 124 return GET_STRING + capitalize(propertyName); 125 } 126 127 138 public static String makeSetter(String propertyName) { 139 return SET_STRING + capitalize(propertyName); 140 } 141 142 155 public static String makeIs(String propertyName) { 156 return IS_STRING + capitalize(propertyName); 157 } 158 159 171 public static String makeHandle(String propertyName) { 172 return HANDLE_STRING + capitalize(propertyName); 173 } 174 175 182 public static boolean isValidGetter(Method method) { 183 return ((method.getName().startsWith(GET_STRING) || 184 method.getName().startsWith(IS_STRING)) && 185 getPropertyName(method) != null && 186 method.getParameterTypes().length == 0 && 187 method.getReturnType() != Void.TYPE); 188 } 189 190 197 public static boolean isValidIndexedGetter(Method method) { 198 return ((method.getName().startsWith(GET_STRING) || 199 method.getName().startsWith(IS_STRING)) && 200 getPropertyName(method) != null && 201 method.getParameterTypes().length == 1 && 202 method.getParameterTypes()[0] == int.class && 203 method.getReturnType() != Void.TYPE); 204 } 205 206 217 public static boolean isValidGetter(Method method, boolean checkName) { 218 219 if (checkName) { 220 return isValidGetter(method); 221 } 222 223 return (method.getParameterTypes().length == 0 && 224 method.getReturnType() != Void.TYPE); 225 } 226 227 238 public static boolean isValidIndexedGetter(Method method, boolean checkName) { 239 240 if (checkName) { 241 return isValidIndexedGetter(method); 242 } 243 244 return (method.getParameterTypes().length == 1 && 245 method.getParameterTypes()[0] == int.class && 246 method.getReturnType() != Void.TYPE); 247 } 248 249 257 public static boolean isValidSetter(Method method) { 258 return (method.getName().startsWith(SET_STRING) && 259 getPropertyName(method) != null && 260 method.getParameterTypes().length == 1 && 261 method.getReturnType() == Void.TYPE); 262 } 263 264 272 public static boolean isValidIndexedSetter(Method method) { 273 return (method.getName().startsWith(SET_STRING) && 274 getPropertyName(method) != null && 275 method.getParameterTypes().length == 2 && 276 method.getParameterTypes()[0] == int.class && 277 method.getReturnType() == Void.TYPE); 278 } 279 280 290 public static boolean isValidSetter(Method method, boolean checkName) { 291 292 if (checkName) { 293 return isValidSetter(method); 294 } 295 296 return (method.getParameterTypes().length == 1 && 297 method.getReturnType() == Void.TYPE); 298 } 299 300 311 public static boolean isValidIndexedSetter(Method method, boolean checkName) { 312 313 if (checkName) { 314 return isValidIndexedSetter(method); 315 } 316 317 return (method.getParameterTypes().length == 2 && 318 method.getParameterTypes()[0] == int.class && 319 method.getReturnType() == Void.TYPE); 320 } 321 322 333 public static Method findReadMethod(String property, Class beanClass) 334 throws BeanException { 335 336 property = property.trim(); 338 if (property.length() == 0) { 339 throw new BeanException("The property name String is empty or contains " + 340 "only white space and is not valid"); 341 } 342 343 String getter = makeGetter(property); 344 Method method; 345 346 try { 347 method = beanClass.getMethod(getter, null); 348 } catch (SecurityException se) { 349 throw new BeanException(se.getMessage(), se); 350 } catch (NoSuchMethodException nsme) { 351 352 String is = makeIs(property); 353 354 try { 356 method = beanClass.getMethod(is, null); 357 } catch (SecurityException se) { 358 throw new BeanException(se); 359 } catch (NoSuchMethodException nsme2) { 360 throw new BeanException("Getter/is for property named: " + property 361 + " does not exist in class: " + beanClass.getName(), nsme2); 362 } 363 } 364 365 if (!isValidGetter(method, false)) { 366 throw new BeanException("Getter/is for property named: " + property 367 + " is not a valid Java bean getter"); 368 } 369 370 return method; 371 } 372 373 381 public static Method findIndexedReadMethod(String property, Class beanClass) 382 throws BeanException { 383 384 property = property.trim(); 386 if (property.length() == 0) { 387 throw new BeanException("The property name String is empty or contains " + 388 "only white space and is not valid"); 389 } 390 391 String getter = makeGetter(property); 392 Class [] params = {int.class}; 393 Method method; 394 395 try { 396 method = beanClass.getMethod(getter, params); 397 } catch (SecurityException se) { 398 throw new BeanException(se); 399 } catch (NoSuchMethodException nsme) { 400 401 String is = makeIs(property); 402 403 try { 405 method = beanClass.getMethod(is, params); 406 } catch (SecurityException se) { 407 throw new BeanException(se); 408 } catch (NoSuchMethodException nsme2) { 409 throw new BeanException("Indexed getter for property named: " + 410 property + " does not exist in class: " + beanClass.getName(), 411 nsme); 412 } 413 } 414 415 if (!isValidIndexedGetter(method, false)) { 416 throw new BeanException("Indexed getter for property named: " + property 417 + " is not a valid Java bean getter"); 418 } 419 420 return method; 421 } 422 423 432 public static Method findWriteMethod(String property, Class beanClass, 433 Class type) 434 throws BeanException { 435 436 if (type == null || type == Void.TYPE) { 437 throw new BeanException("Setters do not taken void or null parameter types"); 438 } 439 440 property = property.trim(); 442 if (property.length() == 0) { 443 throw new BeanException("The property name String is empty or contains " + 444 "only white space and is not valid"); 445 } 446 447 String setter = makeSetter(property); 448 Method method; 449 450 try { 451 method = beanClass.getMethod(setter, new Class []{type}); 452 } catch (SecurityException se) { 453 throw new BeanException(se.getMessage(), se); 454 } catch (NoSuchMethodException nsme) { 455 throw new BeanException("Setter for property named: " + property 456 + " does not exist in class: " + beanClass.getName(), nsme); 457 } 458 459 if (!isValidSetter(method, false)) { 460 throw new BeanException("Setter for property named: " + property 461 + " is not a valid Java bean setter"); 462 } 463 464 return method; 465 } 466 467 476 public static Method findIndexedWriteMethod(String property, Class beanClass, 477 Class type) 478 throws BeanException { 479 480 if (type == null || type == Void.TYPE) { 481 throw new BeanException("Setters do not taken void or null parameter types"); 482 } 483 484 property = property.trim(); 486 if (property.length() == 0) { 487 throw new BeanException("The property name String is empty or contains " + 488 "only white space and is not valid"); 489 } 490 491 String setter = makeSetter(property); 492 Class [] params = {int.class, type}; 493 Method method; 494 495 try { 496 method = beanClass.getMethod(setter, params); 497 } catch (SecurityException se) { 498 throw new BeanException(se.getMessage(), se); 499 } catch (NoSuchMethodException nsme) { 500 throw new BeanException("Indexed setter for property named: " + property 501 + " does not exist in class: " + beanClass.getName(), nsme); 502 } 503 504 if (!isValidIndexedSetter(method, false)) { 505 throw new BeanException("Indexed setter for property named: " + property 506 + " is not a valid Java bean setter"); 507 } 508 509 return method; 510 } 511 512 522 public static Object callGetter(BeanProperty prop, Object bean) throws BeanException { 523 524 Method read = prop.getReadMethod(); 525 if (read == null) { 526 StringBuffer buf = new StringBuffer (128); 527 buf.append("Getter/is for property named: ").append(prop.getPropertyName()); 528 buf.append(" does not exist for class: ").append(bean.getClass().getName()); 529 throw new BeanException(buf.toString()); 530 } 531 532 return invokeMethod(read, bean, null); 533 } 534 535 547 public static Object callIndexedGetter(IndexedBeanProperty prop, Object bean, 548 Integer index) throws BeanException { 549 550 Method read = prop.getReadMethod(); 551 if (read == null) { 552 StringBuffer buf = new StringBuffer (128); 553 buf.append("Indexed getter for property named: ").append(prop.getPropertyName()); 554 buf.append(" does not exist for class: ").append(bean.getClass().getName()); 555 throw new BeanException(buf.toString()); 556 } 557 558 Object [] params = {index}; 560 561 return invokeMethod(read, bean, params); 562 } 563 564 575 public static void callSetter(BeanProperty prop, Object bean, Object param) 576 throws BeanException { 577 578 Method write = prop.getWriteMethod(); 579 if (write == null) { 580 StringBuffer buf = new StringBuffer (128); 581 buf.append("Setter for property named: ").append(prop.getPropertyName()); 582 buf.append(" does not exist for class: ").append(bean.getClass().getName()); 583 throw new BeanException(buf.toString()); 584 } 585 586 invokeMethod(write, bean, new Object [] {param}); 587 } 588 589 601 public static void callIndexedSetter(BeanProperty prop, Object bean, Integer index, 602 Object param) throws BeanException { 603 604 Method write = prop.getWriteMethod(); 605 if (write == null) { 606 StringBuffer buf = new StringBuffer (128); 607 buf.append("Indexed setter for property named: ").append(prop.getPropertyName()); 608 buf.append(" does not exist for class: ").append(bean.getClass().getName()); 609 throw new BeanException(buf.toString()); 610 } 611 612 Object [] params = {index, param}; 613 614 invokeMethod(write, bean, params); 615 } 616 617 632 static Object invokeMethod(Method method, Object bean, Object [] params) 633 throws BeanException { 634 try { 635 return ReflectionTools.invokeMethod(method, bean, params); 636 } catch (ReflectionException re) { 637 throw new BeanException(re); 638 } 639 } 640 641 650 public static NameInfo splitNameFront(String propertyName) { 651 NameInfo nh = new NameInfo(); 652 nh.indexOfDot = propertyName.indexOf("."); 653 654 if (nh.indexOfDot == -1) { 656 nh.localPropertyName = propertyName; 657 nh.nested = false; 658 } else { 659 nh.localPropertyName = propertyName.substring(0, nh.indexOfDot); 660 nh.nestedPropertyName = propertyName.substring(nh.indexOfDot + 1); 661 nh.nested = true; 662 } 663 664 return nh; 665 } 666 667 676 public static NameInfo splitNameBack(String propertyName) { 677 NameInfo nh = new NameInfo(); 678 nh.indexOfDot = propertyName.lastIndexOf("."); 679 680 if (nh.indexOfDot == -1) { 682 nh.localPropertyName = propertyName; 683 nh.nested = false; 684 } else { 685 nh.nestedPropertyName = propertyName.substring(0, nh.indexOfDot); 686 nh.localPropertyName = propertyName.substring(nh.indexOfDot + 1); 687 nh.nested = true; 688 } 689 690 return nh; 691 } 692 693 701 public static String [] splitNameComplete(String propertyName) { 702 StringTokenizer st = new StringTokenizer (propertyName, "."); 703 String [] props = new String [st.countTokens()]; 704 int i = 0; 705 while (st.hasMoreTokens()) { 706 props[i] = st.nextToken(); 707 i++; 708 } 709 710 return props; 711 } 712 713 725 public static PropertyInfo retrievePropertyInfo(String propertyName) 726 throws BeanException { 727 728 PropertyInfo propertyInfo = new PropertyInfo(); 730 int bracet = propertyName.indexOf('['); 731 int firstBracet = bracet; 732 if (bracet == -1) { 733 propertyInfo.indices = null; 734 propertyInfo.propertyName = propertyName; 735 return propertyInfo; 736 } 737 738 while (bracet != -1) { 739 int bracetTwo = propertyName.indexOf(']', bracet); 740 if (bracetTwo == -1) { 741 throw new BeanException("The bean property name string: " + propertyName 742 + " contains an invalid indices"); 743 } 744 745 String indexStr = propertyName.substring(bracet + 1, bracetTwo); 746 int length = indexStr.length(); 747 char ch = indexStr.charAt(0); 748 if (ch == '"' || ch == '\'') { 749 char lastCh = indexStr.charAt(length - 1); 750 if (lastCh != '"' && lastCh != '\'') { 751 throw new BeanException("Invalid indices value: " + indexStr); 752 } 753 754 propertyInfo.indices.add(indexStr.substring(1, length - 1)); 755 } else { 756 try { 757 propertyInfo.indices.add(Integer.valueOf(indexStr)); 758 } catch (NumberFormatException nfe) { 759 propertyInfo.indices.add(indexStr); 760 } 761 } 762 763 bracet = propertyName.indexOf('[', bracetTwo); 764 } 765 766 propertyInfo.propertyName = propertyName.substring(0, firstBracet); 767 768 return propertyInfo; 769 } 770 771 782 public static List retrieveAllPropertyInfo(String propertyName) 783 throws BeanException { 784 List info = new ArrayList (); 785 StringTokenizer ts = new StringTokenizer (propertyName, "."); 786 787 while (ts.hasMoreTokens()) { 788 PropertyInfo pi = retrievePropertyInfo(ts.nextToken()); 790 info.add(pi); 791 } 792 793 return info; 794 } 795 796 797 801 805 public static class PropertyInfo { 806 807 811 public List indices = new ArrayList (); 812 813 817 public String propertyName; 818 } 819 820 824 public static class NameInfo { 825 826 830 public boolean nested; 831 832 837 public int indexOfDot; 838 839 843 public String localPropertyName; 844 845 851 public String nestedPropertyName; 852 } 853 } 854 | Popular Tags |