1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import com.sun.org.apache.bcel.internal.Repository; 59 import java.io.*; 60 import java.util.StringTokenizer ; 61 62 71 public class JavaClass extends AccessFlags implements Cloneable , Node { 72 private String file_name; 73 private String package_name; 74 private String source_file_name = "<Unknown>"; 75 private int class_name_index; 76 private int superclass_name_index; 77 private String class_name; 78 private String superclass_name; 79 private int major, minor; private ConstantPool constant_pool; private int[] interfaces; private String [] interface_names; 83 private Field[] fields; private Method[] methods; private Attribute[] attributes; private byte source = HEAP; 88 public static final byte HEAP = 1; 89 public static final byte FILE = 2; 90 public static final byte ZIP = 3; 91 92 static boolean debug = false; static char sep = '/'; 95 111 public JavaClass(int class_name_index, 112 int superclass_name_index, 113 String file_name, 114 int major, 115 int minor, 116 int access_flags, 117 ConstantPool constant_pool, 118 int[] interfaces, 119 Field[] fields, 120 Method[] methods, 121 Attribute[] attributes, 122 byte source) 123 { 124 if(interfaces == null) interfaces = new int[0]; 126 if(attributes == null) 127 this.attributes = new Attribute[0]; 128 if(fields == null) 129 fields = new Field[0]; 130 if(methods == null) 131 methods = new Method[0]; 132 133 this.class_name_index = class_name_index; 134 this.superclass_name_index = superclass_name_index; 135 this.file_name = file_name; 136 this.major = major; 137 this.minor = minor; 138 this.access_flags = access_flags; 139 this.constant_pool = constant_pool; 140 this.interfaces = interfaces; 141 this.fields = fields; 142 this.methods = methods; 143 this.attributes = attributes; 144 this.source = source; 145 146 for(int i=0; i < attributes.length; i++) { 148 if(attributes[i] instanceof SourceFile) { 149 source_file_name = ((SourceFile)attributes[i]).getSourceFileName(); 150 break; 151 } 152 } 153 154 ConstantUtf8 name; 156 157 161 class_name = constant_pool.getConstantString(class_name_index, 162 Constants.CONSTANT_Class); 163 class_name = Utility.compactClassName(class_name, false); 164 165 int index = class_name.lastIndexOf('.'); 166 if(index < 0) 167 package_name = ""; 168 else 169 package_name = class_name.substring(0, index); 170 171 if(superclass_name_index > 0) { superclass_name = constant_pool.getConstantString(superclass_name_index, 173 Constants.CONSTANT_Class); 174 superclass_name = Utility.compactClassName(superclass_name, false); 175 } 176 else 177 superclass_name = "java.lang.Object"; 178 179 interface_names = new String [interfaces.length]; 180 for(int i=0; i < interfaces.length; i++) { 181 String str = constant_pool.getConstantString(interfaces[i], Constants.CONSTANT_Class); 182 interface_names[i] = Utility.compactClassName(str, false); 183 } 184 } 185 186 201 public JavaClass(int class_name_index, 202 int superclass_name_index, 203 String file_name, 204 int major, 205 int minor, 206 int access_flags, 207 ConstantPool constant_pool, 208 int[] interfaces, 209 Field[] fields, 210 Method[] methods, 211 Attribute[] attributes) { 212 this(class_name_index, superclass_name_index, file_name, major, minor, access_flags, 213 constant_pool, interfaces, fields, methods, attributes, HEAP); 214 } 215 216 217 224 public void accept(Visitor v) { 225 v.visitJavaClass(this); 226 } 227 228 230 static final void Debug(String str) { 231 if(debug) 232 System.out.println(str); 233 } 234 235 241 public void dump(File file) throws IOException 242 { 243 String parent = file.getParent(); 244 245 if(parent != null) { 246 File dir = new File(parent); 247 248 if(dir != null) 249 dir.mkdirs(); 250 } 251 252 dump(new DataOutputStream(new FileOutputStream(file))); 253 } 254 255 261 public void dump(String file_name) throws IOException 262 { 263 dump(new File(file_name)); 264 } 265 266 269 public byte[] getBytes() { 270 ByteArrayOutputStream s = new ByteArrayOutputStream(); 271 DataOutputStream ds = new DataOutputStream(s); 272 273 try { 274 dump(ds); 275 ds.close(); 276 } catch(IOException e) { e.printStackTrace(); } 277 278 return s.toByteArray(); 279 } 280 281 287 public void dump(OutputStream file) throws IOException { 288 dump(new DataOutputStream(file)); 289 } 290 291 297 public void dump(DataOutputStream file) throws IOException 298 { 299 file.writeInt(0xcafebabe); 300 file.writeShort(minor); 301 file.writeShort(major); 302 303 constant_pool.dump(file); 304 305 file.writeShort(access_flags); 306 file.writeShort(class_name_index); 307 file.writeShort(superclass_name_index); 308 309 file.writeShort(interfaces.length); 310 for(int i=0; i < interfaces.length; i++) 311 file.writeShort(interfaces[i]); 312 313 file.writeShort(fields.length); 314 for(int i=0; i < fields.length; i++) 315 fields[i].dump(file); 316 317 file.writeShort(methods.length); 318 for(int i=0; i < methods.length; i++) 319 methods[i].dump(file); 320 321 if(attributes != null) { 322 file.writeShort(attributes.length); 323 for(int i=0; i < attributes.length; i++) 324 attributes[i].dump(file); 325 } 326 else 327 file.writeShort(0); 328 329 file.close(); 330 } 331 332 335 public Attribute[] getAttributes() { return attributes; } 336 337 340 public String getClassName() { return class_name; } 341 342 345 public String getPackageName() { return package_name; } 346 347 350 public int getClassNameIndex() { return class_name_index; } 351 352 355 public ConstantPool getConstantPool() { return constant_pool; } 356 357 360 public Field[] getFields() { return fields; } 361 364 public String getFileName() { return file_name; } 365 368 public String [] getInterfaceNames() { return interface_names; } 369 372 public int[] getInterfaces() { return interfaces; } 373 376 public int getMajor() { return major; } 377 380 public Method[] getMethods() { return methods; } 381 384 public int getMinor() { return minor; } 385 386 389 public String getSourceFileName() { return source_file_name; } 390 391 394 public String getSuperclassName() { return superclass_name; } 395 398 public int getSuperclassNameIndex() { return superclass_name_index; } 399 400 static { 401 404 407 JavaClass.sep = java.io.File.separatorChar; 409 } 410 411 414 public void setAttributes(Attribute[] attributes) { 415 this.attributes = attributes; 416 } 417 420 public void setClassName(String class_name) { 421 this.class_name = class_name; 422 } 423 426 public void setClassNameIndex(int class_name_index) { 427 this.class_name_index = class_name_index; 428 } 429 432 public void setConstantPool(ConstantPool constant_pool) { 433 this.constant_pool = constant_pool; 434 } 435 438 public void setFields(Field[] fields) { 439 this.fields = fields; 440 } 441 444 public void setFileName(String file_name) { 445 this.file_name = file_name; 446 } 447 450 public void setInterfaceNames(String [] interface_names) { 451 this.interface_names = interface_names; 452 } 453 456 public void setInterfaces(int[] interfaces) { 457 this.interfaces = interfaces; 458 } 459 462 public void setMajor(int major) { 463 this.major = major; 464 } 465 468 public void setMethods(Method[] methods) { 469 this.methods = methods; 470 } 471 474 public void setMinor(int minor) { 475 this.minor = minor; 476 } 477 480 public void setSourceFileName(String source_file_name) { 481 this.source_file_name = source_file_name; 482 } 483 486 public void setSuperclassName(String superclass_name) { 487 this.superclass_name = superclass_name; 488 } 489 492 public void setSuperclassNameIndex(int superclass_name_index) { 493 this.superclass_name_index = superclass_name_index; 494 } 495 498 public String toString() { 499 String access = Utility.accessToString(access_flags, true); 500 access = access.equals("")? "" : (access + " "); 501 502 StringBuffer buf = new StringBuffer (access + 503 Utility.classOrInterface(access_flags) + 504 " " + 505 class_name + " extends " + 506 Utility.compactClassName(superclass_name, 507 false) + '\n'); 508 int size = interfaces.length; 509 510 if(size > 0) { 511 buf.append("implements\t\t"); 512 513 for(int i=0; i < size; i++) { 514 buf.append(interface_names[i]); 515 if(i < size - 1) 516 buf.append(", "); 517 } 518 519 buf.append('\n'); 520 } 521 522 buf.append("filename\t\t" + file_name + '\n'); 523 buf.append("compiled from\t\t" + source_file_name + '\n'); 524 buf.append("compiler version\t" + major + "." + minor + '\n'); 525 buf.append("access flags\t\t" + access_flags + '\n'); 526 buf.append("constant pool\t\t" + constant_pool.getLength() + " entries\n"); 527 buf.append("ACC_SUPER flag\t\t" + isSuper() + "\n"); 528 529 if(attributes.length > 0) { 530 buf.append("\nAttribute(s):\n"); 531 for(int i=0; i < attributes.length; i++) 532 buf.append(indent(attributes[i])); 533 } 534 535 if(fields.length > 0) { 536 buf.append("\n" + fields.length + " fields:\n"); 537 for(int i=0; i < fields.length; i++) 538 buf.append("\t" + fields[i] + '\n'); 539 } 540 541 if(methods.length > 0) { 542 buf.append("\n" + methods.length + " methods:\n"); 543 for(int i=0; i < methods.length; i++) 544 buf.append("\t" + methods[i] + '\n'); 545 } 546 547 return buf.toString(); 548 } 549 550 private static final String indent(Object obj) { 551 StringTokenizer tok = new StringTokenizer (obj.toString(), "\n"); 552 StringBuffer buf = new StringBuffer (); 553 554 while(tok.hasMoreTokens()) 555 buf.append("\t" + tok.nextToken() + "\n"); 556 557 return buf.toString(); 558 } 559 560 563 public JavaClass copy() { 564 JavaClass c = null; 565 566 try { 567 c = (JavaClass)clone(); 568 } catch(CloneNotSupportedException e) {} 569 570 c.constant_pool = constant_pool.copy(); 571 c.interfaces = (int[])interfaces.clone(); 572 c.interface_names = (String [])interface_names.clone(); 573 574 c.fields = new Field[fields.length]; 575 for(int i=0; i < fields.length; i++) 576 c.fields[i] = fields[i].copy(c.constant_pool); 577 578 c.methods = new Method[methods.length]; 579 for(int i=0; i < methods.length; i++) 580 c.methods[i] = methods[i].copy(c.constant_pool); 581 582 c.attributes = new Attribute[attributes.length]; 583 for(int i=0; i < attributes.length; i++) 584 c.attributes[i] = attributes[i].copy(c.constant_pool); 585 586 return c; 587 } 588 589 public final boolean instanceOf(JavaClass super_class) { 590 return Repository.instanceOf(this, super_class); 591 } 592 593 public final boolean isSuper() { 594 return (access_flags & Constants.ACC_SUPER) != 0; 595 } 596 597 public final boolean isClass() { 598 return (access_flags & Constants.ACC_INTERFACE) == 0; 599 } 600 601 603 public final byte getSource() { 604 return source; 605 } 606 } 607 | Popular Tags |