Returns the content of the class file that was built by this ClassWriter. @return the binary content of the JVMS ClassFile structure that was built by this ClassWriter.
()
| 438 | * @return the binary content of the JVMS ClassFile structure that was built by this ClassWriter. |
| 439 | */ |
| 440 | public byte[] toByteArray() { |
| 441 | // First step: compute the size in bytes of the ClassFile structure. |
| 442 | // The magic field uses 4 bytes, 10 mandatory fields (minor_version, major_version, |
| 443 | // constant_pool_count, access_flags, this_class, super_class, interfaces_count, fields_count, |
| 444 | // methods_count and attributes_count) use 2 bytes each, and each interface uses 2 bytes too. |
| 445 | int size = 24 + 2 * interfaceCount; |
| 446 | int fieldsCount = 0; |
| 447 | FieldWriter fieldWriter = firstField; |
| 448 | while (fieldWriter != null) { |
| 449 | ++fieldsCount; |
| 450 | size += fieldWriter.computeFieldInfoSize(); |
| 451 | fieldWriter = (FieldWriter) fieldWriter.fv; |
| 452 | } |
| 453 | int methodsCount = 0; |
| 454 | MethodWriter methodWriter = firstMethod; |
| 455 | while (methodWriter != null) { |
| 456 | ++methodsCount; |
| 457 | size += methodWriter.computeMethodInfoSize(); |
| 458 | methodWriter = (MethodWriter) methodWriter.mv; |
| 459 | } |
| 460 | // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS. |
| 461 | int attributesCount = 0; |
| 462 | if (innerClasses != null) { |
| 463 | ++attributesCount; |
| 464 | size += 8 + innerClasses.length; |
| 465 | symbolTable.addConstantUtf8(Constants.INNER_CLASSES); |
| 466 | } |
| 467 | if (enclosingClassIndex != 0) { |
| 468 | ++attributesCount; |
| 469 | size += 10; |
| 470 | symbolTable.addConstantUtf8(Constants.ENCLOSING_METHOD); |
| 471 | } |
| 472 | if ((accessFlags & Opcodes.ACC_SYNTHETIC) != 0 && (version & 0xFFFF) < Opcodes.V1_5) { |
| 473 | ++attributesCount; |
| 474 | size += 6; |
| 475 | symbolTable.addConstantUtf8(Constants.SYNTHETIC); |
| 476 | } |
| 477 | if (signatureIndex != 0) { |
| 478 | ++attributesCount; |
| 479 | size += 8; |
| 480 | symbolTable.addConstantUtf8(Constants.SIGNATURE); |
| 481 | } |
| 482 | if (sourceFileIndex != 0) { |
| 483 | ++attributesCount; |
| 484 | size += 8; |
| 485 | symbolTable.addConstantUtf8(Constants.SOURCE_FILE); |
| 486 | } |
| 487 | if (debugExtension != null) { |
| 488 | ++attributesCount; |
| 489 | size += 6 + debugExtension.length; |
| 490 | symbolTable.addConstantUtf8(Constants.SOURCE_DEBUG_EXTENSION); |
| 491 | } |
| 492 | if ((accessFlags & Opcodes.ACC_DEPRECATED) != 0) { |
| 493 | ++attributesCount; |
| 494 | size += 6; |
| 495 | symbolTable.addConstantUtf8(Constants.DEPRECATED); |
| 496 | } |
| 497 | if (lastRuntimeVisibleAnnotation != null) { |
no test coverage detected