1 16 19 package org.apache.xml.utils.synthetic; 20 21 import java.lang.reflect.Modifier ; 22 23 import org.apache.xml.utils.synthetic.reflection.Constructor; 24 import org.apache.xml.utils.synthetic.reflection.Field; 25 import org.apache.xml.utils.synthetic.reflection.Method; 26 27 39 40 74 public class Class extends Object implements java.io.Serializable 75 { 76 77 78 private static java.util.Hashtable global_classtable = 79 new java.util.Hashtable (); 80 81 83 private java.lang.String name; 84 85 94 private java.lang.Class realclass = null; 95 96 100 private int modifiers; 101 102 105 private boolean isInterface = false; 106 107 114 private Class superclass = null; 115 116 121 private Class declaringclass = null; 122 123 127 private Class [] interfaces = new Class [0]; 128 129 134 private Class [] allclasses = new Class [0]; 135 136 141 private Class [] declaredclasses = new Class [0]; 142 143 151 private Constructor[] allconstructors = new Constructor[0]; 152 153 159 private Constructor[] declaredconstructors = new Constructor[0]; 160 161 163 private Method[] allmethods = new Method[0]; 164 165 167 private Method[] declaredmethods = new Method[0]; 168 169 171 private Field[] allfields = new Field[0]; 172 173 175 private Field[] declaredfields = new Field[0]; 176 177 179 private Class [] innerclasses = new Class [0]; 180 181 190 Class(java.lang.Class realclass) 191 { 192 193 this(realclass.getName()); 194 195 try 196 { 197 setRealClass(realclass); 198 } 199 catch (SynthesisException e) 200 { 201 e.printStackTrace(); 202 } 203 } 204 205 215 Class(String fullname) 216 { 217 218 this.name = fullname; 219 220 global_classtable.put(fullname, this); 221 } 222 223 245 public static Class forClass(java.lang.Class cls) 246 { 247 248 if (cls == null) 249 return null; 250 251 Class ret = (Class ) (global_classtable.get(cls.getName())); 252 253 if (null == ret) 254 ret = new Class (cls); 255 256 return ret; 257 } 258 259 271 public Class forNameInContext(String classname) 272 throws ClassNotFoundException 273 { 274 275 for (int i = innerclasses.length - 1; i >= 0; --i) 276 { 277 if (classname.equals(innerclasses[i].getShortName())) 278 return innerclasses[i]; 279 } 280 281 return forName(classname); 282 } 283 284 309 public static Class forName(String className) throws ClassNotFoundException 310 { 311 312 if (className.endsWith("]")) 316 { 317 StringBuffer arrayname = new StringBuffer (); 318 319 for (int i = className.indexOf('['); i != -1; 320 i = className.indexOf('[', i + 1)) 321 { 322 arrayname.append('['); 323 } 324 325 String classname = className.substring(0, className.indexOf('[')); 330 331 if ("byte".equals(classname)) 332 arrayname.append('B'); 333 else if ("char".equals(classname)) 334 arrayname.append('C'); 335 else if ("double".equals(classname)) 336 arrayname.append('D'); 337 else if ("float".equals(classname)) 338 arrayname.append('F'); 339 else if ("int".equals(classname)) 340 arrayname.append('I'); 341 else if ("long".equals(classname)) 342 arrayname.append('J'); 343 else if ("short".equals(classname)) 344 arrayname.append('S'); 345 else if ("boolean".equals(classname)) 346 arrayname.append('Z'); 347 else 348 arrayname.append('L').append(classname).append(';'); 349 350 return forName(arrayname.toString()); 352 } 353 354 Class ret = (Class ) (global_classtable.get(className)); 355 356 if (null == ret) 357 { 358 359 if ("boolean".equals(className)) 363 { 364 ret = new Class (className); 365 ret.realclass = java.lang.Boolean.TYPE; 366 } 367 else if ("byte".equals(className)) 368 { 369 ret = new Class (className); 370 ret.realclass = java.lang.Byte.TYPE; 371 } 372 else if ("char".equals(className)) 373 { 374 ret = new Class (className); 375 ret.realclass = java.lang.Character.TYPE; 376 } 377 else if ("short".equals(className)) 378 { 379 ret = new Class (className); 380 ret.realclass = java.lang.Short.TYPE; 381 } 382 else if ("int".equals(className)) 383 { 384 ret = new Class (className); 385 ret.realclass = java.lang.Integer.TYPE; 386 } 387 else if ("long".equals(className)) 388 { 389 ret = new Class (className); 390 ret.realclass = java.lang.Long.TYPE; 391 } 392 else if ("float".equals(className)) 393 { 394 ret = new Class (className); 395 ret.realclass = java.lang.Float.TYPE; 396 } 397 else if ("double".equals(className)) 398 { 399 ret = new Class (className); 400 ret.realclass = java.lang.Double.TYPE; 401 } 402 else if ("void".equals(className)) 403 { 404 405 ret = new Class (className); 408 ret.realclass = java.lang.Class.forName("java.lang.Object"); 409 } 410 411 else 415 ret = new Class (java.lang.Class.forName(className)); 416 } 417 418 return ret; 419 } 420 421 431 public static Class declareClass(String className) throws SynthesisException 432 { 433 434 Class ret = (Class ) (global_classtable.get(className)); 435 436 if (null == ret) 437 ret = new Class (className); 438 439 if (ret.realclass != null) 440 throw new SynthesisException(SynthesisException.REIFIED); 441 442 return ret; 443 } 444 445 457 public static Class reallyDeclareClass(String className) 458 { 459 460 Class ret = (Class ) (global_classtable.get(className)); 461 462 if (null != ret) 463 global_classtable.remove(ret); 464 465 ret = new Class (className); 466 467 return ret; 468 } 469 470 497 public Class [] getClasses() 498 { 499 500 if (realclass != null && allclasses == null) 501 { 502 java.lang.Class [] realDE = realclass.getClasses(); 503 504 allclasses = new Class [realDE.length]; 505 506 for (int i = 0; i < realDE.length; ++i) 507 { 508 allclasses[i] = forClass(realDE[i]); 509 } 510 } 511 512 return allclasses; 513 } 514 515 522 public ClassLoader getClassLoader() 523 { 524 return (realclass == null) ? null : realclass.getClassLoader(); 525 } 526 527 539 public Class getComponentType() 540 { 541 return realclass == null ? null : new Class (realclass.getComponentType()); 542 } 543 544 572 public Constructor getConstructor(Class parameterTypes[]) 573 throws NoSuchMethodException , SecurityException , SynthesisException 574 { 575 576 if (realclass == null) 577 throw new SynthesisException(SynthesisException.UNREIFIED); 578 579 java.lang.Class [] real = new java.lang.Class [parameterTypes.length]; 580 581 for (int i = 0; i < parameterTypes.length; ++i) 582 { 583 if ((real[i] = parameterTypes[i].getRealClass()) == null) 584 throw new SynthesisException(SynthesisException.UNREIFIED); 585 } 586 587 return new Constructor(realclass.getConstructor(real), this); 588 } 589 590 605 public Constructor[] getConstructors() throws SecurityException 606 { 607 608 if (realclass != null && allconstructors == null) 609 { 610 java.lang.reflect.Constructor [] realDC = realclass.getConstructors(); 611 612 allconstructors = new Constructor[realDC.length]; 613 614 for (int i = 0; i < realDC.length; ++i) 615 { 616 allconstructors[i] = new Constructor(realDC[i], this); 617 } 618 } 619 620 return allconstructors; 621 } 622 623 644 public Class [] getDeclaredClasses() throws SecurityException 645 { 646 647 if (realclass != null && declaredclasses == null) 649 { 650 java.lang.Class [] realDE = realclass.getDeclaredClasses(); 651 652 declaredclasses = new Class [realDE.length]; 653 654 for (int i = 0; i < realDE.length; ++i) 655 { 656 declaredclasses[i] = forClass(realDE[i]); 657 658 if (!realDE[i].isInterface()) 659 superclass = declaredclasses[i]; 660 } 661 } 662 663 return declaredclasses; 664 } 665 666 674 public void addExtends(Class newclass) throws SynthesisException 675 { 676 677 if (realclass != null) 678 throw new SynthesisException(SynthesisException.REIFIED); 679 680 Class [] scratch = new Class [declaredclasses.length + 1]; 681 682 System.arraycopy(declaredclasses, 0, scratch, 0, declaredclasses.length); 683 684 scratch[declaredclasses.length] = newclass; 685 declaredclasses = scratch; 686 } 687 688 710 public Constructor getDeclaredConstructor(Class parameterTypes[]) 711 throws NoSuchMethodException , SecurityException 712 { 713 throw new java.lang.IllegalStateException (); 714 } 715 716 725 public Constructor declareConstructor() throws SynthesisException 726 { 727 728 if (realclass != null) 729 throw new SynthesisException(SynthesisException.REIFIED); 730 731 Constructor newctor = new Constructor(this); 732 Constructor[] scratch = new Constructor[declaredconstructors.length + 1]; 733 734 System.arraycopy(declaredconstructors, 0, scratch, 0, 735 declaredconstructors.length); 736 737 scratch[declaredconstructors.length] = newctor; 738 declaredconstructors = scratch; 739 scratch = new Constructor[allconstructors.length + 1]; 740 741 System.arraycopy(allconstructors, 0, scratch, 0, allconstructors.length); 742 743 scratch[allconstructors.length] = newctor; 744 allconstructors = scratch; 745 746 return newctor; 747 } 748 749 760 public Class declareInterface(Class newifce) throws SynthesisException 761 { 762 763 if (realclass != null) 764 throw new SynthesisException(SynthesisException.REIFIED); 765 766 if (!newifce.isInterface()) 767 throw new SynthesisException(SynthesisException.SYNTAX, 768 newifce.getName() + " isn't an interface"); 769 770 Class [] scratch = new Class [interfaces.length + 1]; 771 772 System.arraycopy(interfaces, 0, scratch, 0, interfaces.length); 773 774 scratch[interfaces.length] = newifce; 775 interfaces = scratch; 776 scratch = new Class [allclasses.length + 1]; 777 778 System.arraycopy(allclasses, 0, scratch, 0, allclasses.length); 779 780 scratch[allclasses.length] = newifce; 781 allclasses = scratch; 782 783 return newifce; 784 } 785 786 805 public Constructor[] getDeclaredConstructors() throws SecurityException 806 { 807 808 if (realclass != null && declaredconstructors == null) 809 { 810 java.lang.reflect.Constructor [] realDC = 811 realclass.getDeclaredConstructors(); 812 813 declaredconstructors = new Constructor[realDC.length]; 814 815 for (int i = 0; i < realDC.length; ++i) 816 { 817 declaredconstructors[i] = new Constructor(realDC[i], this); 818 } 819 } 820 821 return declaredconstructors; 822 } 823 824 844 public Field getDeclaredField(String name) 845 throws NoSuchFieldException , SecurityException 846 { 847 throw new java.lang.IllegalStateException (); 848 } 849 850 862 public Field declareField(String name) throws SynthesisException 863 { 864 865 if (realclass != null) 866 throw new SynthesisException(SynthesisException.REIFIED); 867 868 Field newfield = new Field(name, this); 869 Field[] scratch = new Field[declaredfields.length + 1]; 870 871 System.arraycopy(declaredfields, 0, scratch, 0, declaredfields.length); 872 873 scratch[declaredfields.length] = newfield; 874 declaredfields = scratch; 875 scratch = new Field[allfields.length + 1]; 876 877 System.arraycopy(allfields, 0, scratch, 0, allfields.length); 878 879 scratch[allfields.length] = newfield; 880 allfields = scratch; 881 882 return newfield; 883 } 884 885 904 public Field[] getDeclaredFields() throws SecurityException 905 { 906 907 if (realclass != null && declaredfields == null) 908 { 909 java.lang.reflect.Field [] realDF = realclass.getDeclaredFields(); 910 911 declaredfields = new Field[realDF.length]; 912 913 for (int i = 0; i < realDF.length; ++i) 914 { 915 declaredfields[i] = new Field(realDF[i], this); 916 } 917 } 918 919 return declaredfields; 920 } 921 922 949 public Method getDeclaredMethod(String name, Class parameterTypes[]) 950 throws NoSuchMethodException , SecurityException 951 { 952 throw new java.lang.IllegalStateException (); 953 } 954 955 967 public Method declareMethod(String name) throws SynthesisException 968 { 969 970 if (realclass != null) 971 throw new SynthesisException(SynthesisException.REIFIED); 972 973 Method newMethod = new Method(name, this); 974 Method[] scratch = new Method[declaredmethods.length + 1]; 975 976 System.arraycopy(declaredmethods, 0, scratch, 0, declaredmethods.length); 977 978 scratch[declaredmethods.length] = newMethod; 979 declaredmethods = scratch; 980 scratch = new Method[allmethods.length + 1]; 981 982 System.arraycopy(allmethods, 0, scratch, 0, allmethods.length); 983 984 scratch[allmethods.length] = newMethod; 985 allmethods = scratch; 986 987 return newMethod; 988 } 989 990 1009 public Method[] getDeclaredMethods() throws SecurityException 1010 { 1011 1012 if (realclass != null && declaredmethods == null) 1013 { 1014 java.lang.reflect.Method [] realDM = realclass.getDeclaredMethods(); 1015 1016 declaredmethods = new Method[realDM.length]; 1017 1018 for (int i = 0; i < realDM.length; ++i) 1019 { 1020 declaredmethods[i] = new Method(realDM[i], this); 1021 } 1022 } 1023 1024 return declaredmethods; 1025 } 1026 1027 1038 public Class getDeclaringClass() 1039 { 1040 1041 if (realclass != null && declaringclass == null) 1042 { 1043 java.lang.Class dc = realclass.getDeclaringClass(); 1044 1045 if (dc == null) 1046 declaringclass = null; 1047 else 1048 declaringclass = forClass(dc); 1049 } 1050 1051 return declaringclass; 1052 } 1053 1054 1061 private void addInnerClass(Class newclass) throws SynthesisException 1062 { 1063 1064 if (realclass != null) 1065 throw new SynthesisException(SynthesisException.REIFIED); 1066 1067 if (newclass.getDeclaringClass() != this) 1068 throw new SynthesisException(SynthesisException.WRONG_OWNER); 1069 1070 Class [] scratch = new Class [innerclasses.length + 1]; 1071 1072 System.arraycopy(innerclasses, 0, scratch, 0, innerclasses.length); 1073 1074 scratch[innerclasses.length] = newclass; 1075 innerclasses = scratch; 1076 } 1077 1078 1096 public Class declareInnerClass(String className) throws SynthesisException 1097 { 1098 1099 if (realclass != null) 1100 throw new SynthesisException(SynthesisException.REIFIED); 1101 1102 String relativeName = getName() + "$" + className; 1103 Class newclass = (Class ) (global_classtable.get(relativeName)); 1104 1105 if (newclass != null) 1106 throw new SynthesisException(SynthesisException.SYNTAX, 1107 "Inner class " + name + " already exists"); 1108 1109 newclass = new Class (className); 1110 newclass.declaringclass = this; 1111 1112 Class [] scratch = new Class [innerclasses.length + 1]; 1113 1114 System.arraycopy(innerclasses, 0, scratch, 0, innerclasses.length); 1115 1116 scratch[innerclasses.length] = newclass; 1117 innerclasses = scratch; 1118 1119 return newclass; 1120 } 1121 1122 1132 public Class [] getInnerClasses() 1133 { 1134 return innerclasses; 1135 } 1136 1137 1161 public Field getField(String name) 1162 throws NoSuchFieldException , SecurityException 1163 { 1164 throw new java.lang.IllegalStateException (); 1165 } 1166 1167 1194 public Field[] getFields() throws SecurityException 1195 { 1196 1197 if (realclass != null && allfields == null) 1198 { 1199 java.lang.reflect.Field [] realDF = realclass.getFields(); 1200 1201 allfields = new Field[realDF.length]; 1202 1203 for (int i = 0; i < realDF.length; ++i) 1204 { 1205 allfields[i] = new Field(realDF[i], this); 1206 } 1207 } 1208 1209 return allfields; 1210 } 1211 1212 1238 public Class [] getInterfaces() 1239 { 1240 1241 if (realclass != null && interfaces == null) 1242 { 1243 java.lang.Class [] realI = realclass.getInterfaces(); 1244 1245 interfaces = new Class [realI.length]; 1246 1247 for (int i = 0; i < realI.length; ++i) 1248 { 1249 interfaces[i] = forClass(realI[i]); 1250 } 1251 } 1252 1253 return interfaces; 1254 } 1255 1256 1265 public void addImplements(Class newclass) throws SynthesisException 1266 { 1267 1268 if (realclass != null) 1269 throw new SynthesisException(SynthesisException.REIFIED); 1270 1271 Class [] scratch = new Class [interfaces.length + 1]; 1272 1273 System.arraycopy(interfaces, 0, scratch, 0, interfaces.length); 1274 1275 scratch[interfaces.length] = newclass; 1276 interfaces = scratch; 1277 } 1278 1279 1307 public Method getMethod(String name, Class parameterTypes[]) 1308 throws NoSuchMethodException , SecurityException 1309 { 1310 throw new java.lang.IllegalStateException (); 1311 } 1312 1313 1329 public Method[] getMethods() throws SecurityException 1330 { 1331 1332 if (realclass != null && allmethods == null) 1333 { 1334 java.lang.reflect.Method [] realDM = realclass.getMethods(); 1335 1336 allmethods = new Method[realDM.length]; 1337 1338 for (int i = 0; i < realDM.length; ++i) 1339 { 1340 allmethods[i] = new Method(realDM[i], this); 1341 } 1342 } 1343 1344 return allmethods; 1345 } 1346 1347 1362 public int getModifiers() 1363 { 1364 return modifiers; 1365 } 1366 1367 1385 public void setModifiers(int modifiers) throws SynthesisException 1386 { 1387 1388 if (this.realclass != null) 1389 throw new SynthesisException(SynthesisException.REIFIED); 1390 1391 this.modifiers = modifiers; 1392 } 1393 1394 1401 public java.lang.String getName() 1402 { 1403 return name; 1404 } 1405 1406 1413 public java.lang.String getJavaName() 1414 { 1415 1416 if (name.charAt(0) != '[') 1417 return name; 1418 1419 int count = name.lastIndexOf('['); 1422 StringBuffer jname = new StringBuffer (name.substring(count + 2)); 1423 1424 jname.setLength(jname.length() - 1); 1426 1427 while (count-- >= 0) 1428 { 1429 jname.append("[]"); 1430 } 1431 1432 return jname.toString(); 1433 } 1434 1435 1444 public java.lang.String getShortName() 1445 { 1446 1447 int start = name.lastIndexOf("."); 1448 1449 if (start != 0 || name.charAt(0) == '.') 1450 ++start; 1451 1452 if (declaringclass != null) 1453 { 1454 int d = name.lastIndexOf('$', start); 1455 1456 if (d != 0) 1457 start = d + 1; 1458 } 1459 1460 return name.substring(start); 1461 } 1462 1463 1470 public java.lang.String getJavaShortName() 1471 { 1472 1473 String shortname = getShortName(); 1474 1475 if (shortname.charAt(0) != '[') 1476 return shortname; 1477 1478 int count = shortname.lastIndexOf('['); 1481 StringBuffer jname = new StringBuffer (shortname.substring(count + 2)); 1482 1483 jname.setLength(jname.length() - 1); 1485 1486 while (count-- >= 0) 1487 { 1488 jname.append("[]"); 1489 } 1490 1491 return jname.toString(); 1492 } 1493 1494 1501 public java.lang.String getPackageName() 1502 { 1503 1504 int start = name.lastIndexOf("."); 1505 1506 return name.substring(0, start); 1507 } 1508 1509 1517 public java.lang.Class getRealClass() 1518 { 1519 return realclass; 1520 } 1521 1522 1547 public void setRealClass(java.lang.Class realclass) 1548 throws SynthesisException 1549 { 1550 1551 if (this.realclass != null) 1552 throw new SynthesisException(SynthesisException.REIFIED); 1553 1554 this.realclass = realclass; 1555 this.modifiers = realclass.getModifiers(); 1556 this.isInterface = realclass.isInterface(); 1557 1558 this.declaringclass = null; 1560 this.interfaces = null; 1561 this.declaredconstructors = null; 1562 this.allconstructors = null; 1563 this.declaredmethods = null; 1564 this.allmethods = null; 1565 this.declaredfields = null; 1566 this.allfields = null; 1567 this.declaredclasses = null; 1568 this.allclasses = null; 1569 this.superclass = null; 1570 } 1571 1572 1582 public void setSuperClass(Class superclass) throws SynthesisException 1583 { 1584 1585 if (realclass != null) 1586 throw new SynthesisException(SynthesisException.REIFIED); 1587 1588 this.superclass = superclass; 1589 } 1590 1591 1601 public void setSuperClass(java.lang.Class superclass) 1602 throws ClassNotFoundException , SynthesisException 1603 { 1604 1605 if (realclass != null) 1606 throw new SynthesisException(SynthesisException.REIFIED); 1607 1608 this.superclass = Class.forClass(superclass); 1609 } 1610 1611 1630 public java.net.URL getResource(String name) 1631 { 1632 throw new java.lang.IllegalStateException (); 1633 } 1634 1635 1655 public java.io.InputStream getResourceAsStream(String name) 1656 { 1657 throw new java.lang.IllegalStateException (); 1658 } 1659 1660 1664 public Object [] getSigners() 1665 { 1666 throw new java.lang.IllegalStateException (); 1667 } 1668 1669 1681 public Class getSuperclass() 1682 { 1683 1684 if (realclass != null && superclass == null) 1685 { 1686 superclass = forClass(realclass.getSuperclass()); 1687 1688 } 1690 1691 if (superclass == null) 1692 superclass = forClass(Object .class); 1693 1694 return superclass; 1695 } 1696 1697 1702 public boolean isArray() 1703 { 1704 return realclass != null && realclass.isArray(); 1705 } 1706 1707 1729 public boolean isAssignableFrom(Class cls) 1730 { 1731 1732 if (realclass != null && cls.realclass != null) 1733 return realclass.isAssignableFrom(cls.realclass); 1734 1735 throw new java.lang.IllegalStateException (); 1736 } 1737 1738 1760 public boolean isAssignableFrom(java.lang.Class cls) 1761 { 1762 1763 if (realclass != null) 1764 return realclass.isAssignableFrom((java.lang.Class ) cls); 1765 1766 throw new java.lang.IllegalStateException (); 1767 } 1768 1769 1794 public boolean isInstance(Object obj) 1795 { 1796 1797 if (realclass != null) 1798 return realclass.isInstance(obj); 1799 1800 throw new java.lang.IllegalStateException (); 1803 } 1804 1805 1812 public boolean isInterface() 1813 { 1814 return (realclass != null) ? realclass.isInterface() : isInterface; 1815 } 1816 1817 1829 public void isInterface(boolean isInterface) throws SynthesisException 1830 { 1831 1832 if (realclass == null) 1833 this.isInterface = isInterface; 1834 else if (realclass.isInterface() != isInterface) 1835 throw new SynthesisException(SynthesisException.REIFIED); 1836 } 1837 1838 1855 public boolean isPrimitive() 1856 { 1857 return realclass != null && realclass.isPrimitive(); 1858 } 1859 1860 1874 public Object newInstance() 1875 throws InstantiationException , IllegalAccessException 1876 { 1877 throw new java.lang.IllegalStateException (); 1878 } 1879 1880 1894 public String toString() 1895 { 1896 1897 if (realclass != null) 1898 return realclass.toString(); 1899 else if (isInterface()) 1900 return "interface " + name; 1901 else 1902 return "class " + name; 1903 } 1904 1905 1911 public void toSource(java.io.OutputStream out, int depth) 1912 { 1913 1914 java.io.PrintWriter writer = new java.io.PrintWriter (out); 1915 1916 toSource(writer, depth); 1917 } 1918 1919 1929 public void toSource(java.io.PrintWriter out, int depth) 1930 { 1931 1932 String tab = tabset(depth); 1933 1934 if (realclass != null) 1935 out.println( 1936 tab 1937 + "/** Code back-generated from a \"real\" Class; accuracy limited by reflection APIs. */"); 1938 else 1939 out.println( 1940 tab 1941 + "/** Code generated via org.apache.xml.utils.synthetic.Class */"); 1942 1943 1944 if (getDeclaringClass() == null) 1945 out.println(tab + "package " + getPackageName() + ";"); 1946 1947 out.print(tab + Modifier.toString(getModifiers())); 1948 1949 if (isInterface()) 1950 out.print(" interface "); 1951 else 1952 out.print(" class "); 1953 1954 out.println(getJavaShortName()); 1955 1956 if (superclass != null) 1957 { 1958 out.print('\n' + tab + " extends " + superclass.getJavaName()); 1959 } 1960 1961 Class [] ext = getInterfaces(); 1962 1963 if (ext != null & ext.length > 0) 1964 { 1965 1966 out.print('\n' + tab + (isInterface ? " extends " : " implements ") 1969 + ext[0].getName()); 1970 1971 for (int i = 1; i < ext.length; ++i) 1972 { 1973 out.print(", " + ext[i].getJavaName()); 1974 } 1975 1976 out.print("\n"); 1977 } 1978 1979 out.print(tab + "{\n"); 1980 1981 tab = tabset(++depth); 1982 1983 Field[] fields = null; 1985 1986 try 1987 { 1988 fields = getDeclaredFields(); 1989 } 1990 catch (SecurityException e) 1991 { 1992 out.println(tab + "//SecurityException retrieving fields"); 1993 } 1994 1995 if (fields != null) 1996 { 1997 for (int i = 0; i < fields.length; ++i) 1998 { 1999 out.println(tab + fields[i].toSource()); 2000 } 2001 } 2002 2003 Constructor[] ctors = null; 2005 2006 try 2007 { 2008 ctors = getDeclaredConstructors(); 2009 } 2010 catch (SecurityException e) 2011 { 2012 out.println(tab + "//SecurityException retrieving ctors"); 2013 } 2014 2015 if (ctors != null) 2016 { 2017 for (int i = 0; i < ctors.length; ++i) 2018 { 2019 out.print(ctors[i].toSource(tab)); 2020 } 2021 } 2022 2023 Method[] methods = null; 2025 2026 try 2027 { 2028 methods = getDeclaredMethods(); 2029 } 2030 catch (SecurityException e) 2031 { 2032 out.println(tab + "//SecurityException retrieving methods"); 2033 } 2034 2035 if (methods != null) 2036 { 2037 for (int i = 0; i < methods.length; ++i) 2038 { 2039 out.print('\n'); 2040 out.print(methods[i].toSource(tab)); 2041 } 2042 } 2043 2044 Class [] inners = getInnerClasses(); 2046 2047 if (inners != null) 2048 { 2049 for (int i = 0; i < inners.length; ++i) 2050 { 2051 out.print('\n'); 2052 inners[i].toSource(out, depth); 2053 } 2054 } 2055 2056 tab = tabset(--depth); 2058 2059 out.print(tab + "}\n"); 2060 out.flush(); 2061 } 2062 2063 2071 private String tabset(int depth) 2072 { 2073 2074 StringBuffer t = new StringBuffer (); 2075 2076 while (depth-- > 0) 2077 { 2078 t.append(" "); 2079 } 2080 2081 return t.toString(); 2082 } 2083 2084 2086 2087 static final int[] val = { Modifier.ABSTRACT, Modifier.FINAL, 2088 Modifier.INTERFACE, Modifier.NATIVE, 2089 Modifier.PRIVATE, Modifier.PROTECTED, 2090 Modifier.PUBLIC, Modifier.STATIC, 2091 Modifier.SYNCHRONIZED, Modifier.TRANSIENT, 2092 Modifier.VOLATILE }; 2093 2094 2095 static final String [] kwd = { "abstract", "final", "interface", "native", 2096 "private", "protected", "public", "static", 2097 "synchronized", "transient", "volatile" }; 2098 2099 2107 static public int modifierFromString(String t) 2108 { 2109 2110 for (int i = 0; i < kwd.length; ++i) 2111 { 2112 if (kwd[i].equals(t)) 2113 return val[i]; 2114 } 2115 2116 return 0; 2117 } 2118 2119 2127 static public int modifiersFromString(String s) 2128 { 2129 2130 int mods = 0; 2131 java.util.StringTokenizer parts = new java.util.StringTokenizer (s); 2132 2133 while (parts.hasMoreTokens()) 2134 { 2135 String t = parts.nextToken(); 2136 2137 mods |= modifierFromString(t); 2138 } 2139 2140 return mods; 2141 } 2142} 2143 | Popular Tags |