1 15 16 package javassist; 17 18 import javassist.bytecode.ClassFile; 19 import javassist.bytecode.Descriptor; 20 import javassist.bytecode.Opcode; 21 import javassist.expr.ExprEditor; 22 23 import java.io.BufferedOutputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.DataOutputStream ; 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.OutputStream ; 30 import java.net.URL ; 31 import java.util.Collection ; 32 33 35 41 public abstract class CtClass { 42 protected String qualifiedName; 43 44 47 public static final String version = "3.0"; 48 49 56 public static void main(String [] args) { 57 System.out.println("Javassist version " + CtClass.version); 58 System.out.println("Copyright (C) 1999-2005 Shigeru Chiba." 59 + " All Rights Reserved."); 60 } 61 62 static final String javaLangObject = "java.lang.Object"; 63 64 68 public static CtClass booleanType; 69 70 74 public static CtClass charType; 75 76 80 public static CtClass byteType; 81 82 86 public static CtClass shortType; 87 88 92 public static CtClass intType; 93 94 98 public static CtClass longType; 99 100 104 public static CtClass floatType; 105 106 110 public static CtClass doubleType; 111 112 116 public static CtClass voidType; 117 118 static CtClass[] primitiveTypes; 119 120 static { 121 primitiveTypes = new CtClass[9]; 122 123 booleanType = 124 new CtPrimitiveType("boolean", 'Z', "java.lang.Boolean", 125 "booleanValue", "()Z", Opcode.IRETURN, 126 Opcode.T_BOOLEAN, 1); 127 primitiveTypes[0] = booleanType; 128 129 charType = new CtPrimitiveType("char", 'C', "java.lang.Character", 130 "charValue", "()C", Opcode.IRETURN, 131 Opcode.T_CHAR, 1); 132 primitiveTypes[1] = charType; 133 134 byteType = new CtPrimitiveType("byte", 'B', "java.lang.Byte", 135 "byteValue", "()B", Opcode.IRETURN, 136 Opcode.T_BYTE, 1); 137 primitiveTypes[2] = byteType; 138 139 shortType = new CtPrimitiveType("short", 'S', "java.lang.Short", 140 "shortValue", "()S", Opcode.IRETURN, 141 Opcode.T_SHORT, 1); 142 primitiveTypes[3] = shortType; 143 144 intType = new CtPrimitiveType("int", 'I', "java.lang.Integer", 145 "intValue", "()I", Opcode.IRETURN, 146 Opcode.T_INT, 1); 147 primitiveTypes[4] = intType; 148 149 longType = new CtPrimitiveType("long", 'J', "java.lang.Long", 150 "longValue", "()J", Opcode.LRETURN, 151 Opcode.T_LONG, 2); 152 primitiveTypes[5] = longType; 153 154 floatType = new CtPrimitiveType("float", 'F', "java.lang.Float", 155 "floatValue", "()F", Opcode.FRETURN, 156 Opcode.T_FLOAT, 1); 157 primitiveTypes[6] = floatType; 158 159 doubleType = new CtPrimitiveType("double", 'D', "java.lang.Double", 160 "doubleValue", "()D", Opcode.DRETURN, 161 Opcode.T_DOUBLE, 2); 162 primitiveTypes[7] = doubleType; 163 164 voidType = new CtPrimitiveType("void", 'V', "java.lang.Void", 165 null, null, Opcode.RETURN, 0, 0); 166 primitiveTypes[8] = voidType; 167 } 168 169 protected CtClass(String name) { 170 qualifiedName = name; 171 } 172 173 176 public String toString() { 177 StringBuffer buf = new StringBuffer (getClass().getName()); 178 buf.append("@"); 179 buf.append(Integer.toHexString(hashCode())); 180 buf.append("["); 181 extendToString(buf); 182 buf.append("]"); 183 return buf.toString(); 184 } 185 186 190 protected void extendToString(StringBuffer buffer) { 191 buffer.append(getName()); 192 } 193 194 197 public ClassPool getClassPool() { return null; } 198 199 205 public ClassFile getClassFile() { 206 checkModify(); 207 return getClassFile2(); 208 } 209 210 213 public ClassFile getClassFile2() { return null; } 214 215 218 public javassist.compiler.AccessorMaker getAccessorMaker() { 219 return null; 220 } 221 222 225 public URL getURL() throws NotFoundException { 226 throw new NotFoundException(getName()); 227 } 228 229 232 public boolean isModified() { return false; } 233 234 240 public boolean isFrozen() { return true; } 241 242 void freeze() {} 243 244 void checkModify() throws RuntimeException { 245 if (isFrozen()) 246 throw new RuntimeException (getName() + " class is frozen"); 247 248 } 250 251 265 public void defrost() { 266 throw new RuntimeException ("cannot defrost " + getName()); 267 } 268 269 289 public void stopPruning(boolean stop) {} 290 291 294 void incGetCounter() {} 295 296 301 public boolean isPrimitive() { return false; } 302 303 306 public boolean isArray() { 307 return false; 308 } 309 310 314 public CtClass getComponentType() throws NotFoundException { 315 return null; 316 } 317 318 323 public boolean subtypeOf(CtClass clazz) throws NotFoundException { 324 return this == clazz || getName().equals(clazz.getName()); 325 } 326 327 330 public String getName() { return qualifiedName; } 331 332 335 public final String getSimpleName() { 336 String qname = qualifiedName; 337 int index = qname.lastIndexOf('.'); 338 if (index < 0) 339 return qname; 340 else 341 return qname.substring(index + 1); 342 } 343 344 347 public final String getPackageName() { 348 String qname = qualifiedName; 349 int index = qname.lastIndexOf('.'); 350 if (index < 0) 351 return null; 352 else 353 return qname.substring(0, index); 354 } 355 356 361 public void setName(String name) { 362 checkModify(); 363 if (name != null) 364 qualifiedName = name; 365 } 366 367 374 public void replaceClassName(String oldName, String newName) { 375 checkModify(); 376 } 377 378 395 public void replaceClassName(ClassMap map) { 396 checkModify(); 397 } 398 399 406 public Collection getRefClasses() { 407 ClassFile cf = getClassFile2(); 408 if (cf != null) { 409 ClassMap cm = new ClassMap() { 410 public void put(String oldname, String newname) { 411 put0(oldname, newname); 412 } 413 414 public Object get(Object jvmClassName) { 415 String n = toJavaName((String )jvmClassName); 416 put0(n, n); 417 return null; 418 } 419 420 public void fix(String name) {} 421 }; 422 cf.renameClass(cm); 423 return cm.values(); 424 } 425 else 426 return null; 427 } 428 429 433 public boolean isInterface() { 434 return false; 435 } 436 437 443 public int getModifiers() { 444 return 0; 445 } 446 447 454 public void setModifiers(int mod) { 455 checkModify(); 456 } 457 458 466 public boolean subclassOf(CtClass superclass) { 467 return false; 468 } 469 470 482 public CtClass getSuperclass() throws NotFoundException { 483 return null; 484 } 485 486 495 public void setSuperclass(CtClass clazz) throws CannotCompileException { 496 checkModify(); 497 } 498 499 504 public CtClass[] getInterfaces() throws NotFoundException { 505 return new CtClass[0]; 506 } 507 508 517 public void setInterfaces(CtClass[] list) { 518 checkModify(); 519 } 520 521 526 public void addInterface(CtClass anInterface) { 527 checkModify(); 528 } 529 530 536 public CtClass getDeclaringClass() throws NotFoundException { 537 return null; 538 } 539 540 546 public CtClass getEnclosingClass() throws NotFoundException { 547 return null; 548 } 549 550 560 public CtClass makeNestedClass(String name, boolean isStatic) { 561 throw new RuntimeException (getName() + " is not a class"); 562 } 563 564 570 public CtField[] getFields() { return new CtField[0]; } 571 572 576 public CtField getField(String name) throws NotFoundException { 577 throw new NotFoundException(name); 578 } 579 580 583 CtField getField2(String name) { return null; } 584 585 591 public CtField[] getDeclaredFields() { return new CtField[0]; } 592 593 599 public CtField getDeclaredField(String name) throws NotFoundException { 600 throw new NotFoundException(name); 601 } 602 603 606 public CtBehavior[] getDeclaredBehaviors() { 607 return new CtBehavior[0]; 608 } 609 610 614 public CtConstructor[] getConstructors() { 615 return new CtConstructor[0]; 616 } 617 618 628 public CtConstructor getConstructor(String desc) 629 throws NotFoundException 630 { 631 throw new NotFoundException("no such a constructor"); 632 } 633 634 639 public CtConstructor[] getDeclaredConstructors() { 640 return new CtConstructor[0]; 641 } 642 643 648 public CtConstructor getDeclaredConstructor(CtClass[] params) 649 throws NotFoundException 650 { 651 String desc = Descriptor.ofConstructor(params); 652 return getConstructor(desc); 653 } 654 655 664 public CtConstructor getClassInitializer() { 665 return null; 666 } 667 668 674 public CtMethod[] getMethods() { 675 return new CtMethod[0]; 676 } 677 678 689 public CtMethod getMethod(String name, String desc) 690 throws NotFoundException 691 { 692 throw new NotFoundException(name); 693 } 694 695 701 public CtMethod[] getDeclaredMethods() { 702 return new CtMethod[0]; 703 } 704 705 715 public CtMethod getDeclaredMethod(String name, CtClass[] params) 716 throws NotFoundException 717 { 718 throw new NotFoundException(name); 719 } 720 721 730 public CtMethod getDeclaredMethod(String name) throws NotFoundException { 731 throw new NotFoundException(name); 732 } 733 734 741 public CtConstructor makeClassInitializer() 742 throws CannotCompileException 743 { 744 throw new CannotCompileException("not a class"); 745 } 746 747 753 public void addConstructor(CtConstructor c) 754 throws CannotCompileException 755 { 756 checkModify(); 757 } 758 759 765 public void removeConstructor(CtConstructor c) throws NotFoundException { 766 checkModify(); 767 } 768 769 772 public void addMethod(CtMethod m) throws CannotCompileException { 773 checkModify(); 774 } 775 776 782 public void removeMethod(CtMethod m) throws NotFoundException { 783 checkModify(); 784 } 785 786 795 public void addField(CtField f) throws CannotCompileException { 796 addField(f, (CtField.Initializer)null); 797 } 798 799 824 public void addField(CtField f, String init) 825 throws CannotCompileException 826 { 827 checkModify(); 828 } 829 830 852 public void addField(CtField f, CtField.Initializer init) 853 throws CannotCompileException 854 { 855 checkModify(); 856 } 857 858 864 public void removeField(CtField f) throws NotFoundException { 865 checkModify(); 866 } 867 868 885 public byte[] getAttribute(String name) { 886 return null; 887 } 888 889 911 public void setAttribute(String name, byte[] data) { 912 checkModify(); 913 } 914 915 923 public void instrument(CodeConverter converter) 924 throws CannotCompileException 925 { 926 checkModify(); 927 } 928 929 937 public void instrument(ExprEditor editor) 938 throws CannotCompileException 939 { 940 checkModify(); 941 } 942 943 961 public Class toClass() throws CannotCompileException { 962 return getClassPool().toClass(this); 963 } 964 965 985 public Class toClass(ClassLoader loader) 986 throws CannotCompileException 987 { 988 return getClassPool().toClass(this, loader); 989 } 990 991 1003 public void detach() { 1004 ClassPool cp = getClassPool(); 1005 CtClass obj = cp.removeCached(getName()); 1006 if (obj != this) 1007 cp.cacheCtClass(getName(), obj); 1008 } 1009 1010 1017 public byte[] toBytecode() throws IOException , CannotCompileException { 1018 ByteArrayOutputStream barray = new ByteArrayOutputStream (); 1019 DataOutputStream out = new DataOutputStream (barray); 1020 try { 1021 toBytecode(out); 1022 } 1023 finally { 1024 out.close(); 1025 } 1026 1027 return barray.toByteArray(); 1028 } 1029 1030 1036 public void writeFile() 1037 throws NotFoundException, IOException , CannotCompileException 1038 { 1039 writeFile("."); 1040 } 1041 1042 1050 public void writeFile(String directoryName) 1051 throws NotFoundException, CannotCompileException, IOException 1052 { 1053 String classname = getName(); 1054 String filename = directoryName + File.separatorChar 1055 + classname.replace('.', File.separatorChar) + ".class"; 1056 int pos = filename.lastIndexOf(File.separatorChar); 1057 if (pos > 0) { 1058 String dir = filename.substring(0, pos); 1059 if (!dir.equals(".")) 1060 new File(dir).mkdirs(); 1061 } 1062 1063 DataOutputStream out 1064 = new DataOutputStream (new BufferedOutputStream ( 1065 new DelayedFileOutputStream(filename))); 1066 try { 1067 toBytecode(out); 1068 } 1069 finally { 1070 out.close(); 1071 } 1072 } 1073 1074 static class DelayedFileOutputStream extends OutputStream { 1075 private FileOutputStream file; 1076 private String filename; 1077 1078 DelayedFileOutputStream(String name) { 1079 file = null; 1080 filename = name; 1081 } 1082 1083 private void init() throws IOException { 1084 if (file == null) 1085 file = new FileOutputStream (filename); 1086 } 1087 1088 public void write(int b) throws IOException { 1089 init(); 1090 file.write(b); 1091 } 1092 1093 public void write(byte[] b) throws IOException { 1094 init(); 1095 file.write(b); 1096 } 1097 1098 public void write(byte[] b, int off, int len) throws IOException { 1099 init(); 1100 file.write(b, off, len); 1101 1102 } 1103 1104 public void flush() throws IOException { 1105 init(); 1106 file.flush(); 1107 } 1108 1109 public void close() throws IOException { 1110 init(); 1111 file.close(); 1112 } 1113 } 1114 1115 1124 public void toBytecode(DataOutputStream out) 1125 throws CannotCompileException, IOException 1126 { 1127 throw new CannotCompileException("not a class"); 1128 } 1129} 1130 | Popular Tags |