1 29 30 package com.caucho.bytecode; 31 32 import com.caucho.log.Log; 33 34 import java.io.IOException ; 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.logging.Logger ; 38 39 42 public class ConstantPool { 43 static private final Logger log = Log.open(ConstantPool.class); 44 45 public static final int CP_CLASS = 7; 46 public static final int CP_FIELD_REF = 9; 47 public static final int CP_METHOD_REF = 10; 48 public static final int CP_INTERFACE_METHOD_REF = 11; 49 public static final int CP_STRING = 8; 50 public static final int CP_INTEGER = 3; 51 public static final int CP_FLOAT = 4; 52 public static final int CP_LONG = 5; 53 public static final int CP_DOUBLE = 6; 54 public static final int CP_NAME_AND_TYPE = 12; 55 public static final int CP_UTF8 = 1; 56 57 private ArrayList <ConstantPoolEntry> _entries; 58 private HashMap <String ,Utf8Constant> _utf8Map 59 = new HashMap <String ,Utf8Constant>(); 60 61 ConstantPool() 62 { 63 _entries = new ArrayList <ConstantPoolEntry>(); 64 _entries.add(null); 65 } 66 67 70 public ConstantPoolEntry getEntry(int index) 71 { 72 return _entries.get(index); 73 } 74 75 78 public ArrayList <ConstantPoolEntry> getEntries() 79 { 80 return _entries; 81 } 82 83 86 public ClassConstant getClass(int index) 87 { 88 return (ClassConstant) _entries.get(index); 89 } 90 91 94 public FieldRefConstant getFieldRef(int index) 95 { 96 return (FieldRefConstant) _entries.get(index); 97 } 98 99 102 public MethodRefConstant getMethodRef(int index) 103 { 104 return (MethodRefConstant) _entries.get(index); 105 } 106 107 110 public InterfaceMethodRefConstant getInterfaceMethodRef(int index) 111 { 112 return (InterfaceMethodRefConstant) _entries.get(index); 113 } 114 115 118 public StringConstant getString(int index) 119 { 120 return (StringConstant) _entries.get(index); 121 } 122 123 126 public IntegerConstant getInteger(int index) 127 { 128 return (IntegerConstant) _entries.get(index); 129 } 130 131 134 public LongConstant getLong(int index) 135 { 136 return (LongConstant) _entries.get(index); 137 } 138 139 142 public FloatConstant getFloat(int index) 143 { 144 return (FloatConstant) _entries.get(index); 145 } 146 147 150 public DoubleConstant getDouble(int index) 151 { 152 return (DoubleConstant) _entries.get(index); 153 } 154 155 158 public NameAndTypeConstant getNameAndType(int index) 159 { 160 return (NameAndTypeConstant) _entries.get(index); 161 } 162 163 166 public Utf8Constant getUtf8(int index) 167 { 168 return (Utf8Constant) _entries.get(index); 169 } 170 171 174 public String getUtf8AsString(int index) 175 { 176 Utf8Constant utf8 = (Utf8Constant) _entries.get(index); 177 178 if (utf8 == null) 179 return null; 180 else 181 return utf8.getValue(); 182 } 183 184 187 public void addConstant(ConstantPoolEntry entry) 188 { 189 if (entry instanceof Utf8Constant) { 190 Utf8Constant utf8 = (Utf8Constant) entry; 191 192 _utf8Map.put(utf8.getValue(), utf8); 193 } 194 195 _entries.add(entry); 196 } 197 198 201 public Utf8Constant getUTF8(String value) 202 { 203 return _utf8Map.get(value); 204 } 205 206 209 public Utf8Constant addUTF8(String value) 210 { 211 Utf8Constant entry = getUTF8(value); 212 213 if (entry != null) 214 return entry; 215 216 entry = new Utf8Constant(this, _entries.size(), value); 217 218 addConstant(entry); 219 220 return entry; 221 } 222 223 226 public StringConstant getString(String name) 227 { 228 for (int i = 0; i < _entries.size(); i++) { 229 ConstantPoolEntry entry = _entries.get(i); 230 231 if (! (entry instanceof StringConstant)) 232 continue; 233 234 StringConstant stringEntry = (StringConstant) entry; 235 236 if (stringEntry.getString().equals(name)) 237 return stringEntry; 238 } 239 240 return null; 241 } 242 243 246 public StringConstant addString(String name) 247 { 248 StringConstant entry = getString(name); 249 250 if (entry != null) 251 return entry; 252 253 Utf8Constant utf8 = addUTF8(name); 254 255 entry = new StringConstant(this, _entries.size(), utf8.getIndex()); 256 257 addConstant(entry); 258 259 return entry; 260 } 261 262 265 public IntegerConstant getIntegerByValue(int value) 266 { 267 for (int i = 0; i < _entries.size(); i++) { 268 ConstantPoolEntry entry = _entries.get(i); 269 270 if (! (entry instanceof IntegerConstant)) 271 continue; 272 273 IntegerConstant integerEntry = (IntegerConstant) entry; 274 275 if (integerEntry.getValue() == value) 276 return integerEntry; 277 } 278 279 return null; 280 } 281 282 285 public IntegerConstant addInteger(int value) 286 { 287 IntegerConstant entry = getIntegerByValue(value); 288 289 if (entry != null) 290 return entry; 291 292 entry = new IntegerConstant(this, _entries.size(), value); 293 294 addConstant(entry); 295 296 return entry; 297 } 298 299 302 public LongConstant getLongByValue(long value) 303 { 304 for (int i = 0; i < _entries.size(); i++) { 305 ConstantPoolEntry entry = _entries.get(i); 306 307 if (! (entry instanceof LongConstant)) 308 continue; 309 310 LongConstant longEntry = (LongConstant) entry; 311 312 if (longEntry.getValue() == value) 313 return longEntry; 314 } 315 316 return null; 317 } 318 319 322 public LongConstant addLong(long value) 323 { 324 LongConstant entry = getLongByValue(value); 325 326 if (entry != null) 327 return entry; 328 329 entry = new LongConstant(this, _entries.size(), value); 330 331 addConstant(entry); 332 addConstant(null); 333 334 return entry; 335 } 336 337 340 public FloatConstant getFloatByValue(float value) 341 { 342 for (int i = 0; i < _entries.size(); i++) { 343 ConstantPoolEntry entry = _entries.get(i); 344 345 if (! (entry instanceof FloatConstant)) 346 continue; 347 348 FloatConstant floatEntry = (FloatConstant) entry; 349 350 if (floatEntry.getValue() == value) 351 return floatEntry; 352 } 353 354 return null; 355 } 356 357 360 public FloatConstant addFloat(float value) 361 { 362 FloatConstant entry = getFloatByValue(value); 363 364 if (entry != null) 365 return entry; 366 367 entry = new FloatConstant(this, _entries.size(), value); 368 369 addConstant(entry); 370 371 return entry; 372 } 373 374 377 public DoubleConstant getDoubleByValue(double value) 378 { 379 for (int i = 0; i < _entries.size(); i++) { 380 ConstantPoolEntry entry = _entries.get(i); 381 382 if (! (entry instanceof DoubleConstant)) 383 continue; 384 385 DoubleConstant doubleEntry = (DoubleConstant) entry; 386 387 if (doubleEntry.getValue() == value) 388 return doubleEntry; 389 } 390 391 return null; 392 } 393 394 397 public DoubleConstant addDouble(double value) 398 { 399 DoubleConstant entry = getDoubleByValue(value); 400 401 if (entry != null) 402 return entry; 403 404 entry = new DoubleConstant(this, _entries.size(), value); 405 406 addConstant(entry); 407 addConstant(null); 408 409 return entry; 410 } 411 412 415 public ClassConstant getClass(String name) 416 { 417 for (int i = 0; i < _entries.size(); i++) { 418 ConstantPoolEntry entry = _entries.get(i); 419 420 if (! (entry instanceof ClassConstant)) 421 continue; 422 423 ClassConstant classEntry = (ClassConstant) entry; 424 425 if (classEntry.getName().equals(name)) 426 return classEntry; 427 } 428 429 return null; 430 } 431 432 435 public ClassConstant addClass(String name) 436 { 437 ClassConstant entry = getClass(name); 438 439 if (entry != null) 440 return entry; 441 442 Utf8Constant utf8 = addUTF8(name); 443 444 entry = new ClassConstant(this, _entries.size(), utf8.getIndex()); 445 446 addConstant(entry); 447 448 return entry; 449 } 450 451 454 public NameAndTypeConstant getNameAndType(String name, String type) 455 { 456 for (int i = 0; i < _entries.size(); i++) { 457 ConstantPoolEntry entry = _entries.get(i); 458 459 if (! (entry instanceof NameAndTypeConstant)) 460 continue; 461 462 NameAndTypeConstant methodEntry = (NameAndTypeConstant) entry; 463 464 if (methodEntry.getName().equals(name) && 465 methodEntry.getType().equals(type)) 466 return methodEntry; 467 } 468 469 return null; 470 } 471 472 475 public NameAndTypeConstant addNameAndType(String name, String type) 476 { 477 NameAndTypeConstant entry = getNameAndType(name, type); 478 479 if (entry != null) 480 return entry; 481 482 Utf8Constant nameEntry = addUTF8(name); 483 Utf8Constant typeEntry = addUTF8(type); 484 485 entry = new NameAndTypeConstant(this, _entries.size(), 486 nameEntry.getIndex(), 487 typeEntry.getIndex()); 488 489 addConstant(entry); 490 491 return entry; 492 } 493 494 497 public FieldRefConstant getFieldRef(String className, 498 String name, 499 String type) 500 { 501 for (int i = 0; i < _entries.size(); i++) { 502 ConstantPoolEntry entry = _entries.get(i); 503 504 if (! (entry instanceof FieldRefConstant)) 505 continue; 506 507 FieldRefConstant fieldEntry = (FieldRefConstant) entry; 508 509 if (fieldEntry.getClassName().equals(className) && 510 fieldEntry.getName().equals(name) && 511 fieldEntry.getType().equals(type)) 512 return fieldEntry; 513 } 514 515 return null; 516 } 517 518 521 public FieldRefConstant getFieldRef(String name) 522 { 523 for (int i = 0; i < _entries.size(); i++) { 524 ConstantPoolEntry entry = _entries.get(i); 525 526 if (! (entry instanceof FieldRefConstant)) 527 continue; 528 529 FieldRefConstant fieldEntry = (FieldRefConstant) entry; 530 531 if (fieldEntry.getName().equals(name)) 532 return fieldEntry; 533 } 534 535 return null; 536 } 537 538 541 public FieldRefConstant addFieldRef(String className, 542 String name, 543 String type) 544 { 545 FieldRefConstant entry = getFieldRef(className, name, type); 546 547 if (entry != null) 548 return entry; 549 550 ClassConstant classEntry = addClass(className); 551 NameAndTypeConstant typeEntry = addNameAndType(name, type); 552 553 entry = new FieldRefConstant(this, _entries.size(), 554 classEntry.getIndex(), 555 typeEntry.getIndex()); 556 557 addConstant(entry); 558 559 return entry; 560 } 561 562 565 public MethodRefConstant getMethodRef(String className, 566 String name, 567 String type) 568 { 569 for (int i = 0; i < _entries.size(); i++) { 570 ConstantPoolEntry entry = _entries.get(i); 571 572 if (! (entry instanceof MethodRefConstant)) 573 continue; 574 575 MethodRefConstant methodEntry = (MethodRefConstant) entry; 576 577 if (methodEntry.getClassName().equals(className) && 578 methodEntry.getName().equals(name) && 579 methodEntry.getType().equals(type)) 580 return methodEntry; 581 } 582 583 return null; 584 } 585 586 589 public MethodRefConstant addMethodRef(String className, 590 String name, 591 String type) 592 { 593 MethodRefConstant entry = getMethodRef(className, name, type); 594 595 if (entry != null) 596 return entry; 597 598 ClassConstant classEntry = addClass(className); 599 NameAndTypeConstant typeEntry = addNameAndType(name, type); 600 601 entry = new MethodRefConstant(this, _entries.size(), 602 classEntry.getIndex(), 603 typeEntry.getIndex()); 604 605 addConstant(entry); 606 607 return entry; 608 } 609 610 613 public InterfaceMethodRefConstant getInterfaceRef(String className, 614 String name, 615 String type) 616 { 617 for (int i = 0; i < _entries.size(); i++) { 618 ConstantPoolEntry entry = _entries.get(i); 619 620 if (! (entry instanceof InterfaceMethodRefConstant)) 621 continue; 622 623 InterfaceMethodRefConstant methodEntry; 624 methodEntry = (InterfaceMethodRefConstant) entry; 625 626 if (methodEntry.getClassName().equals(className) && 627 methodEntry.getName().equals(name) && 628 methodEntry.getType().equals(type)) 629 return methodEntry; 630 } 631 632 return null; 633 } 634 635 638 public InterfaceMethodRefConstant addInterfaceRef(String className, 639 String name, 640 String type) 641 { 642 InterfaceMethodRefConstant entry = getInterfaceRef(className, name, type); 643 644 if (entry != null) 645 return entry; 646 647 ClassConstant classEntry = addClass(className); 648 NameAndTypeConstant typeEntry = addNameAndType(name, type); 649 650 entry = new InterfaceMethodRefConstant(this, _entries.size(), 651 classEntry.getIndex(), 652 typeEntry.getIndex()); 653 654 addConstant(entry); 655 656 return entry; 657 } 658 659 662 void write(ByteCodeWriter out) 663 throws IOException 664 { 665 out.writeShort(_entries.size()); 666 667 for (int i = 1; i < _entries.size(); i++) { 668 ConstantPoolEntry entry = _entries.get(i); 669 670 if (entry != null) 671 entry.write(out); 672 } 673 } 674 } 675 | Popular Tags |