1 22 package org.jboss.util; 23 24 import java.lang.reflect.Method ; 25 import java.lang.reflect.Modifier ; 26 import java.lang.reflect.Array ; 27 import java.lang.NoSuchMethodException ; 28 import java.net.URL ; 29 30 import java.security.AccessController ; 31 import java.security.CodeSource ; 32 import java.security.PrivilegedAction ; 33 import java.security.ProtectionDomain ; 34 import java.util.*; 35 36 43 public final class Classes 44 { 45 46 public static final String PACKAGE_SEPARATOR = "."; 47 48 49 public static final char PACKAGE_SEPARATOR_CHAR = '.'; 50 51 52 public static final String DEFAULT_PACKAGE_NAME = "<default>"; 53 54 60 public static void displayClassInfo(Class clazz, StringBuffer results) 61 { 62 ClassLoader cl = clazz.getClassLoader(); 64 results.append("\n"); 65 results.append(clazz.getName()); 66 results.append("("); 67 results.append(Integer.toHexString(clazz.hashCode())); 68 results.append(").ClassLoader="); 69 results.append(cl); 70 ClassLoader parent = cl; 71 while( parent != null ) 72 { 73 results.append("\n.."); 74 results.append(parent); 75 URL [] urls = getClassLoaderURLs(parent); 76 int length = urls != null ? urls.length : 0; 77 for(int u = 0; u < length; u ++) 78 { 79 results.append("\n...."); 80 results.append(urls[u]); 81 } 82 if( parent != null ) 83 parent = parent.getParent(); 84 } 85 CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource(); 86 if( clazzCS != null ) 87 { 88 results.append("\n++++CodeSource: "); 89 results.append(clazzCS); 90 } 91 else 92 results.append("\n++++Null CodeSource"); 93 94 results.append("\nImplemented Interfaces:"); 95 Class [] ifaces = clazz.getInterfaces(); 96 for(int i = 0; i < ifaces.length; i ++) 97 { 98 Class iface = ifaces[i]; 99 results.append("\n++"); 100 results.append(iface); 101 results.append("("); 102 results.append(Integer.toHexString(iface.hashCode())); 103 results.append(")"); 104 ClassLoader loader = ifaces[i].getClassLoader(); 105 results.append("\n++++ClassLoader: "); 106 results.append(loader); 107 ProtectionDomain pd = ifaces[i].getProtectionDomain(); 108 CodeSource cs = pd.getCodeSource(); 109 if( cs != null ) 110 { 111 results.append("\n++++CodeSource: "); 112 results.append(cs); 113 } 114 else 115 results.append("\n++++Null CodeSource"); 116 } 117 } 118 119 123 public static URL [] getClassLoaderURLs(ClassLoader cl) 124 { 125 URL [] urls = {}; 126 try 127 { 128 Class returnType = urls.getClass(); 129 Class [] parameterTypes = {}; 130 Class clClass = cl.getClass(); 131 Method getURLs = clClass.getMethod("getURLs", parameterTypes); 132 if( returnType.isAssignableFrom(getURLs.getReturnType()) ) 133 { 134 Object [] args = {}; 135 urls = (URL []) getURLs.invoke(cl, args); 136 } 137 if( urls == null || urls.length == 0 ) 138 { 139 Method getCp = clClass.getMethod("getClasspath", parameterTypes); 140 if( returnType.isAssignableFrom(getCp.getReturnType()) ) 141 { 142 Object [] args = {}; 143 urls = (URL []) getCp.invoke(cl, args); 144 } 145 } 146 } 147 catch(Exception ignore) 148 { 149 } 150 return urls; 151 } 152 153 159 public static String getDescription(Object object) 160 { 161 StringBuffer buffer = new StringBuffer (); 162 describe(buffer, object); 163 return buffer.toString(); 164 } 165 166 172 public static void describe(StringBuffer buffer, Object object) 173 { 174 if (object == null) 175 buffer.append("**null**"); 176 else 177 describe(buffer, object.getClass()); 178 } 179 180 186 public static void describe(StringBuffer buffer, Class clazz) 187 { 188 if (clazz == null) 189 buffer.append("**null**"); 190 else 191 { 192 buffer.append("{class=").append(clazz.getName()); 193 Class [] intfs = clazz.getInterfaces(); 194 if (intfs.length > 0) 195 { 196 buffer.append(" intfs="); 197 for (int i = 0; i < intfs.length; ++i) 198 { 199 buffer.append(intfs[i].getName()); 200 if (i < intfs.length-1) 201 buffer.append(", "); 202 } 203 } 204 buffer.append("}"); 205 } 206 } 207 208 215 public static String stripPackageName(final String classname) 216 { 217 int idx = classname.lastIndexOf(PACKAGE_SEPARATOR); 218 219 if (idx != -1) 220 return classname.substring(idx + 1, classname.length()); 221 return classname; 222 } 223 224 231 public static String stripPackageName(final Class type) 232 { 233 return stripPackageName(type.getName()); 234 } 235 236 245 public static String getPackageName(final String classname) 246 { 247 if (classname.length() == 0) 248 throw new EmptyStringException(); 249 250 int index = classname.lastIndexOf(PACKAGE_SEPARATOR); 251 if (index != -1) 252 return classname.substring(0, index); 253 return ""; 254 } 255 256 262 public static String getPackageName(final Class type) 263 { 264 return getPackageName(type.getName()); 265 } 266 267 279 public static void forceLoad(final Class type) 280 { 281 if (type == null) 282 throw new NullArgumentException("type"); 283 284 if (type.isPrimitive()) return; 286 287 String packageName = Classes.getPackageName(type); 289 291 if (packageName.startsWith("java.") || 292 packageName.startsWith("javax.")) 293 { 294 return; 295 } 296 297 299 try 300 { 301 Method methods[] = type.getDeclaredMethods(); 302 Method method = null; 303 for (int i = 0; i < methods.length; i++) 304 { 305 int modifiers = methods[i].getModifiers(); 306 if (Modifier.isStatic(modifiers)) 307 { 308 method = methods[i]; 309 break; 310 } 311 } 312 313 if (method != null) 314 { 315 method.invoke(null, null); 316 } 317 else 318 { 319 type.newInstance(); 320 } 321 } 322 catch (Exception ignore) 323 { 324 ThrowableHandler.add(ignore); 325 } 326 } 327 328 329 333 334 private static final Map PRIMITIVE_NAME_TYPE_MAP = new HashMap(); 335 336 337 static 338 { 339 PRIMITIVE_NAME_TYPE_MAP.put("boolean", Boolean.TYPE); 340 PRIMITIVE_NAME_TYPE_MAP.put("byte", Byte.TYPE); 341 PRIMITIVE_NAME_TYPE_MAP.put("char", Character.TYPE); 342 PRIMITIVE_NAME_TYPE_MAP.put("short", Short.TYPE); 343 PRIMITIVE_NAME_TYPE_MAP.put("int", Integer.TYPE); 344 PRIMITIVE_NAME_TYPE_MAP.put("long", Long.TYPE); 345 PRIMITIVE_NAME_TYPE_MAP.put("float", Float.TYPE); 346 PRIMITIVE_NAME_TYPE_MAP.put("double", Double.TYPE); 347 } 348 349 360 public static Class getPrimitiveTypeForName(final String name) 361 { 362 return (Class ) PRIMITIVE_NAME_TYPE_MAP.get(name); 363 } 364 365 366 private static final Class [] PRIMITIVE_WRAPPER_MAP = { 367 Boolean.TYPE, Boolean .class, 368 Byte.TYPE, Byte .class, 369 Character.TYPE, Character .class, 370 Double.TYPE, Double .class, 371 Float.TYPE, Float .class, 372 Integer.TYPE, Integer .class, 373 Long.TYPE, Long .class, 374 Short.TYPE, Short .class, 375 }; 376 377 385 public static Class getPrimitiveWrapper(final Class type) 386 { 387 if (!type.isPrimitive()) 388 { 389 throw new IllegalArgumentException ("type is not a primitive class"); 390 } 391 392 for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) 393 { 394 if (type.equals(PRIMITIVE_WRAPPER_MAP[i])) 395 return PRIMITIVE_WRAPPER_MAP[i + 1]; 396 } 397 398 throw new UnreachableStatementException(); 401 } 402 403 410 public static void getAllInterfaces(List allIfaces, Class c) 411 { 412 while( c != null ) 413 { 414 Class [] ifaces = c.getInterfaces(); 415 for(int n = 0; n < ifaces.length; n ++) 416 { 417 allIfaces.add(ifaces[n]); 418 } 419 c = c.getSuperclass(); 420 } 421 } 422 423 429 public static boolean isPrimitiveWrapper(final Class type) 430 { 431 for (int i = 0; i < PRIMITIVE_WRAPPER_MAP.length; i += 2) 432 { 433 if (type.equals(PRIMITIVE_WRAPPER_MAP[i + 1])) 434 { 435 return true; 436 } 437 } 438 439 return false; 440 } 441 442 449 public static boolean isPrimitive(final Class type) 450 { 451 if (type.isPrimitive() || isPrimitiveWrapper(type)) 452 { 453 return true; 454 } 455 456 return false; 457 } 458 462 public static boolean isPrimitive(final String type) 463 { 464 return PRIMITIVE_NAME_TYPE_MAP.containsKey(type); 465 } 466 467 471 public static Class getPrimitive(Class wrapper) 472 { 473 Class primitive; 474 if(Integer .class == wrapper) 475 { 476 primitive = int.class; 477 } 478 else if(Long .class == wrapper) 479 { 480 primitive = long.class; 481 } 482 else if(Double .class == wrapper) 483 { 484 primitive = double.class; 485 } 486 else if(Boolean .class == wrapper) 487 { 488 primitive = boolean.class; 489 } 490 else if(Short .class == wrapper) 491 { 492 primitive = short.class; 493 } 494 else if(Float .class == wrapper) 495 { 496 primitive = float.class; 497 } 498 else if(Byte .class == wrapper) 499 { 500 primitive = byte.class; 501 } 502 else if(Character .class == wrapper) 503 { 504 primitive = char.class; 505 } 506 else 507 { 508 throw new IllegalArgumentException ("The class is not a primitive wrapper type: " + wrapper); 509 } 510 return primitive; 511 } 512 513 521 public static Object instantiate(Class expected, String property, String defaultClassName) 522 { 523 String className = getProperty(property, defaultClassName); 524 Class clazz = null; 525 try 526 { 527 clazz = loadClass(className); 528 } 529 catch (ClassNotFoundException e) 530 { 531 throw new NestedRuntimeException("Cannot load class " + className, e); 532 } 533 Object result = null; 534 try 535 { 536 result = clazz.newInstance(); 537 } 538 catch (InstantiationException e) 539 { 540 throw new NestedRuntimeException("Error instantiating " + className, e); 541 } 542 catch (IllegalAccessException e) 543 { 544 throw new NestedRuntimeException("Error instantiating " + className, e); 545 } 546 if (expected.isAssignableFrom(clazz) == false) 547 throw new NestedRuntimeException("Class " + className + " from classloader " + clazz.getClassLoader() + 548 " is not of the expected class " + expected + " loaded from " + expected.getClassLoader()); 549 return result; 550 } 551 552 556 568 public static Class loadClass(String className) throws ClassNotFoundException 569 { 570 return loadClass(className, Thread.currentThread().getContextClassLoader()); 571 } 572 573 587 public static Class loadClass(String className, ClassLoader classLoader) 588 throws ClassNotFoundException 589 { 590 if (className.length() == 1) 603 { 604 char type = className.charAt(0); 605 if (type == 'B') return Byte.TYPE; 606 if (type == 'C') return Character.TYPE; 607 if (type == 'D') return Double.TYPE; 608 if (type == 'F') return Float.TYPE; 609 if (type == 'I') return Integer.TYPE; 610 if (type == 'J') return Long.TYPE; 611 if (type == 'S') return Short.TYPE; 612 if (type == 'Z') return Boolean.TYPE; 613 if (type == 'V') return Void.TYPE; 614 throw new ClassNotFoundException (className); 616 } 617 618 if( isPrimitive(className) == true ) 620 return (Class ) Classes.PRIMITIVE_NAME_TYPE_MAP.get(className); 621 622 if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') 624 return classLoader.loadClass(className.substring(1, className.length() - 1)); 625 626 try 630 { 631 return classLoader.loadClass(className); 632 } 633 catch (ClassNotFoundException e) 634 { 635 if (className.charAt(0) != '[') 637 throw e; 638 } 639 640 642 int arrayDimension = 0; 644 while (className.charAt(arrayDimension) == '[') 645 arrayDimension++; 646 647 Class componentType = loadClass(className.substring(arrayDimension), classLoader); 649 650 return Array.newInstance(componentType, new int[arrayDimension]).getClass(); 652 } 653 654 666 public final static Class [] convertToJavaClasses(Iterator it, 667 ClassLoader cl) 668 throws ClassNotFoundException 669 { 670 ArrayList classes = new ArrayList(); 671 while (it.hasNext()) 672 { 673 classes.add(convertToJavaClass((String ) it.next(), cl)); 674 } 675 return (Class []) classes.toArray(new Class [classes.size()]); 676 } 677 678 685 public final static Method getAttributeGetter(Class cls, String attr) throws NoSuchMethodException 686 { 687 StringBuffer buf = new StringBuffer (attr.length() + 3); 688 buf.append("get"); 689 if(Character.isLowerCase(attr.charAt(0))) 690 { 691 buf.append(Character.toUpperCase(attr.charAt(0))) 692 .append(attr.substring(1)); 693 } 694 else 695 { 696 buf.append(attr); 697 } 698 699 try 700 { 701 return cls.getMethod(buf.toString(), null); 702 } 703 catch (NoSuchMethodException e) 704 { 705 buf.replace(0, 3, "is"); 706 return cls.getMethod(buf.toString(), null); 707 } 708 } 709 710 718 public final static Method getAttributeSetter(Class cls, String attr, Class type) throws NoSuchMethodException 719 { 720 StringBuffer buf = new StringBuffer (attr.length() + 3); 721 buf.append("set"); 722 if(Character.isLowerCase(attr.charAt(0))) 723 { 724 buf.append(Character.toUpperCase(attr.charAt(0))) 725 .append(attr.substring(1)); 726 } 727 else 728 { 729 buf.append(attr); 730 } 731 732 return cls.getMethod(buf.toString(), new Class []{type}); 733 } 734 735 746 private final static Class convertToJavaClass(String name, 747 ClassLoader cl) 748 throws ClassNotFoundException 749 { 750 int arraySize = 0; 751 while (name.endsWith("[]")) 752 { 753 name = name.substring(0, name.length() - 2); 754 arraySize++; 755 } 756 757 Class c = (Class ) PRIMITIVE_NAME_TYPE_MAP.get(name); 759 760 if (c == null) 761 { 762 try 764 { 765 c = cl.loadClass(name); 766 } 767 catch (ClassNotFoundException cnfe) 768 { 769 throw new ClassNotFoundException ("Parameter class not found: " + 770 name); 771 } 772 } 773 774 if (arraySize > 0) 776 { 777 int[] dims = new int[arraySize]; 778 for (int i = 0; i < arraySize; i++) 779 { 780 dims[i] = 1; 781 } 782 c = Array.newInstance(c, dims).getClass(); 783 } 784 785 return c; 786 } 787 788 794 private static String getProperty(final String name, final String defaultValue) 795 { 796 return (String ) AccessController.doPrivileged( 797 new PrivilegedAction () 798 { 799 public Object run() 800 { 801 return System.getProperty(name, defaultValue); 802 } 803 804 }); 805 } 806 } 807 | Popular Tags |