| 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 
|