1 18 package org.apache.tools.ant.taskdefs.optional.depend.constantpool; 19 20 import java.io.DataInputStream ; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Vector ; 25 26 34 public class ConstantPool { 35 36 37 private Vector entries; 38 39 43 private Hashtable utf8Indexes; 44 45 46 public ConstantPool() { 47 entries = new Vector (); 48 49 entries.addElement(null); 52 53 utf8Indexes = new Hashtable (); 54 } 55 56 63 public void read(DataInputStream classStream) throws IOException { 64 int numEntries = classStream.readUnsignedShort(); 65 66 for (int i = 1; i < numEntries;) { 67 ConstantPoolEntry nextEntry 68 = ConstantPoolEntry.readEntry(classStream); 69 70 i += nextEntry.getNumEntries(); 71 72 addEntry(nextEntry); 73 } 74 } 75 76 81 public int size() { 82 return entries.size(); 83 } 84 85 92 public int addEntry(ConstantPoolEntry entry) { 93 int index = entries.size(); 94 95 entries.addElement(entry); 96 97 int numSlots = entry.getNumEntries(); 98 99 for (int j = 0; j < numSlots - 1; ++j) { 101 entries.addElement(null); 102 } 103 104 if (entry instanceof Utf8CPInfo) { 105 Utf8CPInfo utf8Info = (Utf8CPInfo) entry; 106 107 utf8Indexes.put(utf8Info.getValue(), new Integer (index)); 108 } 109 110 return index; 111 } 112 113 118 public void resolve() { 119 for (Enumeration i = entries.elements(); i.hasMoreElements();) { 120 ConstantPoolEntry poolInfo = (ConstantPoolEntry) i.nextElement(); 121 122 if (poolInfo != null && !poolInfo.isResolved()) { 123 poolInfo.resolve(this); 124 } 125 } 126 } 127 128 129 135 public ConstantPoolEntry getEntry(int index) { 136 return (ConstantPoolEntry) entries.elementAt(index); 137 } 138 139 146 public int getUTF8Entry(String value) { 147 int index = -1; 148 Integer indexInteger = (Integer ) utf8Indexes.get(value); 149 150 if (indexInteger != null) { 151 index = indexInteger.intValue(); 152 } 153 154 return index; 155 } 156 157 165 public int getClassEntry(String className) { 166 int index = -1; 167 168 for (int i = 0; i < entries.size() && index == -1; ++i) { 169 Object element = entries.elementAt(i); 170 171 if (element instanceof ClassCPInfo) { 172 ClassCPInfo classinfo = (ClassCPInfo) element; 173 174 if (classinfo.getClassName().equals(className)) { 175 index = i; 176 } 177 } 178 } 179 180 return index; 181 } 182 183 191 public int getConstantEntry(Object constantValue) { 192 int index = -1; 193 194 for (int i = 0; i < entries.size() && index == -1; ++i) { 195 Object element = entries.elementAt(i); 196 197 if (element instanceof ConstantCPInfo) { 198 ConstantCPInfo constantEntry = (ConstantCPInfo) element; 199 200 if (constantEntry.getValue().equals(constantValue)) { 201 index = i; 202 } 203 } 204 } 205 206 return index; 207 } 208 209 220 public int getMethodRefEntry(String methodClassName, String methodName, 221 String methodType) { 222 int index = -1; 223 224 for (int i = 0; i < entries.size() && index == -1; ++i) { 225 Object element = entries.elementAt(i); 226 227 if (element instanceof MethodRefCPInfo) { 228 MethodRefCPInfo methodRefEntry = (MethodRefCPInfo) element; 229 230 if (methodRefEntry.getMethodClassName().equals(methodClassName) 231 && methodRefEntry.getMethodName().equals(methodName) 232 && methodRefEntry.getMethodType().equals(methodType)) { 233 index = i; 234 } 235 } 236 } 237 238 return index; 239 } 240 241 253 public int getInterfaceMethodRefEntry(String interfaceMethodClassName, 254 String interfaceMethodName, 255 String interfaceMethodType) { 256 int index = -1; 257 258 for (int i = 0; i < entries.size() && index == -1; ++i) { 259 Object element = entries.elementAt(i); 260 261 if (element instanceof InterfaceMethodRefCPInfo) { 262 InterfaceMethodRefCPInfo interfaceMethodRefEntry 263 = (InterfaceMethodRefCPInfo) element; 264 265 if (interfaceMethodRefEntry.getInterfaceMethodClassName().equals( 266 interfaceMethodClassName) 267 && interfaceMethodRefEntry.getInterfaceMethodName().equals( 268 interfaceMethodName) 269 && interfaceMethodRefEntry.getInterfaceMethodType().equals( 270 interfaceMethodType)) { 271 index = i; 272 } 273 } 274 } 275 276 return index; 277 } 278 279 290 public int getFieldRefEntry(String fieldClassName, String fieldName, 291 String fieldType) { 292 int index = -1; 293 294 for (int i = 0; i < entries.size() && index == -1; ++i) { 295 Object element = entries.elementAt(i); 296 297 if (element instanceof FieldRefCPInfo) { 298 FieldRefCPInfo fieldRefEntry = (FieldRefCPInfo) element; 299 300 if (fieldRefEntry.getFieldClassName().equals(fieldClassName) 301 && fieldRefEntry.getFieldName().equals(fieldName) 302 && fieldRefEntry.getFieldType().equals(fieldType)) { 303 index = i; 304 } 305 } 306 } 307 308 return index; 309 } 310 311 320 public int getNameAndTypeEntry(String name, String type) { 321 int index = -1; 322 323 for (int i = 0; i < entries.size() && index == -1; ++i) { 324 Object element = entries.elementAt(i); 325 326 if (element instanceof NameAndTypeCPInfo) { 327 NameAndTypeCPInfo nameAndTypeEntry 328 = (NameAndTypeCPInfo) element; 329 330 if (nameAndTypeEntry.getName().equals(name) 331 && nameAndTypeEntry.getType().equals(type)) { 332 index = i; 333 } 334 } 335 } 336 337 return index; 338 } 339 340 345 public String toString() { 346 StringBuffer sb = new StringBuffer ("\n"); 347 int size = entries.size(); 348 349 for (int i = 0; i < size; ++i) { 350 sb.append("[" + i + "] = " + getEntry(i) + "\n"); 351 } 352 353 return sb.toString(); 354 } 355 356 } 357 358 | Popular Tags |