1 30 31 package jbet; 32 import java.io.*; 33 import java.util.*; 34 35 56 final class ConstantPool extends CPInterface { 57 private Vector cpVector; private Hashtable hashtable; 59 60 61 66 ConstantPool() { 67 hashtable = new Hashtable(); 68 cpVector = new Vector(); 69 cpVector.addElement(null); 70 } 71 72 73 79 ConstantPool (DataInputStream dataIn) throws IOException, 80 ClassFileException { 81 82 int size = dataIn.readUnsignedShort(); 84 85 cpVector = new Vector(size); 86 cpVector.setSize(size); 87 hashtable= new Hashtable(); 88 89 93 99 for (int i=1; i < size ; i++) { 100 byte tag = (byte) dataIn.readUnsignedByte(); 101 switch (tag) { 102 case CONSTANT_Class : { 103 CpClass cp = new CpClass(i, this); 104 cp.nameIndex = dataIn.readUnsignedShort(); 105 cpVector.setElementAt(cp, i); 106 break; 107 } 108 case CONSTANT_Fieldref: { 109 CpFieldRef cp = new CpFieldRef(i, this); 110 cp.classIndex = dataIn.readUnsignedShort(); 111 cp.nameAndTypeIndex = dataIn.readUnsignedShort(); 112 cpVector.setElementAt(cp, i); 113 break; 114 } 115 case CONSTANT_Methodref: { 116 CpMethodRef cp = new CpMethodRef(i, this, false); 117 cp.classIndex = dataIn.readUnsignedShort(); 118 cp.nameAndTypeIndex = dataIn.readUnsignedShort(); 119 cpVector.setElementAt(cp, i); 120 break; 121 } 122 case CONSTANT_InterfaceMethodref: { 123 126 CpMethodRef cp = new CpMethodRef(i, this, true); 127 cp.classIndex = dataIn.readUnsignedShort(); 128 cp.nameAndTypeIndex = dataIn.readUnsignedShort(); 129 cpVector.setElementAt(cp, i); 130 break; 131 } 132 case CONSTANT_String: { 133 CpString cp = new CpString(i, this); 134 cp.stringIndex = dataIn.readUnsignedShort(); 135 cpVector.setElementAt(cp, i); 136 break; 137 } 138 case CONSTANT_Integer: { 139 CpInteger cp = new CpInteger(i, this); 140 cp.value = dataIn.readInt(); 141 cpVector.setElementAt(cp, i); 142 break; 143 } 144 case CONSTANT_Float: { 145 CpFloat cp = new CpFloat(i, this); 146 cp.value = dataIn.readFloat(); 147 cpVector.setElementAt(cp, i); 148 break; 149 } 150 case CONSTANT_Long: { 151 CpLong cp = new CpLong(i, this); 152 cp.value = dataIn.readLong(); 153 cpVector.setElementAt(cp, i); 154 i++; break; 156 } 157 case CONSTANT_Double: { 158 CpDouble cp = new CpDouble(i, this); 159 cp.value = dataIn.readDouble(); 160 cpVector.setElementAt(cp, i); 161 i++; break; 163 } 164 case CONSTANT_NameAndType: { 165 CpNameAndType cp = new CpNameAndType(i, this); 166 cp.nameIndex = dataIn.readUnsignedShort(); 167 cp.typeIndex = dataIn.readUnsignedShort(); 168 cpVector.setElementAt(cp, i); 169 break; 170 } 171 case CONSTANT_Utf8: { 172 CpUtf8 cp = new CpUtf8(i, this); 173 cp.string = dataIn.readUTF(); 174 cpVector.setElementAt(cp, i); 175 break; 176 } 177 default: 178 throw new RuntimeException ("error reading constant pool"); 179 } 180 } 181 182 for (int i=1; i< cpVector.size(); i++) { 183 CpEntry cp = (CpEntry)cpVector.elementAt(i); 184 cp.setup(); 185 hashtable.put(cp, cp); 186 187 if ( cp.tag == CONSTANT_Long || cp.tag == CONSTANT_Double ) 189 i++; 190 } 191 } 192 193 203 void writeFile(DataOutputStream dataOut) throws IOException { 204 dataOut.writeShort( cpVector.size() ); 205 206 for (int i=1; i<cpVector.size(); i++) { 208 209 CpEntry entry = (CpEntry) cpVector.elementAt(i); 210 entry.write(dataOut); 211 212 if (entry.tag == CONSTANT_Long || 213 entry.tag == CONSTANT_Double) 214 i++; 215 } 216 } 217 218 219 220 223 void printout (LineWriter out) { 224 for (int i = 1; i < poolCount(); i++) 228 if (elementAt(i) != null) 229 out.println (i + "\t" + elementAt(i).recString()); 230 } 231 232 int tagAt (int i) { 233 return elementAt(i)==null ? 0 : elementAt(i).tag; 234 } 235 236 CpEntry elementAt (int i) { 237 return (CpEntry) cpVector.elementAt(i); 238 } 239 240 241 245 int poolCount() { 246 return(cpVector.size()); 247 } 248 249 255 256 int integerAt (int index) throws ClassFileException { 257 Object obj = cpVector.elementAt(index); 258 if (! (obj instanceof CpInteger)) 259 throw new CPException ("not a integer " + index); 260 return ((CpInteger)obj).value; 261 } 262 float floatAt (int index) throws ClassFileException { 263 Object obj = cpVector.elementAt(index); 264 if (! (obj instanceof CpFloat)) 265 throw new CPException ("not a float " + index); 266 return ((CpFloat)obj).value; 267 } 268 long longAt (int index) throws ClassFileException { 269 Object obj = cpVector.elementAt(index); 270 if (! (obj instanceof CpLong) ) 271 throw new CPException ("not a long " + index); 272 return ((CpLong)obj).value; 273 } 274 double doubleAt (int index) throws ClassFileException { 275 Object obj = cpVector.elementAt(index); 276 if (! (obj instanceof CpDouble) ) 277 throw new CPException ("not a double " + index); 278 return ((CpDouble)obj).value; 279 } 280 String utf8At (int index) throws ClassFileException { 281 Object obj = cpVector.elementAt(index); 282 if (! (obj instanceof CpUtf8) ) 283 throw new CPException ("not a utf8 " + index); 284 return ((CpUtf8)obj).string; 285 } 286 String stringAt (int index) throws ClassFileException { 287 Object obj = cpVector.elementAt(index); 288 if (! (obj instanceof CpString) ) 289 throw new CPException ("not a string " + index); 290 return ((CpString)obj).string(); 291 } 292 CpNameAndType cpNameAndTypeAt (int index) throws ClassFileException { 293 Object obj = cpVector.elementAt(index); 294 if (obj instanceof CpNameAndType) 295 return (CpNameAndType) obj; 296 else 297 throw new CPException ("not a name_and_type " + index); 298 } 299 CpInteger cpIntegerAt (int index) throws ClassFileException { 300 Object obj = cpVector.elementAt(index); 301 if (! (obj instanceof CpInteger)) 302 throw new CPException ("not a integer " + index); 303 return (CpInteger)obj; 304 } 305 CpFloat cpFloatAt (int index) throws ClassFileException { 306 Object obj = cpVector.elementAt(index); 307 if (! (obj instanceof CpFloat)) 308 throw new CPException ("not a float " + index); 309 return ((CpFloat)obj); 310 } 311 CpLong cpLongAt (int index) throws ClassFileException { 312 Object obj = cpVector.elementAt(index); 313 if (! (obj instanceof CpLong) ) 314 throw new CPException ("not a long " + index); 315 return ((CpLong)obj); 316 } 317 CpDouble cpDoubleAt (int index) throws ClassFileException { 318 Object obj = cpVector.elementAt(index); 319 if (! (obj instanceof CpDouble) ) 320 throw new CPException ("not a double " + index); 321 return ((CpDouble)obj); 322 } 323 CpUtf8 cpUtf8At (int index) throws ClassFileException { 324 Object obj = cpVector.elementAt(index); 325 if (! (obj instanceof CpUtf8) ) 326 throw new CPException ("not a utf8 " + index); 327 return ((CpUtf8)obj); 328 } 329 CpClass cpClassAt (int index) throws ClassFileException { 330 Object obj = cpVector.elementAt(index); 331 if (! (obj instanceof CpClass) ) 332 throw new CPException ("not a class " + index + " " + ((CpEntry)obj).tag); 333 return (CpClass)obj; 334 } 335 CpString cpStringAt (int index) throws ClassFileException { 336 Object obj = cpVector.elementAt(index); 337 if (! (obj instanceof CpString) ) 338 throw new CPException ("not a string " + index); 339 return ((CpString)obj); 340 } 341 CpMethodRef cpMethodRefAt (int index) throws ClassFileException { 342 Object obj = cpVector.elementAt(index); 343 if (! (obj instanceof CpMethodRef) ) 344 throw new CPException ("not a methodref " + index); 345 return ((CpMethodRef)obj); 346 } 347 CpFieldRef cpFieldRefAt (int index) throws ClassFileException { 348 Object obj = cpVector.elementAt(index); 349 if (! (obj instanceof CpFieldRef) ) 350 throw new CPException ("not a fieldref " + index); 351 return ((CpFieldRef)obj); 352 } 353 354 361 362 int internUtf8 (String s) { 363 CpUtf8 val, key = new CpUtf8(this, s); 364 val = (CpUtf8) hashtable.get(key); 365 if (val != null) return val.index; 366 appendRec(key); 367 return key.index; 368 } 369 370 int internClass (String s) { 371 if (s==null) 372 return 0; 373 CpClass val, key = new CpClass(this, s); 374 val = (CpClass) hashtable.get(key); 375 if (val != null) return val.index; 376 key.nameIndex = internUtf8(s); 377 appendRec(key); 378 return key.index; 379 } 380 381 382 int internString (String s) { 383 CpString val, key = new CpString(this, s); 384 val = (CpString) hashtable.get(key); 385 if (val != null) return val.index; 386 key.stringIndex = internUtf8(s); 387 appendRec(key); 388 return key.index; 389 } 390 391 392 int internMethodRef (String cname, String mname, Descriptor type) { 393 CpMethodRef val, key = new CpMethodRef(this, false, cname, mname, type); 394 val = (CpMethodRef) hashtable.get(key); 395 if (val != null) return val.index; 396 key.classIndex = internClass(cname); 397 key.nameAndTypeIndex = internNameAndType(mname, type); 398 appendRec(key); 399 return key.index; 400 } 401 402 int internInterfaceMethodRef (String cname, String mname, Descriptor type) { 403 CpMethodRef val, key = new CpMethodRef(this, true, cname, mname, type); 404 val = (CpMethodRef) hashtable.get(key); 405 if (val != null) return val.index; 406 key.classIndex = internClass(cname); 407 key.nameAndTypeIndex = internNameAndType(mname, type); 408 appendRec(key); 409 return key.index; 410 } 411 412 int internFieldRef (String cname, String fname, Type type) { 413 CpFieldRef val, key = new CpFieldRef(this, cname, fname, type); 414 val = (CpFieldRef) hashtable.get(key); 415 if (val != null) return val.index; 416 key.classIndex = internClass(cname); 417 key.nameAndTypeIndex = internNameAndType(fname, type); 418 appendRec(key); 419 return key.index; 420 } 421 422 423 424 int internNameAndType (String name, Object type) { 425 CpNameAndType val, key = new CpNameAndType(this, name, type); 426 val = (CpNameAndType) hashtable.get(key); 427 if (val != null) return val.index; 428 key.nameIndex = internUtf8(name); 429 key.typeIndex = internUtf8(type.toString()); 430 appendRec(key); 431 return key.index; 432 } 433 434 int internInteger (int i) { 435 CpInteger val, key = new CpInteger(this, i); 436 val = (CpInteger) hashtable.get(key); 437 if (val != null) return val.index; 438 appendRec(key); 439 return key.index; 440 } 441 442 int internFloat (float i) { 443 CpFloat val, key = new CpFloat(this, i); 444 val = (CpFloat) hashtable.get(key); 445 if (val != null) return val.index; 446 appendRec(key); 447 return key.index; 448 } 449 450 int internLong (long i) { 451 CpLong val, key = new CpLong(this, i); 452 val = (CpLong) hashtable.get(key); 453 if (val != null) return val.index; 454 appendRec(key); 455 return key.index; 456 } 457 458 459 int internDouble (double i) { 460 CpDouble val, key = new CpDouble(this, i); 461 val = (CpDouble) hashtable.get(key); 462 if (val != null) return val.index; 463 appendRec(key); 464 return key.index; 465 } 466 467 468 471 private int appendRec(CpEntry rec) { 472 cpVector.addElement(rec); 473 rec.index = cpVector.size() - 1; 474 478 if ((rec instanceof CpLong) || (rec instanceof CpDouble)) 479 cpVector.addElement(null); 480 481 hashtable.put(rec, rec); 482 483 return(rec.index); 484 } 485 } 486 | Popular Tags |