1 52 53 package com.go.teatools; 54 55 import com.go.tea.runtime.*; 56 import com.go.tea.util.*; 57 58 import com.go.trove.classfile.AccessFlags; 59 import com.go.trove.util.*; 60 61 62 import java.beans.*; 63 import java.io.*; 64 import java.lang.reflect.*; 65 import java.util.*; 66 import java.text.BreakIterator ; 67 68 82 public class TeaToolsUtils extends DefaultContext 83 implements TeaToolsConstants { 84 85 86 87 private final static PropertyDescriptor[] NO_PROPERTIES = 88 new PropertyDescriptor[0]; 89 90 92 private final static Hashtable cPrimativeClasses = new Hashtable(); 93 94 95 private final static DescriptorComparator DESCRIPTOR_COMPARATOR = 96 new DescriptorComparator(); 97 98 101 public static void main(String [] args) throws Exception { 102 new Tester(args); 103 } 104 105 static { 106 107 Class [] primativeClasses = 108 new Class [] { 109 void.class, 110 boolean.class, 111 char.class, 112 byte.class, 113 short.class, 114 int.class, 115 float.class, 116 double.class, 117 long.class 118 }; 119 120 for (int i = 0; i < primativeClasses.length; i++) { 121 cPrimativeClasses.put(primativeClasses[i].getName(), 122 primativeClasses[i]); 123 } 124 } 125 126 129 public TeaToolsUtils() { 130 } 131 132 public void print(Object obj) throws Exception { 134 } 135 136 140 public TypeDescription createTypeDescription(Class type) { 141 return new TypeDescription(type, this); 142 } 143 144 148 public PropertyDescription[] createPropertyDescriptions( 149 PropertyDescriptor[] pds) { 150 151 if (pds == null) { 152 return null; 153 } 154 155 PropertyDescription[] descriptions = 156 new PropertyDescription[pds.length]; 157 158 for (int i = 0; i < pds.length; i++) { 159 descriptions[i] = new PropertyDescription(pds[i], this); 160 } 161 162 return descriptions; 163 } 164 165 169 public MethodDescription[] createMethodDescriptions( 170 MethodDescriptor[] mds) { 171 if (mds == null) { 172 return null; 173 } 174 175 MethodDescription[] descriptions = 176 new MethodDescription[mds.length]; 177 178 for (int i = 0; i < mds.length; i++) { 179 descriptions[i] = new MethodDescription(mds[i], this); 180 } 181 182 return descriptions; 183 } 184 185 189 public ParameterDescription[] createParameterDescriptions( 190 MethodDescriptor md) { 191 if (md == null) { 192 return null; 193 } 194 195 Method method = md.getMethod(); 196 Class [] paramClasses = method.getParameterTypes(); 197 int descriptionCount = paramClasses.length; 198 if (acceptsSubstitution(md)) { 199 descriptionCount--; 200 } 201 202 ParameterDescriptor[] pds = md.getParameterDescriptors(); 203 ParameterDescription[] descriptions = 204 new ParameterDescription[descriptionCount]; 205 206 for (int i = 0; i < descriptions.length; i++) { 207 208 TypeDescription type = createTypeDescription(paramClasses[i]); 209 ParameterDescriptor pd = null; 210 if (pds != null && i < pds.length) { 211 pd = pds[i]; 212 } 213 214 descriptions[i] = new ParameterDescription(type, pd, this); 215 } 216 217 return descriptions; 218 } 219 220 221 226 public AccessFlags getAccessFlags(int modifier) { 227 return new AccessFlags(modifier); 228 } 229 230 241 public Class getClassForName(String className) { 242 return getClassForName(className, null); 243 } 244 245 257 public Class getClassForName(String className, ClassLoader classLoader) { 258 259 if (className == null) { 260 return null; 261 } 262 263 int bracketIndex = className.indexOf('['); 264 if (bracketIndex > 0) { 265 className = convertArrayClassName(className, bracketIndex); 267 } 268 269 if (className == null) { 270 return null; 273 } 274 275 Class clazz = (Class ) cPrimativeClasses.get(className); 276 if (clazz != null) { 277 return clazz; 279 } 280 281 Throwable error = null; 282 try { 283 if (classLoader == null) { 284 classLoader = getClass().getClassLoader(); 285 } 286 287 if (classLoader != null) { 288 clazz = classLoader.loadClass(className); 289 } 290 } 291 catch (Throwable t) { 292 error = t; 293 } 294 295 if (clazz == null) { 296 try { 298 clazz = Class.forName(className); 299 } 300 catch (Throwable t) { 301 if (error == null) { 302 t.printStackTrace(); 303 } 304 else { 305 error.printStackTrace(); 306 } 307 } 308 } 309 310 return clazz; 311 } 312 313 317 public String getFullClassName(Class clazz) { 318 return getFullClassName(getArrayClassName(clazz)); 319 } 320 321 325 public String getFullClassName(String fullClassName) { 326 327 String [] parts = parseClassName(fullClassName); 328 329 String className = getInnerClassName(parts[1]); 330 331 if (parts[0] != null) { 332 return parts[0] + '.' + className; 334 } 335 else { 336 return className; 337 } 338 } 339 340 345 public String getClassName(Class clazz) { 346 return getClassName(getArrayClassName(clazz)); 347 } 348 349 354 public String getClassName(String fullClassName) { 355 return getInnerClassName(parseClassName(fullClassName)[1]); 356 } 357 358 362 public String getClassPackage(Class clazz) { 363 return getClassPackage(getArrayClassName(clazz)); 364 } 365 366 370 public String getClassPackage(String fullClassName) { 371 return parseClassName(fullClassName)[0]; 372 } 373 374 384 public String getClassTypeName(Class clazz) { 385 386 String type = null; 387 388 if (clazz.isInterface()) { 389 type = "interface"; 390 } 391 else if (clazz.isPrimitive()) { 392 type = null; 393 } 394 else if (clazz.isArray()) { 395 type = null; 396 } 397 else { 398 type = "class"; 399 } 400 401 return type; 402 } 403 404 410 public String getPackageVersion(String packageName) { 411 412 if (packageName == null || packageName.trim().length() == 0) { 413 return null; 414 } 415 416 if (!packageName.endsWith(".")) { 417 packageName = packageName + "."; 418 } 419 420 String className = packageName + "PackageInfo"; 421 422 Class packageInfoClass = getClassForName(className); 423 if (packageInfoClass == null) { 424 return null; 425 } 426 427 String productVersion = null; 428 String buildNumber = null; 429 430 try { 431 432 Method getProductVersionMethod = 433 packageInfoClass.getMethod("getProductVersion", 434 EMPTY_CLASS_ARRAY); 435 436 productVersion = 437 (String ) getProductVersionMethod.invoke(null, 438 EMPTY_OBJECT_ARRAY); 439 440 Method getBuildNumberMethod = 441 packageInfoClass.getMethod("getBuildNumber", 442 EMPTY_CLASS_ARRAY); 443 444 buildNumber = 445 (String ) getBuildNumberMethod.invoke(null, 446 EMPTY_OBJECT_ARRAY); 447 448 } 449 catch (Throwable t) { 450 } 452 453 if (productVersion != null && productVersion.length() > 0 && 454 buildNumber != null && buildNumber.length() > 0) { 455 456 productVersion += '.' + buildNumber; 457 } 458 459 return productVersion; 460 } 461 462 463 469 public String [] parseClassName(String fullClassName) { 470 471 int dotIndex = fullClassName.lastIndexOf("."); 472 String packageName = null; 473 String className = fullClassName; 474 475 if (dotIndex > 0) { 476 packageName = fullClassName.substring(0, dotIndex); 477 className = fullClassName.substring(dotIndex + 1); 478 } 479 480 return new String [] { packageName, className }; 481 } 482 483 486 public String getArrayClassName(Class clazz) { 487 488 if (clazz.isArray()) { 489 return getArrayClassName(clazz.getComponentType()) + "[]"; 490 } 491 492 return clazz.getName(); 493 } 494 495 499 public Class getArrayType(Class clazz) { 500 501 if (clazz.isArray()) { 502 return getArrayType(clazz.getComponentType()); 503 } 504 505 return clazz; 506 } 507 508 512 public int getArrayDimensions(Class clazz) { 513 514 if (clazz.isArray()) { 515 return getArrayDimensions(clazz.getComponentType()) + 1; 516 } 517 518 return 0; 519 } 520 521 525 public String getArrayDimensionsString(Class clazz) { 526 return createPatternString("[]", getArrayDimensions(clazz)); 527 } 528 529 543 public String convertArrayClassName(String className, 544 int bracketIndex) { 545 546 String dimensions = ""; 547 548 String brackets = className.substring(bracketIndex).trim(); 549 char[] chars = brackets.toCharArray(); 550 for (int i = 0; i < chars.length; i++) { 551 if (chars[i] == '[') { 552 dimensions = dimensions + '['; 553 } 554 } 555 556 String arrayType = className.substring(0, bracketIndex).trim(); 557 char prefixChar = 'L'; 558 559 Class clazz = (Class ) cPrimativeClasses.get(arrayType); 560 if (clazz != null) { 561 562 arrayType = ""; 563 564 if (clazz == boolean.class) { 565 prefixChar = 'Z'; 566 } 567 else if (clazz == char.class) { 568 prefixChar = 'C'; 569 } 570 else if (clazz == byte.class) { 571 prefixChar = 'B'; 572 } 573 else if (clazz == short.class) { 574 prefixChar = 'S'; 575 } 576 else if (clazz == int.class) { 577 prefixChar = 'I'; 578 } 579 else if (clazz == float.class) { 580 prefixChar = 'F'; 581 } 582 else if (clazz == double.class) { 583 prefixChar = 'D'; 584 } 585 else if (clazz == long.class) { 586 prefixChar = 'J'; 587 } 588 else if (clazz == void.class) { 589 return null; 590 } 591 } 592 else { 593 arrayType = arrayType + ';'; 594 } 595 596 return dimensions + prefixChar + arrayType; 597 } 598 599 602 public String getInnerClassName(String className) { 603 return className.replace('$', '.'); 604 } 605 606 607 613 public BeanInfo getBeanInfo(Class beanClass) 614 throws IntrospectionException { 615 return Introspector.getBeanInfo(beanClass); 616 } 617 618 627 public BeanInfo getBeanInfo(Class beanClass, Class stopClass) 628 throws IntrospectionException { 629 return Introspector.getBeanInfo(beanClass, stopClass); 630 } 631 632 638 public Object getAttributeValue(FeatureDescriptor feature, 639 String attributeName) { 640 641 if (feature == null || attributeName == null) { 642 return null; 643 } 644 645 return feature.getValue(attributeName); 646 } 647 648 662 public String decapitalize(String name) { 663 664 if (name == null) { 665 return ""; 666 } 667 668 return Introspector.decapitalize(name); 669 } 670 671 672 676 public String getFirstSentence(String paragraph) { 677 678 if (paragraph == null || paragraph.length() == 0) { 679 return ""; 680 } 681 682 BreakIterator sentenceBreaks = BreakIterator.getSentenceInstance(); 683 sentenceBreaks.setText(paragraph); 684 int start = sentenceBreaks.first(); 685 int end = sentenceBreaks.next(); 686 687 String sentence = paragraph; 688 689 if (start >= 0 && end <= paragraph.length()) { 690 sentence = paragraph.substring(start, end); 691 } 692 693 return sentence.trim(); 694 } 695 696 697 701 public String createPatternString(String pattern, int length) { 702 if (pattern == null) { 703 return null; 704 } 705 706 int totalLength = pattern.length() * length; 707 StringBuffer sb = new StringBuffer (totalLength); 708 for (int i = 0; i < length; i++) { 709 sb.append(pattern); 710 } 711 return sb.toString(); 712 } 713 714 717 public String createWhitespaceString(int length) { 718 return createPatternString(" ", length); 719 } 720 721 725 public String getDescription(FeatureDescriptor feature) { 726 String description = feature.getShortDescription(); 727 728 if (description == null || 729 description.equals(feature.getDisplayName())) { 730 731 description = ""; 732 } 733 734 return description; 735 } 736 737 743 public String getDescriptionFirstSentence(FeatureDescriptor feature) { 744 return getFirstSentence(getDescription(feature)); 745 } 746 747 753 public FeatureDescriptor[] sortDescriptors(FeatureDescriptor[] fds) { 754 758 FeatureDescriptor[] dolly = (FeatureDescriptor[])fds.clone(); 759 Arrays.sort(dolly, DESCRIPTOR_COMPARATOR); 760 return dolly; 761 } 762 763 767 public void sortMethodDescriptors(MethodDescriptor[] mds) { 768 Arrays.sort(mds, DESCRIPTOR_COMPARATOR); 769 } 770 771 774 public void sortPropertyDescriptors(PropertyDescriptor[] pds) { 775 Arrays.sort(pds, DESCRIPTOR_COMPARATOR); 776 } 777 778 779 783 784 794 public Class createContextClass(ClassLoader loader, 795 ContextClassEntry[] contextClasses) 796 throws Exception { 797 798 Object [] ret = loadContextClasses(loader, contextClasses); 799 Class [] classes = (Class []) ret[0]; 800 String [] prefixNames = (String []) ret[1]; 801 802 return createContextClass(loader, classes, prefixNames); 803 } 804 805 815 public Class createContextClass(ClassLoader loader, 816 Class [] classes, 817 String [] prefixNames) 818 throws Exception { 819 820 821 ClassInjector classInjector = 822 new ClassInjector(loader, (java.io.File ) null, null); 823 824 Constructor constructor = null; 825 826 try { 827 constructor = MergedClass.getConstructor(classInjector, 828 classes, 829 prefixNames); 830 } 831 catch (Throwable t) { 832 System.err.println(t); 833 t.printStackTrace(); 834 throw new Exception (t.getMessage()); 835 } 836 837 Class mergedContextClass = constructor.getDeclaringClass(); 838 839 return mergedContextClass; 840 } 841 842 851 public Object [] loadContextClasses(ClassLoader loader, 852 ContextClassEntry[] contextClasses) 853 throws Exception { 854 855 if (loader == null) { 856 throw new IllegalArgumentException ("ClassLoader is null"); 857 } 858 859 if (contextClasses == null || contextClasses.length == 0) { 860 throw new IllegalArgumentException ("No Context Classes"); 861 } 862 863 Vector classVector = new Vector(contextClasses.length); 864 String [] prefixNames = new String [contextClasses.length]; 865 866 Hashtable uniqueNames = new Hashtable(); 867 868 String className = null; 869 try { 870 for (int i = 0; i < contextClasses.length; i++) { 871 className = contextClasses[i].getContextClassName(); 872 Class contextClass = loader.loadClass(className); 873 874 classVector.addElement(contextClass); 875 String prefixName = contextClasses[i].getPrefixName(); 876 if (prefixName != null) { 877 878 if (uniqueNames.get(prefixName) != null) { 879 throw new IllegalArgumentException ( 880 "Duplicate Context name: " + prefixName); 881 } 882 883 uniqueNames.put(prefixName, prefixName); 884 prefixNames[i] = prefixName + "$"; 885 } 886 else { 887 prefixNames[i] = null; 888 } 889 } 890 } 891 catch (ClassNotFoundException cnfe) { 892 throw new ClassNotFoundException ("Context class \"" + className + 893 "\" was not found in the " + 894 "current Classpath."); 895 } 896 897 Class [] classes = new Class [classVector.size()]; 898 classVector.copyInto(classes); 899 900 return new Object [] {classes, prefixNames}; 901 } 902 903 904 908 public boolean isLikelyContextClass(Class clazz) { 909 return (OutputReceiver.class.isAssignableFrom(clazz) || 910 getClassName(clazz).toLowerCase().endsWith("context")); 911 } 912 913 914 922 public MethodDescriptor[] getTeaContextMethodDescriptors( 923 Class contextClass) { 924 925 return getTeaContextMethodDescriptors(contextClass, false); 926 } 927 928 936 public MethodDescriptor[] getTeaContextMethodDescriptors( 937 Class contextClass, 938 boolean specifiedClassOnly) { 939 940 Vector v = new Vector(); 941 942 MethodDescriptor[] methodDescriptors = null; 943 try { 944 BeanInfo beanInfo = getBeanInfo(contextClass); 945 methodDescriptors = beanInfo.getMethodDescriptors(); 946 } 947 catch (Throwable e) { 948 e.printStackTrace(); 949 } 950 951 if (methodDescriptors != null) { 952 953 for (int i = 0; i < methodDescriptors.length; i++) { 954 955 MethodDescriptor md = methodDescriptors[i]; 956 Class declaringClass = md.getMethod().getDeclaringClass(); 957 958 if (declaringClass != Object .class && 959 !md.isHidden() && 960 (!specifiedClassOnly || declaringClass == contextClass)) { 961 962 v.addElement(md); 963 } 964 } 965 } 966 967 methodDescriptors = new MethodDescriptor[v.size()]; 968 v.copyInto(methodDescriptors); 969 970 sortMethodDescriptors(methodDescriptors); 971 972 return methodDescriptors; 973 } 974 975 976 982 public MethodDescriptor[] getTeaContextMethodDescriptors( 983 Class [] contextClasses) { 984 985 Vector v = new Vector(); 986 Hashtable methods = new Hashtable(); 987 988 if (contextClasses != null) { 989 990 992 for (int i = 0; i < contextClasses.length; i++) { 993 Class c = contextClasses[i]; 994 if (c == null) { 995 continue; 996 } 997 998 MethodDescriptor[] mds = 999 getTeaContextMethodDescriptors(c, false); 1000 1001 for (int j = 0; j < mds.length; j++) { 1002 MethodDescriptor md = mds[j]; 1003 Method m = md.getMethod(); 1004 if (methods.get(m) == null) { 1006 methods.put(m, m); 1007 v.addElement(md); 1008 } 1009 } 1010 } 1011 } 1012 1013 MethodDescriptor[] methodDescriptors = new MethodDescriptor[v.size()]; 1014 v.copyInto(methodDescriptors); 1015 1016 sortMethodDescriptors(methodDescriptors); 1018 1019 return methodDescriptors; 1020 } 1021 1022 1023 1024 1035 public PropertyDescriptor[] getTeaBeanPropertyDescriptors( 1036 Class beanClass) { 1037 1038 1040 if (beanClass == null) { 1041 return NO_PROPERTIES; 1042 } 1043 1044 PropertyDescriptor[] properties = null; 1045 1046 Map allProps = null; 1047 try { 1048 allProps = BeanAnalyzer.getAllProperties(beanClass); 1049 } 1050 catch (Throwable t) { 1051 return NO_PROPERTIES; 1052 } 1053 1054 Collection cleanProps = new ArrayList(allProps.size()); 1055 1056 Iterator it = allProps.entrySet().iterator(); 1057 while (it.hasNext()) { 1058 1059 Map.Entry entry = (Map.Entry) it.next(); 1060 String name = (String ) entry.getKey(); 1061 PropertyDescriptor desc = (PropertyDescriptor) entry.getValue(); 1062 1063 if (name == null || name.length() == 0 || "class".equals(name)) { 1065 continue; 1066 } 1067 1068 if (desc instanceof KeyedPropertyDescriptor) { 1069 1070 KeyedPropertyDescriptor keyed = (KeyedPropertyDescriptor) desc; 1071 Class type = keyed.getKeyedPropertyType(); 1072 1073 try { 1074 desc = new ArrayIndexPropertyDescriptor(beanClass, type); 1077 } 1078 catch (Throwable t) { 1079 continue; 1080 } 1081 } 1082 else if (!beanClass.isArray() && desc.getReadMethod() == null) { 1083 continue; 1084 } 1085 1086 cleanProps.add(desc); 1087 } 1088 1089 properties = (PropertyDescriptor[]) cleanProps.toArray 1090 (new PropertyDescriptor[cleanProps.size()]); 1091 1092 sortPropertyDescriptors(properties); 1094 1095 return properties; 1096 } 1097 1098 1099 1105 public String getTeaFullClassName(Class clazz) { 1106 1107 if (isImplicitTeaImport(clazz)) { 1108 return getClassName(clazz); 1109 } 1110 1111 return getFullClassName(clazz); 1112 } 1113 1114 1122 public boolean isImplicitTeaImport(Class clazz) { 1123 if (getArrayType(clazz).isPrimitive()) { 1124 return true; 1125 } 1126 1127 return isImplicitTeaImport(getClassPackage(clazz)); 1128 } 1129 1130 1134 public boolean isImplicitTeaImport(String classOrPackageName) { 1135 1136 if (classOrPackageName == null) { 1137 return false; 1138 } 1139 1140 if (cPrimativeClasses.get(classOrPackageName) != null) { 1141 return true; 1142 } 1143 1144 for (int i = 0; i < IMPLICIT_TEA_IMPORTS.length; i++) { 1145 String prefix = IMPLICIT_TEA_IMPORTS[i]; 1146 1147 if (classOrPackageName.startsWith(prefix)) { 1148 return true; 1149 } 1150 } 1151 1152 return false; 1153 } 1154 1155 1156 1160 public boolean acceptsSubstitution(MethodDescriptor md) { 1161 return acceptsSubstitution(md.getMethod()); 1162 } 1163 1164 1168 public boolean acceptsSubstitution(Method m) { 1169 Class [] paramTypes = m.getParameterTypes(); 1170 if (paramTypes.length > 0) { 1171 Class lastParam = paramTypes[paramTypes.length - 1]; 1173 return Substitution.class.isAssignableFrom(lastParam); 1174 } 1175 return false; 1176 } 1177 1178 1183 public boolean isForeachCompatible(Class clazz) { 1184 if (clazz == null) { 1185 return false; 1186 } 1187 1188 return (clazz.isArray() || 1189 Collection.class.isAssignableFrom(clazz)); 1190 } 1191 1192 1196 public boolean isIfCompatible(Class clazz) { 1197 1198 if (clazz == null) { 1199 return false; 1200 } 1201 1202 return (clazz == boolean.class || clazz == Boolean .class); 1203 } 1204 1205 1206 1210 1211 1219 public String removeFileExtension(String fileName) { 1220 1221 int dotIndex = fileName.lastIndexOf("."); 1222 if (dotIndex > 0) { 1223 fileName = fileName.substring(0, dotIndex); 1224 } 1225 1226 return fileName; 1227 } 1228 1229 1232 public boolean isTeaFileName(String fileName) { 1233 return compareFileExtension(fileName, TEA_FILE_EXTENSION); 1234 } 1235 1236 1240 public boolean compareFileExtension(String fileName, 1241 String extension) { 1242 1243 if (fileName == null || extension == null) { 1244 return false; 1245 } 1246 1247 fileName = fileName.toLowerCase().trim(); 1248 extension = extension.toLowerCase(); 1249 return (fileName.endsWith(extension)); 1250 } 1251 1252 1258 1267 private static class DescriptorComparator implements Comparator { 1268 1269 1276 public int compare(Object x, Object y) { 1277 int nameResult = compareNames(x,y); 1278 1279 if (nameResult == 0 && x != null && y != null && 1280 x instanceof MethodDescriptor && 1281 y instanceof MethodDescriptor) { 1282 1283 MethodDescriptor mdX = (MethodDescriptor) x; 1284 MethodDescriptor mdY = (MethodDescriptor) y; 1285 1286 Method mX = mdX.getMethod(); 1287 Method mY = mdY.getMethod(); 1288 1289 return (getParamCount(mX) - getParamCount(mY)); 1290 } 1291 return nameResult; 1292 } 1293 1294 private int compareNames(Object x, Object y) { 1295 1296 if (x != null && y != null && 1297 x instanceof FeatureDescriptor && 1298 y instanceof FeatureDescriptor) { 1299 1300 FeatureDescriptor fdX = (FeatureDescriptor) x; 1301 FeatureDescriptor fdY = (FeatureDescriptor) y; 1302 1303 return fdX.getName().compareTo(fdY.getName()); 1304 } 1305 1306 return 0; 1307 } 1308 1309 private int getParamCount(Method method) { 1310 1311 int paramCount = 0; 1312 1313 Class [] paramClasses = method.getParameterTypes(); 1314 if (paramClasses != null) { 1315 paramCount = paramClasses.length; 1316 } 1317 1318 return paramCount; 1319 } 1320 } 1321 1322 1330 private class ArrayIndexPropertyDescriptor 1331 extends PropertyDescriptor { 1332 1333 private Class mPropertyType; 1334 1335 public ArrayIndexPropertyDescriptor(Class beanClass, 1336 Class propertyType) 1337 throws IntrospectionException { 1338 1339 super(BeanAnalyzer.KEYED_PROPERTY_NAME, beanClass, null, null); 1340 1341 mPropertyType = propertyType; 1342 1343 String typeName = getTeaFullClassName(propertyType); 1344 1345 setShortDescription("An indexed property with " + 1346 typeName + " elements"); 1347 1348 } 1349 1350 public Class getPropertyType() { 1351 return mPropertyType; 1352 } 1353 } 1354 1355 1356 1357 1364 private static class Tester { 1365 1366 Tester(String [] args) throws Exception { 1367 1368 TeaToolsUtils utils = new TeaToolsUtils(); 1369 1370 if (args == null || args.length == 0) { 1371 System.err.println("No classes specified, using defaults"); 1372 args = 1373 new String [] { 1374 int.class.getName(), 1375 Object .class.getName(), 1376 String [].class.getName(), 1377 int[].class.getName(), 1378 int[][].class.getName(), 1379 boolean[].class.getName(), 1380 Tester.class.getName() }; 1381 } 1382 1383 for (int i = 0; i < args.length; i++) { 1384 1385 String className = args[i]; 1386 System.out.println("\nTesting with class: " + className); 1387 1388 System.out.println("Class getClassForName(String)"); 1389 1390 Class clazz = utils.getClassForName(className); 1391 if (clazz == null) { 1392 System.err.println("Failed to load class!"); 1393 continue; 1394 } 1395 1396 System.out.println("BeanInfo getBeanInfo(Class)"); 1397 BeanInfo info = utils.getBeanInfo(clazz); 1398 1399 System.out.println("String getDescriptionFirstSentence(FeatureDescriptor)"); 1400 System.out.println(utils.getDescriptionFirstSentence(info.getBeanDescriptor())); 1401 1402 System.out.println("String getFirstSentence(String)"); 1403 1404 System.out.println(utils.getFirstSentence("Hello World. Hi!")); 1405 1406 1407 System.out.println("String getFullClassName(Class)"); 1408 1409 System.out.println(utils.getFullClassName(clazz)); 1410 1411 System.out.println("String getFullClassName(String)"); 1412 System.out.println(utils.getFullClassName(className)); 1413 1414 System.out.println("String getClassName(Class)"); 1415 1416 System.out.println(utils.getClassName(clazz)); 1417 1418 System.out.println("String getClassName(String)"); 1419 System.out.println(utils.getClassName(className)); 1420 1421 System.out.println("String getClassPackage(Class)"); 1422 System.out.println(utils.getClassPackage(clazz)); 1423 1424 System.out.println("String getClassPackage(String)"); 1425 System.out.println(utils.getClassPackage(className)); 1426 1427 System.out.println("String getTeaFullClassName(Class)"); 1428 System.out.println(utils.getTeaFullClassName(clazz)); 1429 1430 System.out.println("boolean isImplicitTeaImport(Class)"); 1431 1432 System.out.println(utils.isImplicitTeaImport(clazz)); 1433 1434 System.out.println("MethodDescriptor[] getTeaContextMethodDescriptors(Class)"); 1435 1436 MethodDescriptor[] mds = 1437 utils.getTeaContextMethodDescriptors(clazz); 1438 1439 printMethodDescriptors(mds, utils); 1440 1441 System.out.println("MethodDescriptor[] getTeaContextMethodDescriptors(Class, boolean)"); 1442 1443 1444 mds = utils.getTeaContextMethodDescriptors(clazz, true); 1445 printMethodDescriptors(mds, utils); 1446 1447 System.out.println("MethodDescriptor[] getTeaBeanPropertyDescriptors(Class)"); 1448 1449 PropertyDescriptor[] pds = 1450 utils.getTeaBeanPropertyDescriptors(clazz); 1451 1452 printPropertyDescriptors(pds, utils); 1453 1454 } 1455 1456 if (args.length > 1) { 1457 System.out.println(""); 1458 System.out.println(""); 1459 System.out.println("MethodDescriptor[] getTeaContextMethodDescriptors(Class[])"); 1460 1461 Class [] classes = new Class [args.length]; 1462 for (int i = 0; i < classes.length; i++) { 1463 classes[i] = utils.getClassForName(args[i]); 1464 } 1465 1466 MethodDescriptor[] mds = 1467 utils.getTeaContextMethodDescriptors(classes); 1468 printMethodDescriptors(mds, utils); 1469 } 1470 } 1471 1472 1473 private void printMethodDescriptors(MethodDescriptor[] mds, 1474 TeaToolsUtils utils) { 1475 1476 for (int i = 0; i < mds.length; i++) { 1477 printMethodDescriptor(mds[i], utils); 1478 } 1479 } 1480 1481 private void printMethodDescriptor(MethodDescriptor md, 1482 TeaToolsUtils utils) { 1483 1484 printlnIndented("Method: " + md.getName(), 2); 1485 String desc = utils.getDescriptionFirstSentence(md); 1486 if (desc.length() > 0) { 1487 printlnIndented(desc, 8); 1488 } 1489 } 1490 1491 1492 private void printPropertyDescriptors(PropertyDescriptor[] pds, 1493 TeaToolsUtils utils) { 1494 1495 for (int i = 0; i < pds.length; i++) { 1496 printPropertyDescriptor(pds[i], utils); 1497 } 1498 } 1499 1500 private void printPropertyDescriptor(PropertyDescriptor pd, 1501 TeaToolsUtils utils) { 1502 1503 printlnIndented("Property: " + pd.getName(), 2); 1504 String desc = utils.getDescriptionFirstSentence(pd); 1505 if (desc.length() > 0) { 1506 printlnIndented(desc, 8); 1507 } 1508 } 1509 1510 1511 1512 private void printlnIndented(Object o, int indent) { 1513 StringBuffer sb = new StringBuffer (); 1514 for (int i = 0; i < indent; i++) { 1515 sb.append(' '); 1516 } 1517 1518 System.out.println(sb.toString() + o); 1519 } 1520 1521 } 1522} 1523 | Popular Tags |