1 package sli.kim.classfile; 2 3 import java.util.*; 4 import java.io.*; 5 6 36 public class ConstPool { 37 private Vector cp = new Vector(); 38 private Hashtable cpHash = new Hashtable(); 42 43 46 public int size() { 47 return cp.size(); 48 } 49 50 56 public short addEntry(ConstPoolEntry entry) { 57 cp.addElement(entry); 58 short result = (short)(size() - 1); 59 cpHash.put(entry, new Integer (result)); 60 if (entry.typecode() == ConstPoolEntry.LONG 62 || entry.typecode() == ConstPoolEntry.DOUBLE) 63 cp.addElement(new ConstPoolEntry().setUnused()); 64 return result; 65 } 66 67 70 public ConstPoolEntry getEntryAtIndex(short index) { 71 return (ConstPoolEntry)cp.elementAt(index); 72 } 73 74 79 public short getIndexOfEntryNoAdd(ConstPoolEntry entry) { 80 Integer i = (Integer )cpHash.get(entry); 81 if (i != null) 82 return (short)i.intValue(); 83 return (short)-1; 84 } 85 86 91 public short getIndexOfEntryAdd(ConstPoolEntry entry) { 92 short result = getIndexOfEntryNoAdd(entry); 93 if (result < 0) 94 result = addEntry((ConstPoolEntry)entry.clone()); 95 return result; 96 } 97 98 private ConstPoolEntry searchEntry = new ConstPoolEntry(); 101 102 110 public short getIndexOfUTFAdd(String str) { 111 return getIndexOfEntryAdd(searchEntry.setUTF(str)); 112 } 113 114 123 public short getIndexOfClassAdd(String classname) { 124 searchEntry.setClass(getIndexOfUTFAdd(classname)); 125 return getIndexOfEntryAdd(searchEntry); 126 } 127 128 133 public void read(DataInput in) throws IOException, ClassFileParseException { 134 int count = in.readShort(); 135 136 if (Debug.readConstPool != null) Debug.println(Debug.readConstPool, 137 "# of entries = " + count); 138 139 addEntry(new ConstPoolEntry().setUnused()); 141 for (int i = 1; i < count; i++) { 143 ConstPoolEntry entry = new ConstPoolEntry(); 144 entry.read(in, entry); 145 146 if (Debug.readConstPool != null) Debug.println(Debug.readConstPool, 147 i + " " + entry.toString()); 148 149 addEntry(entry); 150 if (ConstPoolEntry.LONG == entry.typecode() 152 || ConstPoolEntry.DOUBLE == entry.typecode()) 153 i++; 154 } 155 } 156 157 161 public void write(DataOutput out) throws IOException { 162 int numEntries = size(); 163 164 if (Debug.writeConstPool != null) Debug.println(Debug.writeConstPool, 165 "# of entries = " + numEntries); 166 167 out.writeShort(numEntries); 168 for (short i = 1; i < numEntries; i++) { 169 ConstPoolEntry entry = getEntryAtIndex(i); 170 if (entry.isUnused()) { 172 if (Debug.writeConstPool != null) Debug.println(Debug.writeConstPool, 173 "skipping unused entry"); 174 } 175 else { 176 entry.write(out, entry); 177 178 if (Debug.writeConstPool != null) Debug.println(Debug.writeConstPool, 179 i + " " + entry.toString()); 180 } 181 } 182 } 183 } 184 | Popular Tags |