1 17 18 package org.apache.tomcat.util; 19 20 import java.io.File ; 21 import java.io.FilenameFilter ; 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.InetAddress ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.net.UnknownHostException ; 29 import java.util.Hashtable ; 30 import java.util.StringTokenizer ; 31 import java.util.Vector ; 32 33 35 38 public final class IntrospectionUtils { 39 40 41 private static org.apache.commons.logging.Log log= 42 org.apache.commons.logging.LogFactory.getLog( IntrospectionUtils.class ); 43 44 47 public static void execute(Object proxy, String method) throws Exception { 48 Method executeM = null; 49 Class c = proxy.getClass(); 50 Class params[] = new Class [0]; 51 executeM = findMethod(c, method, params); 53 if (executeM == null) { 54 throw new RuntimeException ("No execute in " + proxy.getClass()); 55 } 56 executeM.invoke(proxy, (Object []) null); } 58 59 62 public static void setAttribute(Object proxy, String n, Object v) 63 throws Exception { 64 if (proxy instanceof AttributeHolder) { 65 ((AttributeHolder) proxy).setAttribute(n, v); 66 return; 67 } 68 69 Method executeM = null; 70 Class c = proxy.getClass(); 71 Class params[] = new Class [2]; 72 params[0] = String .class; 73 params[1] = Object .class; 74 executeM = findMethod(c, "setAttribute", params); 75 if (executeM == null) { 76 if (log.isDebugEnabled()) 77 log.debug("No setAttribute in " + proxy.getClass()); 78 return; 79 } 80 if (false) 81 if (log.isDebugEnabled()) 82 log.debug("Setting " + n + "=" + v + " in " + proxy); 83 executeM.invoke(proxy, new Object [] { n, v }); 84 return; 85 } 86 87 90 public static Object getAttribute(Object proxy, String n) throws Exception { 91 Method executeM = null; 92 Class c = proxy.getClass(); 93 Class params[] = new Class [1]; 94 params[0] = String .class; 95 executeM = findMethod(c, "getAttribute", params); 96 if (executeM == null) { 97 if (log.isDebugEnabled()) 98 log.debug("No getAttribute in " + proxy.getClass()); 99 return null; 100 } 101 return executeM.invoke(proxy, new Object [] { n }); 102 } 103 104 107 public static ClassLoader getURLClassLoader(URL urls[], ClassLoader parent) { 108 try { 109 Class urlCL = Class.forName("java.net.URLClassLoader"); 110 Class paramT[] = new Class [2]; 111 paramT[0] = urls.getClass(); 112 paramT[1] = ClassLoader .class; 113 Method m = findMethod(urlCL, "newInstance", paramT); 114 if (m == null) 115 return null; 116 117 ClassLoader cl = (ClassLoader ) m.invoke(urlCL, new Object [] { urls, 118 parent }); 119 return cl; 120 } catch (ClassNotFoundException ex) { 121 return null; 123 } catch (Exception ex) { 124 ex.printStackTrace(); 125 return null; 126 } 127 } 128 129 public static String guessInstall(String installSysProp, 130 String homeSysProp, String jarName) { 131 return guessInstall(installSysProp, homeSysProp, jarName, null); 132 } 133 134 144 public static String guessInstall(String installSysProp, 145 String homeSysProp, String jarName, String classFile) { 146 String install = null; 147 String home = null; 148 149 if (installSysProp != null) 150 install = System.getProperty(installSysProp); 151 152 if (homeSysProp != null) 153 home = System.getProperty(homeSysProp); 154 155 if (install != null) { 156 if (home == null) 157 System.getProperties().put(homeSysProp, install); 158 return install; 159 } 160 161 163 String cpath = System.getProperty("java.class.path"); 164 String pathSep = System.getProperty("path.separator"); 165 StringTokenizer st = new StringTokenizer (cpath, pathSep); 166 while (st.hasMoreTokens()) { 167 String path = st.nextToken(); 168 if (path.endsWith(jarName)) { 170 home = path.substring(0, path.length() - jarName.length()); 171 try { 172 if ("".equals(home)) { 173 home = new File ("./").getCanonicalPath(); 174 } else if (home.endsWith(File.separator)) { 175 home = home.substring(0, home.length() - 1); 176 } 177 File f = new File (home); 178 String parentDir = f.getParent(); 179 if (parentDir == null) 180 parentDir = home; File f1 = new File (parentDir); 182 install = f1.getCanonicalPath(); 183 if (installSysProp != null) 184 System.getProperties().put(installSysProp, install); 185 if (home == null && homeSysProp != null) 186 System.getProperties().put(homeSysProp, install); 187 return install; 188 } catch (Exception ex) { 189 ex.printStackTrace(); 190 } 191 } else { 192 String fname = path + (path.endsWith("/") ? "" : "/") 193 + classFile; 194 if (new File (fname).exists()) { 195 try { 196 File f = new File (path); 197 String parentDir = f.getParent(); 198 if (parentDir == null) 199 parentDir = path; File f1 = new File (parentDir); 201 install = f1.getCanonicalPath(); 202 if (installSysProp != null) 203 System.getProperties().put(installSysProp, install); 204 if (home == null && homeSysProp != null) 205 System.getProperties().put(homeSysProp, install); 206 return install; 207 } catch (Exception ex) { 208 ex.printStackTrace(); 209 } 210 } 211 } 212 } 213 214 if (home != null) { 216 System.getProperties().put(installSysProp, home); 217 return home; 218 } 219 220 return null; 221 } 222 223 226 public static void displayClassPath(String msg, URL [] cp) { 227 if (log.isDebugEnabled()) { 228 log.debug(msg); 229 for (int i = 0; i < cp.length; i++) { 230 log.debug(cp[i].getFile()); 231 } 232 } 233 } 234 235 public static String PATH_SEPARATOR = System.getProperty("path.separator"); 236 237 243 public static String classPathAdd(URL urls[], String cp) { 244 if (urls == null) 245 return cp; 246 247 for (int i = 0; i < urls.length; i++) { 248 if (cp != null) 249 cp += PATH_SEPARATOR + urls[i].getFile(); 250 else 251 cp = urls[i].getFile(); 252 } 253 return cp; 254 } 255 256 261 public static void setProperty(Object o, String name, String value) { 262 if (dbg > 1) 263 d("setProperty(" + o.getClass() + " " + name + "=" + value + ")"); 264 265 String setter = "set" + capitalize(name); 266 267 try { 268 Method methods[] = findMethods(o.getClass()); 269 Method setPropertyMethod = null; 270 271 for (int i = 0; i < methods.length; i++) { 273 Class paramT[] = methods[i].getParameterTypes(); 274 if (setter.equals(methods[i].getName()) && paramT.length == 1 275 && "java.lang.String".equals(paramT[0].getName())) { 276 277 methods[i].invoke(o, new Object [] { value }); 278 return; 279 } 280 } 281 282 for (int i = 0; i < methods.length; i++) { 284 boolean ok = true; 285 if (setter.equals(methods[i].getName()) 286 && methods[i].getParameterTypes().length == 1) { 287 288 Class paramType = methods[i].getParameterTypes()[0]; 290 Object params[] = new Object [1]; 291 292 if ("java.lang.Integer".equals(paramType.getName()) 294 || "int".equals(paramType.getName())) { 295 try { 296 params[0] = new Integer (value); 297 } catch (NumberFormatException ex) { 298 ok = false; 299 } 300 }else if ("java.lang.Long".equals(paramType.getName()) 302 || "long".equals(paramType.getName())) { 303 try { 304 params[0] = new Long (value); 305 } catch (NumberFormatException ex) { 306 ok = false; 307 } 308 309 } else if ("java.lang.Boolean".equals(paramType.getName()) 311 || "boolean".equals(paramType.getName())) { 312 params[0] = new Boolean (value); 313 314 } else if ("java.net.InetAddress".equals(paramType 316 .getName())) { 317 try { 318 params[0] = InetAddress.getByName(value); 319 } catch (UnknownHostException exc) { 320 d("Unable to resolve host name:" + value); 321 ok = false; 322 } 323 324 } else { 326 d("Unknown type " + paramType.getName()); 327 } 328 329 if (ok) { 330 methods[i].invoke(o, params); 331 return; 332 } 333 } 334 335 if ("setProperty".equals(methods[i].getName())) { 337 setPropertyMethod = methods[i]; 338 } 339 } 340 341 if (setPropertyMethod != null) { 343 Object params[] = new Object [2]; 344 params[0] = name; 345 params[1] = value; 346 setPropertyMethod.invoke(o, params); 347 } 348 349 } catch (IllegalArgumentException ex2) { 350 log.warn("IAE " + o + " " + name + " " + value, ex2); 351 } catch (SecurityException ex1) { 352 if (dbg > 0) 353 d("SecurityException for " + o.getClass() + " " + name + "=" 354 + value + ")"); 355 if (dbg > 1) 356 ex1.printStackTrace(); 357 } catch (IllegalAccessException iae) { 358 if (dbg > 0) 359 d("IllegalAccessException for " + o.getClass() + " " + name 360 + "=" + value + ")"); 361 if (dbg > 1) 362 iae.printStackTrace(); 363 } catch (InvocationTargetException ie) { 364 if (dbg > 0) 365 d("InvocationTargetException for " + o.getClass() + " " + name 366 + "=" + value + ")"); 367 if (dbg > 1) 368 ie.printStackTrace(); 369 } 370 } 371 372 public static Object getProperty(Object o, String name) { 373 String getter = "get" + capitalize(name); 374 String isGetter = "is" + capitalize(name); 375 376 try { 377 Method methods[] = findMethods(o.getClass()); 378 Method getPropertyMethod = null; 379 380 for (int i = 0; i < methods.length; i++) { 382 Class paramT[] = methods[i].getParameterTypes(); 383 if (getter.equals(methods[i].getName()) && paramT.length == 0) { 384 return methods[i].invoke(o, (Object []) null); 385 } 386 if (isGetter.equals(methods[i].getName()) && paramT.length == 0) { 387 return methods[i].invoke(o, (Object []) null); 388 } 389 390 if ("getProperty".equals(methods[i].getName())) { 391 getPropertyMethod = methods[i]; 392 } 393 } 394 395 if (getPropertyMethod != null) { 397 Object params[] = new Object [1]; 398 params[0] = name; 399 return getPropertyMethod.invoke(o, params); 400 } 401 402 } catch (IllegalArgumentException ex2) { 403 log.warn("IAE " + o + " " + name, ex2); 404 } catch (SecurityException ex1) { 405 if (dbg > 0) 406 d("SecurityException for " + o.getClass() + " " + name + ")"); 407 if (dbg > 1) 408 ex1.printStackTrace(); 409 } catch (IllegalAccessException iae) { 410 if (dbg > 0) 411 d("IllegalAccessException for " + o.getClass() + " " + name 412 + ")"); 413 if (dbg > 1) 414 iae.printStackTrace(); 415 } catch (InvocationTargetException ie) { 416 if (dbg > 0) 417 d("InvocationTargetException for " + o.getClass() + " " + name 418 + ")"); 419 if (dbg > 1) 420 ie.printStackTrace(); 421 } 422 return null; 423 } 424 425 427 public static void setProperty(Object o, String name) { 428 String setter = "set" + capitalize(name); 429 try { 430 Method methods[] = findMethods(o.getClass()); 431 Method setPropertyMethod = null; 432 for (int i = 0; i < methods.length; i++) { 434 Class paramT[] = methods[i].getParameterTypes(); 435 if (setter.equals(methods[i].getName()) && paramT.length == 0) { 436 methods[i].invoke(o, new Object [] {}); 437 return; 438 } 439 } 440 } catch (Exception ex1) { 441 if (dbg > 0) 442 d("Exception for " + o.getClass() + " " + name); 443 if (dbg > 1) 444 ex1.printStackTrace(); 445 } 446 } 447 448 453 public static String replaceProperties(String value, Object getter) { 454 if (getter instanceof Hashtable ) 455 return replaceProperties(value, (Hashtable ) getter, null); 456 457 if (getter instanceof PropertySource) { 458 PropertySource src[] = new PropertySource[] { (PropertySource) getter }; 459 return replaceProperties(value, null, src); 460 } 461 return value; 462 } 463 464 467 public static String replaceProperties(String value, Hashtable staticProp, 468 PropertySource dynamicProp[]) { 469 StringBuffer sb = new StringBuffer (); 470 int prev = 0; 471 int pos; 473 while ((pos = value.indexOf("$", prev)) >= 0) { 474 if (pos > 0) { 475 sb.append(value.substring(prev, pos)); 476 } 477 if (pos == (value.length() - 1)) { 478 sb.append('$'); 479 prev = pos + 1; 480 } else if (value.charAt(pos + 1) != '{') { 481 sb.append('$'); 482 prev = pos + 1; } else { 484 int endName = value.indexOf('}', pos); 485 if (endName < 0) { 486 sb.append(value.substring(pos)); 487 prev = value.length(); 488 continue; 489 } 490 String n = value.substring(pos + 2, endName); 491 String v = null; 492 if (staticProp != null) { 493 v = (String ) ((Hashtable ) staticProp).get(n); 494 } 495 if (v == null && dynamicProp != null) { 496 for (int i = 0; i < dynamicProp.length; i++) { 497 v = dynamicProp[i].getProperty(n); 498 if (v != null) { 499 break; 500 } 501 } 502 } 503 if (v == null) 504 v = "${" + n + "}"; 505 506 sb.append(v); 507 prev = endName + 1; 508 } 509 } 510 if (prev < value.length()) 511 sb.append(value.substring(prev)); 512 return sb.toString(); 513 } 514 515 518 public static String capitalize(String name) { 519 if (name == null || name.length() == 0) { 520 return name; 521 } 522 char chars[] = name.toCharArray(); 523 chars[0] = Character.toUpperCase(chars[0]); 524 return new String (chars); 525 } 526 527 public static String unCapitalize(String name) { 528 if (name == null || name.length() == 0) { 529 return name; 530 } 531 char chars[] = name.toCharArray(); 532 chars[0] = Character.toLowerCase(chars[0]); 533 return new String (chars); 534 } 535 536 538 542 public static void addToClassPath(Vector cpV, String dir) { 543 try { 544 String cpComp[] = getFilesByExt(dir, ".jar"); 545 if (cpComp != null) { 546 int jarCount = cpComp.length; 547 for (int i = 0; i < jarCount; i++) { 548 URL url = getURL(dir, cpComp[i]); 549 if (url != null) 550 cpV.addElement(url); 551 } 552 } 553 } catch (Exception ex) { 554 ex.printStackTrace(); 555 } 556 } 557 558 public static void addToolsJar(Vector v) { 559 try { 560 File f = new File (System.getProperty("java.home") 562 + "/../lib/tools.jar"); 563 564 if (!f.exists()) { 565 f = new File (System.getProperty("java.home") + "/lib/tools.jar"); 568 if (f.exists()) { 569 if (log.isDebugEnabled()) 570 log.debug("Detected strange java.home value " 571 + System.getProperty("java.home") 572 + ", it should point to jre"); 573 } 574 } 575 URL url = new URL ("file", "", f.getAbsolutePath()); 576 577 v.addElement(url); 578 } catch (MalformedURLException ex) { 579 ex.printStackTrace(); 580 } 581 } 582 583 586 public static String [] getFilesByExt(String ld, String ext) { 587 File dir = new File (ld); 588 String [] names = null; 589 final String lext = ext; 590 if (dir.isDirectory()) { 591 names = dir.list(new FilenameFilter () { 592 public boolean accept(File d, String name) { 593 if (name.endsWith(lext)) { 594 return true; 595 } 596 return false; 597 } 598 }); 599 } 600 return names; 601 } 602 603 606 public static URL getURL(String base, String file) { 607 try { 608 File baseF = new File (base); 609 File f = new File (baseF, file); 610 String path = f.getCanonicalPath(); 611 if (f.isDirectory()) { 612 path += "/"; 613 } 614 if (!f.exists()) 615 return null; 616 return new URL ("file", "", path); 617 } catch (Exception ex) { 618 ex.printStackTrace(); 619 return null; 620 } 621 } 622 623 634 public static void addJarsFromClassPath(Vector jars, String cp) 635 throws IOException , MalformedURLException { 636 String sep = System.getProperty("path.separator"); 637 String token; 638 StringTokenizer st; 639 if (cp != null) { 640 st = new StringTokenizer (cp, sep); 641 while (st.hasMoreTokens()) { 642 File f = new File (st.nextToken()); 643 String path = f.getCanonicalPath(); 644 if (f.isDirectory()) { 645 path += "/"; 646 } 647 URL url = new URL ("file", "", path); 648 if (!jars.contains(url)) { 649 jars.addElement(url); 650 } 651 } 652 } 653 } 654 655 658 public static URL [] getClassPath(Vector v) { 659 URL [] urls = new URL [v.size()]; 660 for (int i = 0; i < v.size(); i++) { 661 urls[i] = (URL ) v.elementAt(i); 662 } 663 return urls; 664 } 665 666 670 public static URL [] getClassPath(String dir, String cpath, 671 String cpathProp, boolean addTools) throws IOException , 672 MalformedURLException { 673 Vector jarsV = new Vector (); 674 if (dir != null) { 675 URL url = getURL(dir, "classes"); 677 if (url != null) 678 jarsV.addElement(url); 679 addToClassPath(jarsV, dir); 680 } 681 682 if (cpath != null) 683 addJarsFromClassPath(jarsV, cpath); 684 685 if (cpathProp != null) { 686 String cpath1 = System.getProperty(cpathProp); 687 addJarsFromClassPath(jarsV, cpath1); 688 } 689 690 if (addTools) 691 addToolsJar(jarsV); 692 693 return getClassPath(jarsV); 694 } 695 696 698 public static boolean processArgs(Object proxy, String args[]) 699 throws Exception { 700 String args0[] = null; 701 if (null != findMethod(proxy.getClass(), "getOptions1", new Class [] {})) { 702 args0 = (String []) callMethod0(proxy, "getOptions1"); 703 } 704 705 if (args0 == null) { 706 args0 = findBooleanSetters(proxy.getClass()); 708 } 709 Hashtable h = null; 710 if (null != findMethod(proxy.getClass(), "getOptionAliases", 711 new Class [] {})) { 712 h = (Hashtable ) callMethod0(proxy, "getOptionAliases"); 713 } 714 return processArgs(proxy, args, args0, null, h); 715 } 716 717 public static boolean processArgs(Object proxy, String args[], 718 String args0[], String args1[], Hashtable aliases) throws Exception { 719 for (int i = 0; i < args.length; i++) { 720 String arg = args[i]; 721 if (arg.startsWith("-")) 722 arg = arg.substring(1); 723 if (aliases != null && aliases.get(arg) != null) 724 arg = (String ) aliases.get(arg); 725 726 if (args0 != null) { 727 boolean set = false; 728 for (int j = 0; j < args0.length; j++) { 729 if (args0[j].equalsIgnoreCase(arg)) { 730 setProperty(proxy, args0[j], "true"); 731 set = true; 732 break; 733 } 734 } 735 if (set) 736 continue; 737 } 738 if (args1 != null) { 739 for (int j = 0; j < args1.length; j++) { 740 if (args1[j].equalsIgnoreCase(arg)) { 741 i++; 742 if (i >= args.length) 743 return false; 744 setProperty(proxy, arg, args[i]); 745 break; 746 } 747 } 748 } else { 749 i++; 751 if (i >= args.length) 752 return false; 753 setProperty(proxy, arg, args[i]); 754 } 755 756 } 757 return true; 758 } 759 760 public static void clear() { 762 objectMethods.clear(); 763 } 764 765 public static String [] findVoidSetters(Class c) { 766 Method m[] = findMethods(c); 767 if (m == null) 768 return null; 769 Vector v = new Vector (); 770 for (int i = 0; i < m.length; i++) { 771 if (m[i].getName().startsWith("set") 772 && m[i].getParameterTypes().length == 0) { 773 String arg = m[i].getName().substring(3); 774 v.addElement(unCapitalize(arg)); 775 } 776 } 777 String s[] = new String [v.size()]; 778 for (int i = 0; i < s.length; i++) { 779 s[i] = (String ) v.elementAt(i); 780 } 781 return s; 782 } 783 784 public static String [] findBooleanSetters(Class c) { 785 Method m[] = findMethods(c); 786 if (m == null) 787 return null; 788 Vector v = new Vector (); 789 for (int i = 0; i < m.length; i++) { 790 if (m[i].getName().startsWith("set") 791 && m[i].getParameterTypes().length == 1 792 && "boolean".equalsIgnoreCase(m[i].getParameterTypes()[0] 793 .getName())) { 794 String arg = m[i].getName().substring(3); 795 v.addElement(unCapitalize(arg)); 796 } 797 } 798 String s[] = new String [v.size()]; 799 for (int i = 0; i < s.length; i++) { 800 s[i] = (String ) v.elementAt(i); 801 } 802 return s; 803 } 804 805 static Hashtable objectMethods = new Hashtable (); 806 807 public static Method [] findMethods(Class c) { 808 Method methods[] = (Method []) objectMethods.get(c); 809 if (methods != null) 810 return methods; 811 812 methods = c.getMethods(); 813 objectMethods.put(c, methods); 814 return methods; 815 } 816 817 public static Method findMethod(Class c, String name, Class params[]) { 818 Method methods[] = findMethods(c); 819 if (methods == null) 820 return null; 821 for (int i = 0; i < methods.length; i++) { 822 if (methods[i].getName().equals(name)) { 823 Class methodParams[] = methods[i].getParameterTypes(); 824 if (methodParams == null) 825 if (params == null || params.length == 0) 826 return methods[i]; 827 if (params == null) 828 if (methodParams == null || methodParams.length == 0) 829 return methods[i]; 830 if (params.length != methodParams.length) 831 continue; 832 boolean found = true; 833 for (int j = 0; j < params.length; j++) { 834 if (params[j] != methodParams[j]) { 835 found = false; 836 break; 837 } 838 } 839 if (found) 840 return methods[i]; 841 } 842 } 843 return null; 844 } 845 846 849 public static boolean hasHook(Object obj, String methodN) { 850 try { 851 Method myMethods[] = findMethods(obj.getClass()); 852 for (int i = 0; i < myMethods.length; i++) { 853 if (methodN.equals(myMethods[i].getName())) { 854 Class declaring = myMethods[i].getDeclaringClass(); 856 Class parentOfDeclaring = declaring.getSuperclass(); 857 860 if (!"java.lang.Object".equals(parentOfDeclaring.getName())) { 864 return true; 865 } 866 } 867 } 868 } catch (Exception ex) { 869 ex.printStackTrace(); 870 } 871 return false; 872 } 873 874 public static void callMain(Class c, String args[]) throws Exception { 875 Class p[] = new Class [1]; 876 p[0] = args.getClass(); 877 Method m = c.getMethod("main", p); 878 m.invoke(c, new Object [] { args }); 879 } 880 881 public static Object callMethod1(Object target, String methodN, 882 Object param1, String typeParam1, ClassLoader cl) throws Exception { 883 if (target == null || param1 == null) { 884 d("Assert: Illegal params " + target + " " + param1); 885 } 886 if (dbg > 0) 887 d("callMethod1 " + target.getClass().getName() + " " 888 + param1.getClass().getName() + " " + typeParam1); 889 890 Class params[] = new Class [1]; 891 if (typeParam1 == null) 892 params[0] = param1.getClass(); 893 else 894 params[0] = cl.loadClass(typeParam1); 895 Method m = findMethod(target.getClass(), methodN, params); 896 if (m == null) 897 throw new NoSuchMethodException (target.getClass().getName() + " " 898 + methodN); 899 return m.invoke(target, new Object [] { param1 }); 900 } 901 902 public static Object callMethod0(Object target, String methodN) 903 throws Exception { 904 if (target == null) { 905 d("Assert: Illegal params " + target); 906 return null; 907 } 908 if (dbg > 0) 909 d("callMethod0 " + target.getClass().getName() + "." + methodN); 910 911 Class params[] = new Class [0]; 912 Method m = findMethod(target.getClass(), methodN, params); 913 if (m == null) 914 throw new NoSuchMethodException (target.getClass().getName() + " " 915 + methodN); 916 return m.invoke(target, emptyArray); 917 } 918 919 static Object [] emptyArray = new Object [] {}; 920 921 public static Object callMethodN(Object target, String methodN, 922 Object params[], Class typeParams[]) throws Exception { 923 Method m = null; 924 m = findMethod(target.getClass(), methodN, typeParams); 925 if (m == null) { 926 d("Can't find method " + methodN + " in " + target + " CLASS " 927 + target.getClass()); 928 return null; 929 } 930 Object o = m.invoke(target, params); 931 932 if (dbg > 0) { 933 StringBuffer sb = new StringBuffer (); 935 sb.append("" + target.getClass().getName() + "." + methodN + "( "); 936 for (int i = 0; i < params.length; i++) { 937 if (i > 0) 938 sb.append(", "); 939 sb.append(params[i]); 940 } 941 sb.append(")"); 942 d(sb.toString()); 943 } 944 return o; 945 } 946 947 public static Object convert(String object, Class paramType) { 948 Object result = null; 949 if ("java.lang.String".equals(paramType.getName())) { 950 result = object; 951 } else if ("java.lang.Integer".equals(paramType.getName()) 952 || "int".equals(paramType.getName())) { 953 try { 954 result = new Integer (object); 955 } catch (NumberFormatException ex) { 956 } 957 } else if ("java.lang.Boolean".equals(paramType.getName()) 959 || "boolean".equals(paramType.getName())) { 960 result = new Boolean (object); 961 962 } else if ("java.net.InetAddress".equals(paramType 964 .getName())) { 965 try { 966 result = InetAddress.getByName(object); 967 } catch (UnknownHostException exc) { 968 d("Unable to resolve host name:" + object); 969 } 970 971 } else { 973 d("Unknown type " + paramType.getName()); 974 } 975 if (result == null) { 976 throw new IllegalArgumentException ("Can't convert argument: " + object); 977 } 978 return result; 979 } 980 981 984 public static interface PropertySource { 985 986 public String getProperty(String key); 987 988 } 989 990 public static interface AttributeHolder { 991 992 public void setAttribute(String key, Object o); 993 994 } 995 996 static final int dbg = 0; 998 999 static void d(String s) { 1000 if (log.isDebugEnabled()) 1001 log.debug("IntrospectionUtils: " + s); 1002 } 1003} 1004 | Popular Tags |