Returns the bytecode of the class that was build with this class writer. @return the bytecode of the class that was build with this class writer.
()
| 569 | */ |
| 570 | |
| 571 | public byte[] toByteArray () { |
| 572 | // computes the real size of the bytecode of this class |
| 573 | int size = 24 + 2*interfaceCount; |
| 574 | if (fields != null) { |
| 575 | size += fields.length; |
| 576 | } |
| 577 | int nbMethods = 0; |
| 578 | CodeWriter cb = firstMethod; |
| 579 | while (cb != null) { |
| 580 | ++nbMethods; |
| 581 | size += cb.getSize(); |
| 582 | cb = cb.next; |
| 583 | } |
| 584 | size += pool.length; |
| 585 | int attributeCount = 0; |
| 586 | if (sourceFile != null) { |
| 587 | ++attributeCount; |
| 588 | size += 8; |
| 589 | } |
| 590 | if ((access & Constants.ACC_DEPRECATED) != 0) { |
| 591 | ++attributeCount; |
| 592 | size += 6; |
| 593 | } |
| 594 | if (innerClasses != null) { |
| 595 | ++attributeCount; |
| 596 | size += 8 + innerClasses.length; |
| 597 | } |
| 598 | // allocates a byte vector of this size, in order to avoid unnecessary |
| 599 | // arraycopy operations in the ByteVector.enlarge() method |
| 600 | ByteVector out = new ByteVector(size); |
| 601 | out.put4(0xCAFEBABE).put2(3).put2(45); |
| 602 | out.put2(index).putByteArray(pool.data, 0, pool.length); |
| 603 | out.put2(access).put2(name).put2(superName); |
| 604 | out.put2(interfaceCount); |
| 605 | for (int i = 0; i < interfaceCount; ++i) { |
| 606 | out.put2(interfaces[i]); |
| 607 | } |
| 608 | out.put2(fieldCount); |
| 609 | if (fields != null) { |
| 610 | out.putByteArray(fields.data, 0, fields.length); |
| 611 | } |
| 612 | out.put2(nbMethods); |
| 613 | cb = firstMethod; |
| 614 | while (cb != null) { |
| 615 | cb.put(out); |
| 616 | cb = cb.next; |
| 617 | } |
| 618 | out.put2(attributeCount); |
| 619 | if (sourceFile != null) { |
| 620 | out.put2(newUTF8("SourceFile").index).put4(2).put2(sourceFile.index); |
| 621 | } |
| 622 | if ((access & Constants.ACC_DEPRECATED) != 0) { |
| 623 | out.put2(newUTF8("Deprecated").index).put4(0); |
| 624 | } |
| 625 | if (innerClasses != null) { |
| 626 | out.put2(newUTF8("InnerClasses").index); |
| 627 | out.put4(innerClasses.length + 2).put2(innerClassesCount); |
| 628 | out.putByteArray(innerClasses.data, 0, innerClasses.length); |