1 32 33 package com.jeantessier.classreader; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 import org.apache.oro.text.perl.*; 40 41 public class ConstantPool extends ArrayList implements Visitable { 42 private Classfile classfile; 43 44 public ConstantPool(Classfile classfile, DataInputStream in) throws IOException { 45 this.classfile = classfile; 46 47 int count = in.readUnsignedShort(); 48 49 ensureCapacity(count); 50 51 add(null); 53 54 for (int i=1; i<count; i++) { 55 byte tag = in.readByte(); 56 57 switch(tag) { 58 case ConstantPoolEntry.CONSTANT_Class: 59 add(new Class_info(this, in)); 60 break; 61 case ConstantPoolEntry.CONSTANT_Fieldref: 62 add(new FieldRef_info(this, in)); 63 break; 64 case ConstantPoolEntry.CONSTANT_Methodref: 65 add(new MethodRef_info(this, in)); 66 break; 67 case ConstantPoolEntry.CONSTANT_InterfaceMethodref: 68 add(new InterfaceMethodRef_info(this, in)); 69 break; 70 case ConstantPoolEntry.CONSTANT_String: 71 add(new String_info(this, in)); 72 break; 73 case ConstantPoolEntry.CONSTANT_Integer: 74 add(new Integer_info(this, in)); 75 break; 76 case ConstantPoolEntry.CONSTANT_Float: 77 add(new Float_info(this, in)); 78 break; 79 case ConstantPoolEntry.CONSTANT_Long: 80 add(new Long_info(this, in)); 81 i++; 82 add(null); 83 break; 84 case ConstantPoolEntry.CONSTANT_Double: 85 add(new Double_info(this, in)); 86 i++; 87 add(null); 88 break; 89 case ConstantPoolEntry.CONSTANT_NameAndType: 90 add(new NameAndType_info(this, in)); 91 break; 92 case ConstantPoolEntry.CONSTANT_Utf8: 93 add(new UTF8_info(this, in)); 94 break; 95 default: 96 Logger.getLogger(getClass()).info("Unknown Tag " + tag); 97 break; 98 } 99 } 100 } 101 102 public Classfile getClassfile() { 103 return classfile; 104 } 105 106 public void accept(Visitor visitor) { 107 visitor.visitConstantPool(this); 108 } 109 110 public String toString() { 111 StringWriter out = new StringWriter(); 112 PrintWriter writer = new PrintWriter(out); 113 114 writer.println("Constant Pool:"); 115 116 Printer printer = new TextPrinter(writer); 117 accept(printer); 118 119 writer.close(); 120 121 return out.toString(); 122 } 123 } 124 | Popular Tags |