001// ASM: a very small and fast Java bytecode manipulation framework
002// Copyright (c) 2000-2011 INRIA, France Telecom
003// All rights reserved.
004//
005// Redistribution and use in source and binary forms, with or without
006// modification, are permitted provided that the following conditions
007// are met:
008// 1. Redistributions of source code must retain the above copyright
009//    notice, this list of conditions and the following disclaimer.
010// 2. Redistributions in binary form must reproduce the above copyright
011//    notice, this list of conditions and the following disclaimer in the
012//    documentation and/or other materials provided with the distribution.
013// 3. Neither the name of the copyright holders nor the names of its
014//    contributors may be used to endorse or promote products derived from
015//    this software without specific prior written permission.
016//
017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
027// THE POSSIBILITY OF SUCH DAMAGE.
028package io.ebean.enhance.asm;
029
030/**
031 * A {@link ClassVisitor} that generates a corresponding ClassFile structure, as defined in the Java
032 * Virtual Machine Specification (JVMS). It can be used alone, to generate a Java class "from
033 * scratch", or with one or more {@link ClassReader} and adapter {@link ClassVisitor} to generate a
034 * modified class from one or more existing Java classes.
035 *
036 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-4.html">JVMS 4</a>
037 * @author Eric Bruneton
038 */
039public class ClassWriter extends ClassVisitor {
040
041  /**
042   * A flag to automatically compute the maximum stack size and the maximum number of local
043   * variables of methods. If this flag is set, then the arguments of the {@link
044   * MethodVisitor#visitMaxs} method of the {@link MethodVisitor} returned by the {@link
045   * #visitMethod} method will be ignored, and computed automatically from the signature and the
046   * bytecode of each method.
047   *
048   * <p><b>Note:</b> for classes whose version is {@link Opcodes#V1_7} of more, this option requires
049   * valid stack map frames. The maximum stack size is then computed from these frames, and from the
050   * bytecode instructions in between. If stack map frames are not present or must be recomputed,
051   * used {@link #COMPUTE_FRAMES} instead.
052   *
053   * @see #ClassWriter(int)
054   */
055  public static final int COMPUTE_MAXS = 1;
056
057  /**
058   * A flag to automatically compute the stack map frames of methods from scratch. If this flag is
059   * set, then the calls to the {@link MethodVisitor#visitFrame} method are ignored, and the stack
060   * map frames are recomputed from the methods bytecode. The arguments of the {@link
061   * MethodVisitor#visitMaxs} method are also ignored and recomputed from the bytecode. In other
062   * words, {@link #COMPUTE_FRAMES} implies {@link #COMPUTE_MAXS}.
063   *
064   * @see #ClassWriter(int)
065   */
066  public static final int COMPUTE_FRAMES = 2;
067
068  // Note: fields are ordered as in the ClassFile structure, and those related to attributes are
069  // ordered as in Section 4.7 of the JVMS.
070
071  /**
072   * The minor_version and major_version fields of the JVMS ClassFile structure. minor_version is
073   * stored in the 16 most significant bits, and major_version in the 16 least significant bits.
074   */
075  private int version;
076
077  /** The symbol table for this class (contains the constant_pool and the BootstrapMethods). */
078  private final SymbolTable symbolTable;
079
080  /**
081   * The access_flags field of the JVMS ClassFile structure. This field can contain ASM specific
082   * access flags, such as {@link Opcodes#ACC_DEPRECATED} or {}@link Opcodes#ACC_RECORD}, which are
083   * removed when generating the ClassFile structure.
084   */
085  private int accessFlags;
086
087  /** The this_class field of the JVMS ClassFile structure. */
088  private int thisClass;
089
090  /** The super_class field of the JVMS ClassFile structure. */
091  private int superClass;
092
093  /** The interface_count field of the JVMS ClassFile structure. */
094  private int interfaceCount;
095
096  /** The 'interfaces' array of the JVMS ClassFile structure. */
097  private int[] interfaces;
098
099  /**
100   * The fields of this class, stored in a linked list of {@link FieldWriter} linked via their
101   * {@link FieldWriter#fv} field. This field stores the first element of this list.
102   */
103  private FieldWriter firstField;
104
105  /**
106   * The fields of this class, stored in a linked list of {@link FieldWriter} linked via their
107   * {@link FieldWriter#fv} field. This field stores the last element of this list.
108   */
109  private FieldWriter lastField;
110
111  /**
112   * The methods of this class, stored in a linked list of {@link MethodWriter} linked via their
113   * {@link MethodWriter#mv} field. This field stores the first element of this list.
114   */
115  private MethodWriter firstMethod;
116
117  /**
118   * The methods of this class, stored in a linked list of {@link MethodWriter} linked via their
119   * {@link MethodWriter#mv} field. This field stores the last element of this list.
120   */
121  private MethodWriter lastMethod;
122
123  /** The number_of_classes field of the InnerClasses attribute, or 0. */
124  private int numberOfInnerClasses;
125
126  /** The 'classes' array of the InnerClasses attribute, or {@literal null}. */
127  private ByteVector innerClasses;
128
129  /** The class_index field of the EnclosingMethod attribute, or 0. */
130  private int enclosingClassIndex;
131
132  /** The method_index field of the EnclosingMethod attribute. */
133  private int enclosingMethodIndex;
134
135  /** The signature_index field of the Signature attribute, or 0. */
136  private int signatureIndex;
137
138  /** The source_file_index field of the SourceFile attribute, or 0. */
139  private int sourceFileIndex;
140
141  /** The debug_extension field of the SourceDebugExtension attribute, or {@literal null}. */
142  private ByteVector debugExtension;
143
144  /**
145   * The last runtime visible annotation of this class. The previous ones can be accessed with the
146   * {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
147   */
148  private AnnotationWriter lastRuntimeVisibleAnnotation;
149
150  /**
151   * The last runtime invisible annotation of this class. The previous ones can be accessed with the
152   * {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
153   */
154  private AnnotationWriter lastRuntimeInvisibleAnnotation;
155
156  /**
157   * The last runtime visible type annotation of this class. The previous ones can be accessed with
158   * the {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
159   */
160  private AnnotationWriter lastRuntimeVisibleTypeAnnotation;
161
162  /**
163   * The last runtime invisible type annotation of this class. The previous ones can be accessed
164   * with the {@link AnnotationWriter#previousAnnotation} field. May be {@literal null}.
165   */
166  private AnnotationWriter lastRuntimeInvisibleTypeAnnotation;
167
168  /** The Module attribute of this class, or {@literal null}. */
169  private ModuleWriter moduleWriter;
170
171  /** The host_class_index field of the NestHost attribute, or 0. */
172  private int nestHostClassIndex;
173
174  /** The number_of_classes field of the NestMembers attribute, or 0. */
175  private int numberOfNestMemberClasses;
176
177  /** The 'classes' array of the NestMembers attribute, or {@literal null}. */
178  private ByteVector nestMemberClasses;
179
180  /** The number_of_classes field of the PermittedSubclasses attribute, or 0. */
181  private int numberOfPermittedSubclasses;
182
183  /** The 'classes' array of the PermittedSubclasses attribute, or {@literal null}. */
184  private ByteVector permittedSubclasses;
185
186  /**
187   * The record components of this class, stored in a linked list of {@link RecordComponentWriter}
188   * linked via their {@link RecordComponentWriter#delegate} field. This field stores the first
189   * element of this list.
190   */
191  private RecordComponentWriter firstRecordComponent;
192
193  /**
194   * The record components of this class, stored in a linked list of {@link RecordComponentWriter}
195   * linked via their {@link RecordComponentWriter#delegate} field. This field stores the last
196   * element of this list.
197   */
198  private RecordComponentWriter lastRecordComponent;
199
200  /**
201   * The first non standard attribute of this class. The next ones can be accessed with the {@link
202   * Attribute#nextAttribute} field. May be {@literal null}.
203   *
204   * <p><b>WARNING</b>: this list stores the attributes in the <i>reverse</i> order of their visit.
205   * firstAttribute is actually the last attribute visited in {@link #visitAttribute}. The {@link
206   * #toByteArray} method writes the attributes in the order defined by this list, i.e. in the
207   * reverse order specified by the user.
208   */
209  private Attribute firstAttribute;
210
211  /**
212   * Indicates what must be automatically computed in {@link MethodWriter}. Must be one of {@link
213   * MethodWriter#COMPUTE_NOTHING}, {@link MethodWriter#COMPUTE_MAX_STACK_AND_LOCAL}, {@link
214   * MethodWriter#COMPUTE_INSERTED_FRAMES}, or {@link MethodWriter#COMPUTE_ALL_FRAMES}.
215   */
216  private int compute;
217
218  // -----------------------------------------------------------------------------------------------
219  // Constructor
220  // -----------------------------------------------------------------------------------------------
221
222  /**
223   * Constructs a new {@link ClassWriter} object.
224   *
225   * @param flags option flags that can be used to modify the default behavior of this class. Must
226   *     be zero or more of {@link #COMPUTE_MAXS} and {@link #COMPUTE_FRAMES}.
227   */
228  public ClassWriter(final int flags) {
229    this(null, flags);
230  }
231
232  /**
233   * Constructs a new {@link ClassWriter} object and enables optimizations for "mostly add" bytecode
234   * transformations. These optimizations are the following:
235   *
236   * <ul>
237   *   <li>The constant pool and bootstrap methods from the original class are copied as is in the
238   *       new class, which saves time. New constant pool entries and new bootstrap methods will be
239   *       added at the end if necessary, but unused constant pool entries or bootstrap methods
240   *       <i>won't be removed</i>.
241   *   <li>Methods that are not transformed are copied as is in the new class, directly from the
242   *       original class bytecode (i.e. without emitting visit events for all the method
243   *       instructions), which saves a <i>lot</i> of time. Untransformed methods are detected by
244   *       the fact that the {@link ClassReader} receives {@link MethodVisitor} objects that come
245   *       from a {@link ClassWriter} (and not from any other {@link ClassVisitor} instance).
246   * </ul>
247   *
248   * @param classReader the {@link ClassReader} used to read the original class. It will be used to
249   *     copy the entire constant pool and bootstrap methods from the original class and also to
250   *     copy other fragments of original bytecode where applicable.
251   * @param flags option flags that can be used to modify the default behavior of this class.Must be
252   *     zero or more of {@link #COMPUTE_MAXS} and {@link #COMPUTE_FRAMES}. <i>These option flags do
253   *     not affect methods that are copied as is in the new class. This means that neither the
254   *     maximum stack size nor the stack frames will be computed for these methods</i>.
255   */
256  public ClassWriter(final ClassReader classReader, final int flags) {
257    super(/* latest api = */ Opcodes.ASM9);
258    symbolTable = classReader == null ? new SymbolTable(this) : new SymbolTable(this, classReader);
259    if ((flags & COMPUTE_FRAMES) != 0) {
260      this.compute = MethodWriter.COMPUTE_ALL_FRAMES;
261    } else if ((flags & COMPUTE_MAXS) != 0) {
262      this.compute = MethodWriter.COMPUTE_MAX_STACK_AND_LOCAL;
263    } else {
264      this.compute = MethodWriter.COMPUTE_NOTHING;
265    }
266  }
267
268  // -----------------------------------------------------------------------------------------------
269  // Implementation of the ClassVisitor abstract class
270  // -----------------------------------------------------------------------------------------------
271
272  @Override
273  public final void visit(
274      final int version,
275      final int access,
276      final String name,
277      final String signature,
278      final String superName,
279      final String[] interfaces) {
280    this.version = version;
281    this.accessFlags = access;
282    this.thisClass = symbolTable.setMajorVersionAndClassName(version & 0xFFFF, name);
283    if (signature != null) {
284      this.signatureIndex = symbolTable.addConstantUtf8(signature);
285    }
286    this.superClass = superName == null ? 0 : symbolTable.addConstantClass(superName).index;
287    if (interfaces != null && interfaces.length > 0) {
288      interfaceCount = interfaces.length;
289      this.interfaces = new int[interfaceCount];
290      for (int i = 0; i < interfaceCount; ++i) {
291        this.interfaces[i] = symbolTable.addConstantClass(interfaces[i]).index;
292      }
293    }
294    if (compute == MethodWriter.COMPUTE_MAX_STACK_AND_LOCAL && (version & 0xFFFF) >= Opcodes.V1_7) {
295      compute = MethodWriter.COMPUTE_MAX_STACK_AND_LOCAL_FROM_FRAMES;
296    }
297  }
298
299  @Override
300  public final void visitSource(final String file, final String debug) {
301    if (file != null) {
302      sourceFileIndex = symbolTable.addConstantUtf8(file);
303    }
304    if (debug != null) {
305      debugExtension = new ByteVector().encodeUtf8(debug, 0, Integer.MAX_VALUE);
306    }
307  }
308
309  @Override
310  public final ModuleVisitor visitModule(
311      final String name, final int access, final String version) {
312    return moduleWriter =
313        new ModuleWriter(
314            symbolTable,
315            symbolTable.addConstantModule(name).index,
316            access,
317            version == null ? 0 : symbolTable.addConstantUtf8(version));
318  }
319
320  @Override
321  public final void visitNestHost(final String nestHost) {
322    nestHostClassIndex = symbolTable.addConstantClass(nestHost).index;
323  }
324
325  @Override
326  public final void visitOuterClass(
327      final String owner, final String name, final String descriptor) {
328    enclosingClassIndex = symbolTable.addConstantClass(owner).index;
329    if (name != null && descriptor != null) {
330      enclosingMethodIndex = symbolTable.addConstantNameAndType(name, descriptor);
331    }
332  }
333
334  @Override
335  public final AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible) {
336    if (visible) {
337      return lastRuntimeVisibleAnnotation =
338          AnnotationWriter.create(symbolTable, descriptor, lastRuntimeVisibleAnnotation);
339    } else {
340      return lastRuntimeInvisibleAnnotation =
341          AnnotationWriter.create(symbolTable, descriptor, lastRuntimeInvisibleAnnotation);
342    }
343  }
344
345  @Override
346  public final AnnotationVisitor visitTypeAnnotation(
347      final int typeRef, final TypePath typePath, final String descriptor, final boolean visible) {
348    if (visible) {
349      return lastRuntimeVisibleTypeAnnotation =
350          AnnotationWriter.create(
351              symbolTable, typeRef, typePath, descriptor, lastRuntimeVisibleTypeAnnotation);
352    } else {
353      return lastRuntimeInvisibleTypeAnnotation =
354          AnnotationWriter.create(
355              symbolTable, typeRef, typePath, descriptor, lastRuntimeInvisibleTypeAnnotation);
356    }
357  }
358
359  @Override
360  public final void visitAttribute(final Attribute attribute) {
361    // Store the attributes in the <i>reverse</i> order of their visit by this method.
362    attribute.nextAttribute = firstAttribute;
363    firstAttribute = attribute;
364  }
365
366  @Override
367  public final void visitNestMember(final String nestMember) {
368    if (nestMemberClasses == null) {
369      nestMemberClasses = new ByteVector();
370    }
371    ++numberOfNestMemberClasses;
372    nestMemberClasses.putShort(symbolTable.addConstantClass(nestMember).index);
373  }
374
375  @Override
376  public final void visitPermittedSubclass(final String permittedSubclass) {
377    if (permittedSubclasses == null) {
378      permittedSubclasses = new ByteVector();
379    }
380    ++numberOfPermittedSubclasses;
381    permittedSubclasses.putShort(symbolTable.addConstantClass(permittedSubclass).index);
382  }
383
384  @Override
385  public final void visitInnerClass(
386      final String name, final String outerName, final String innerName, final int access) {
387    if (innerClasses == null) {
388      innerClasses = new ByteVector();
389    }
390    // Section 4.7.6 of the JVMS states "Every CONSTANT_Class_info entry in the constant_pool table
391    // which represents a class or interface C that is not a package member must have exactly one
392    // corresponding entry in the classes array". To avoid duplicates we keep track in the info
393    // field of the Symbol of each CONSTANT_Class_info entry C whether an inner class entry has
394    // already been added for C. If so, we store the index of this inner class entry (plus one) in
395    // the info field. This trick allows duplicate detection in O(1) time.
396    Symbol nameSymbol = symbolTable.addConstantClass(name);
397    if (nameSymbol.info == 0) {
398      ++numberOfInnerClasses;
399      innerClasses.putShort(nameSymbol.index);
400      innerClasses.putShort(outerName == null ? 0 : symbolTable.addConstantClass(outerName).index);
401      innerClasses.putShort(innerName == null ? 0 : symbolTable.addConstantUtf8(innerName));
402      innerClasses.putShort(access);
403      nameSymbol.info = numberOfInnerClasses;
404    }
405    // Else, compare the inner classes entry nameSymbol.info - 1 with the arguments of this method
406    // and throw an exception if there is a difference?
407  }
408
409  @Override
410  public final RecordComponentVisitor visitRecordComponent(
411      final String name, final String descriptor, final String signature) {
412    RecordComponentWriter recordComponentWriter =
413        new RecordComponentWriter(symbolTable, name, descriptor, signature);
414    if (firstRecordComponent == null) {
415      firstRecordComponent = recordComponentWriter;
416    } else {
417      lastRecordComponent.delegate = recordComponentWriter;
418    }
419    return lastRecordComponent = recordComponentWriter;
420  }
421
422  @Override
423  public final FieldVisitor visitField(
424      final int access,
425      final String name,
426      final String descriptor,
427      final String signature,
428      final Object value) {
429    FieldWriter fieldWriter =
430        new FieldWriter(symbolTable, access, name, descriptor, signature, value);
431    if (firstField == null) {
432      firstField = fieldWriter;
433    } else {
434      lastField.fv = fieldWriter;
435    }
436    return lastField = fieldWriter;
437  }
438
439  @Override
440  public final MethodVisitor visitMethod(
441      final int access,
442      final String name,
443      final String descriptor,
444      final String signature,
445      final String[] exceptions) {
446    MethodWriter methodWriter =
447        new MethodWriter(symbolTable, access, name, descriptor, signature, exceptions, compute);
448    if (firstMethod == null) {
449      firstMethod = methodWriter;
450    } else {
451      lastMethod.mv = methodWriter;
452    }
453    return lastMethod = methodWriter;
454  }
455
456  @Override
457  public final void visitEnd() {
458    // Nothing to do.
459  }
460
461  // -----------------------------------------------------------------------------------------------
462  // Other public methods
463  // -----------------------------------------------------------------------------------------------
464
465  /**
466   * Returns the content of the class file that was built by this ClassWriter.
467   *
468   * @return the binary content of the JVMS ClassFile structure that was built by this ClassWriter.
469   * @throws ClassTooLargeException if the constant pool of the class is too large.
470   * @throws MethodTooLargeException if the Code attribute of a method is too large.
471   */
472  public byte[] toByteArray() {
473    // First step: compute the size in bytes of the ClassFile structure.
474    // The magic field uses 4 bytes, 10 mandatory fields (minor_version, major_version,
475    // constant_pool_count, access_flags, this_class, super_class, interfaces_count, fields_count,
476    // methods_count and attributes_count) use 2 bytes each, and each interface uses 2 bytes too.
477    int size = 24 + 2 * interfaceCount;
478    int fieldsCount = 0;
479    FieldWriter fieldWriter = firstField;
480    while (fieldWriter != null) {
481      ++fieldsCount;
482      size += fieldWriter.computeFieldInfoSize();
483      fieldWriter = (FieldWriter) fieldWriter.fv;
484    }
485    int methodsCount = 0;
486    MethodWriter methodWriter = firstMethod;
487    while (methodWriter != null) {
488      ++methodsCount;
489      size += methodWriter.computeMethodInfoSize();
490      methodWriter = (MethodWriter) methodWriter.mv;
491    }
492
493    // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
494    int attributesCount = 0;
495    if (innerClasses != null) {
496      ++attributesCount;
497      size += 8 + innerClasses.length;
498      symbolTable.addConstantUtf8(Constants.INNER_CLASSES);
499    }
500    if (enclosingClassIndex != 0) {
501      ++attributesCount;
502      size += 10;
503      symbolTable.addConstantUtf8(Constants.ENCLOSING_METHOD);
504    }
505    if ((accessFlags & Opcodes.ACC_SYNTHETIC) != 0 && (version & 0xFFFF) < Opcodes.V1_5) {
506      ++attributesCount;
507      size += 6;
508      symbolTable.addConstantUtf8(Constants.SYNTHETIC);
509    }
510    if (signatureIndex != 0) {
511      ++attributesCount;
512      size += 8;
513      symbolTable.addConstantUtf8(Constants.SIGNATURE);
514    }
515    if (sourceFileIndex != 0) {
516      ++attributesCount;
517      size += 8;
518      symbolTable.addConstantUtf8(Constants.SOURCE_FILE);
519    }
520    if (debugExtension != null) {
521      ++attributesCount;
522      size += 6 + debugExtension.length;
523      symbolTable.addConstantUtf8(Constants.SOURCE_DEBUG_EXTENSION);
524    }
525    if ((accessFlags & Opcodes.ACC_DEPRECATED) != 0) {
526      ++attributesCount;
527      size += 6;
528      symbolTable.addConstantUtf8(Constants.DEPRECATED);
529    }
530    if (lastRuntimeVisibleAnnotation != null) {
531      ++attributesCount;
532      size +=
533          lastRuntimeVisibleAnnotation.computeAnnotationsSize(
534              Constants.RUNTIME_VISIBLE_ANNOTATIONS);
535    }
536    if (lastRuntimeInvisibleAnnotation != null) {
537      ++attributesCount;
538      size +=
539          lastRuntimeInvisibleAnnotation.computeAnnotationsSize(
540              Constants.RUNTIME_INVISIBLE_ANNOTATIONS);
541    }
542    if (lastRuntimeVisibleTypeAnnotation != null) {
543      ++attributesCount;
544      size +=
545          lastRuntimeVisibleTypeAnnotation.computeAnnotationsSize(
546              Constants.RUNTIME_VISIBLE_TYPE_ANNOTATIONS);
547    }
548    if (lastRuntimeInvisibleTypeAnnotation != null) {
549      ++attributesCount;
550      size +=
551          lastRuntimeInvisibleTypeAnnotation.computeAnnotationsSize(
552              Constants.RUNTIME_INVISIBLE_TYPE_ANNOTATIONS);
553    }
554    if (symbolTable.computeBootstrapMethodsSize() > 0) {
555      ++attributesCount;
556      size += symbolTable.computeBootstrapMethodsSize();
557    }
558    if (moduleWriter != null) {
559      attributesCount += moduleWriter.getAttributeCount();
560      size += moduleWriter.computeAttributesSize();
561    }
562    if (nestHostClassIndex != 0) {
563      ++attributesCount;
564      size += 8;
565      symbolTable.addConstantUtf8(Constants.NEST_HOST);
566    }
567    if (nestMemberClasses != null) {
568      ++attributesCount;
569      size += 8 + nestMemberClasses.length;
570      symbolTable.addConstantUtf8(Constants.NEST_MEMBERS);
571    }
572    if (permittedSubclasses != null) {
573      ++attributesCount;
574      size += 8 + permittedSubclasses.length;
575      symbolTable.addConstantUtf8(Constants.PERMITTED_SUBCLASSES);
576    }
577    int recordComponentCount = 0;
578    int recordSize = 0;
579    if ((accessFlags & Opcodes.ACC_RECORD) != 0 || firstRecordComponent != null) {
580      RecordComponentWriter recordComponentWriter = firstRecordComponent;
581      while (recordComponentWriter != null) {
582        ++recordComponentCount;
583        recordSize += recordComponentWriter.computeRecordComponentInfoSize();
584        recordComponentWriter = (RecordComponentWriter) recordComponentWriter.delegate;
585      }
586      ++attributesCount;
587      size += 8 + recordSize;
588      symbolTable.addConstantUtf8(Constants.RECORD);
589    }
590    if (firstAttribute != null) {
591      attributesCount += firstAttribute.getAttributeCount();
592      size += firstAttribute.computeAttributesSize(symbolTable);
593    }
594    // IMPORTANT: this must be the last part of the ClassFile size computation, because the previous
595    // statements can add attribute names to the constant pool, thereby changing its size!
596    size += symbolTable.getConstantPoolLength();
597    int constantPoolCount = symbolTable.getConstantPoolCount();
598    if (constantPoolCount > 0xFFFF) {
599      throw new ClassTooLargeException(symbolTable.getClassName(), constantPoolCount);
600    }
601
602    // Second step: allocate a ByteVector of the correct size (in order to avoid any array copy in
603    // dynamic resizes) and fill it with the ClassFile content.
604    ByteVector result = new ByteVector(size);
605    result.putInt(0xCAFEBABE).putInt(version);
606    symbolTable.putConstantPool(result);
607    int mask = (version & 0xFFFF) < Opcodes.V1_5 ? Opcodes.ACC_SYNTHETIC : 0;
608    result.putShort(accessFlags & ~mask).putShort(thisClass).putShort(superClass);
609    result.putShort(interfaceCount);
610    for (int i = 0; i < interfaceCount; ++i) {
611      result.putShort(interfaces[i]);
612    }
613    result.putShort(fieldsCount);
614    fieldWriter = firstField;
615    while (fieldWriter != null) {
616      fieldWriter.putFieldInfo(result);
617      fieldWriter = (FieldWriter) fieldWriter.fv;
618    }
619    result.putShort(methodsCount);
620    boolean hasFrames = false;
621    boolean hasAsmInstructions = false;
622    methodWriter = firstMethod;
623    while (methodWriter != null) {
624      hasFrames |= methodWriter.hasFrames();
625      hasAsmInstructions |= methodWriter.hasAsmInstructions();
626      methodWriter.putMethodInfo(result);
627      methodWriter = (MethodWriter) methodWriter.mv;
628    }
629    // For ease of reference, we use here the same attribute order as in Section 4.7 of the JVMS.
630    result.putShort(attributesCount);
631    if (innerClasses != null) {
632      result
633          .putShort(symbolTable.addConstantUtf8(Constants.INNER_CLASSES))
634          .putInt(innerClasses.length + 2)
635          .putShort(numberOfInnerClasses)
636          .putByteArray(innerClasses.data, 0, innerClasses.length);
637    }
638    if (enclosingClassIndex != 0) {
639      result
640          .putShort(symbolTable.addConstantUtf8(Constants.ENCLOSING_METHOD))
641          .putInt(4)
642          .putShort(enclosingClassIndex)
643          .putShort(enclosingMethodIndex);
644    }
645    if ((accessFlags & Opcodes.ACC_SYNTHETIC) != 0 && (version & 0xFFFF) < Opcodes.V1_5) {
646      result.putShort(symbolTable.addConstantUtf8(Constants.SYNTHETIC)).putInt(0);
647    }
648    if (signatureIndex != 0) {
649      result
650          .putShort(symbolTable.addConstantUtf8(Constants.SIGNATURE))
651          .putInt(2)
652          .putShort(signatureIndex);
653    }
654    if (sourceFileIndex != 0) {
655      result
656          .putShort(symbolTable.addConstantUtf8(Constants.SOURCE_FILE))
657          .putInt(2)
658          .putShort(sourceFileIndex);
659    }
660    if (debugExtension != null) {
661      int length = debugExtension.length;
662      result
663          .putShort(symbolTable.addConstantUtf8(Constants.SOURCE_DEBUG_EXTENSION))
664          .putInt(length)
665          .putByteArray(debugExtension.data, 0, length);
666    }
667    if ((accessFlags & Opcodes.ACC_DEPRECATED) != 0) {
668      result.putShort(symbolTable.addConstantUtf8(Constants.DEPRECATED)).putInt(0);
669    }
670    AnnotationWriter.putAnnotations(
671        symbolTable,
672        lastRuntimeVisibleAnnotation,
673        lastRuntimeInvisibleAnnotation,
674        lastRuntimeVisibleTypeAnnotation,
675        lastRuntimeInvisibleTypeAnnotation,
676        result);
677    symbolTable.putBootstrapMethods(result);
678    if (moduleWriter != null) {
679      moduleWriter.putAttributes(result);
680    }
681    if (nestHostClassIndex != 0) {
682      result
683          .putShort(symbolTable.addConstantUtf8(Constants.NEST_HOST))
684          .putInt(2)
685          .putShort(nestHostClassIndex);
686    }
687    if (nestMemberClasses != null) {
688      result
689          .putShort(symbolTable.addConstantUtf8(Constants.NEST_MEMBERS))
690          .putInt(nestMemberClasses.length + 2)
691          .putShort(numberOfNestMemberClasses)
692          .putByteArray(nestMemberClasses.data, 0, nestMemberClasses.length);
693    }
694    if (permittedSubclasses != null) {
695      result
696          .putShort(symbolTable.addConstantUtf8(Constants.PERMITTED_SUBCLASSES))
697          .putInt(permittedSubclasses.length + 2)
698          .putShort(numberOfPermittedSubclasses)
699          .putByteArray(permittedSubclasses.data, 0, permittedSubclasses.length);
700    }
701    if ((accessFlags & Opcodes.ACC_RECORD) != 0 || firstRecordComponent != null) {
702      result
703          .putShort(symbolTable.addConstantUtf8(Constants.RECORD))
704          .putInt(recordSize + 2)
705          .putShort(recordComponentCount);
706      RecordComponentWriter recordComponentWriter = firstRecordComponent;
707      while (recordComponentWriter != null) {
708        recordComponentWriter.putRecordComponentInfo(result);
709        recordComponentWriter = (RecordComponentWriter) recordComponentWriter.delegate;
710      }
711    }
712    if (firstAttribute != null) {
713      firstAttribute.putAttributes(symbolTable, result);
714    }
715
716    // Third step: replace the ASM specific instructions, if any.
717    if (hasAsmInstructions) {
718      return replaceAsmInstructions(result.data, hasFrames);
719    } else {
720      return result.data;
721    }
722  }
723
724  /**
725   * Returns the equivalent of the given class file, with the ASM specific instructions replaced
726   * with standard ones. This is done with a ClassReader -&gt; ClassWriter round trip.
727   *
728   * @param classFile a class file containing ASM specific instructions, generated by this
729   *     ClassWriter.
730   * @param hasFrames whether there is at least one stack map frames in 'classFile'.
731   * @return an equivalent of 'classFile', with the ASM specific instructions replaced with standard
732   *     ones.
733   */
734  private byte[] replaceAsmInstructions(final byte[] classFile, final boolean hasFrames) {
735    final Attribute[] attributes = getAttributePrototypes();
736    firstField = null;
737    lastField = null;
738    firstMethod = null;
739    lastMethod = null;
740    lastRuntimeVisibleAnnotation = null;
741    lastRuntimeInvisibleAnnotation = null;
742    lastRuntimeVisibleTypeAnnotation = null;
743    lastRuntimeInvisibleTypeAnnotation = null;
744    moduleWriter = null;
745    nestHostClassIndex = 0;
746    numberOfNestMemberClasses = 0;
747    nestMemberClasses = null;
748    numberOfPermittedSubclasses = 0;
749    permittedSubclasses = null;
750    firstRecordComponent = null;
751    lastRecordComponent = null;
752    firstAttribute = null;
753    compute = hasFrames ? MethodWriter.COMPUTE_INSERTED_FRAMES : MethodWriter.COMPUTE_NOTHING;
754    new ClassReader(classFile, 0, /* checkClassVersion = */ false)
755        .accept(
756            this,
757            attributes,
758            (hasFrames ? ClassReader.EXPAND_FRAMES : 0) | ClassReader.EXPAND_ASM_INSNS);
759    return toByteArray();
760  }
761
762  /**
763   * Returns the prototypes of the attributes used by this class, its fields and its methods.
764   *
765   * @return the prototypes of the attributes used by this class, its fields and its methods.
766   */
767  private Attribute[] getAttributePrototypes() {
768    Attribute.Set attributePrototypes = new Attribute.Set();
769    attributePrototypes.addAttributes(firstAttribute);
770    FieldWriter fieldWriter = firstField;
771    while (fieldWriter != null) {
772      fieldWriter.collectAttributePrototypes(attributePrototypes);
773      fieldWriter = (FieldWriter) fieldWriter.fv;
774    }
775    MethodWriter methodWriter = firstMethod;
776    while (methodWriter != null) {
777      methodWriter.collectAttributePrototypes(attributePrototypes);
778      methodWriter = (MethodWriter) methodWriter.mv;
779    }
780    RecordComponentWriter recordComponentWriter = firstRecordComponent;
781    while (recordComponentWriter != null) {
782      recordComponentWriter.collectAttributePrototypes(attributePrototypes);
783      recordComponentWriter = (RecordComponentWriter) recordComponentWriter.delegate;
784    }
785    return attributePrototypes.toArray();
786  }
787
788  // -----------------------------------------------------------------------------------------------
789  // Utility methods: constant pool management for Attribute sub classes
790  // -----------------------------------------------------------------------------------------------
791
792  /**
793   * Adds a number or string constant to the constant pool of the class being build. Does nothing if
794   * the constant pool already contains a similar item. <i>This method is intended for {@link
795   * Attribute} sub classes, and is normally not needed by class generators or adapters.</i>
796   *
797   * @param value the value of the constant to be added to the constant pool. This parameter must be
798   *     an {@link Integer}, a {@link Float}, a {@link Long}, a {@link Double} or a {@link String}.
799   * @return the index of a new or already existing constant item with the given value.
800   */
801  public int newConst(final Object value) {
802    return symbolTable.addConstant(value).index;
803  }
804
805  /**
806   * Adds an UTF8 string to the constant pool of the class being build. Does nothing if the constant
807   * pool already contains a similar item. <i>This method is intended for {@link Attribute} sub
808   * classes, and is normally not needed by class generators or adapters.</i>
809   *
810   * @param value the String value.
811   * @return the index of a new or already existing UTF8 item.
812   */
813  // DontCheck(AbbreviationAsWordInName): can't be renamed (for backward binary compatibility).
814  public int newUTF8(final String value) {
815    return symbolTable.addConstantUtf8(value);
816  }
817
818  /**
819   * Adds a class reference to the constant pool of the class being build. Does nothing if the
820   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
821   * sub classes, and is normally not needed by class generators or adapters.</i>
822   *
823   * @param value the internal name of the class.
824   * @return the index of a new or already existing class reference item.
825   */
826  public int newClass(final String value) {
827    return symbolTable.addConstantClass(value).index;
828  }
829
830  /**
831   * Adds a method type reference to the constant pool of the class being build. Does nothing if the
832   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
833   * sub classes, and is normally not needed by class generators or adapters.</i>
834   *
835   * @param methodDescriptor method descriptor of the method type.
836   * @return the index of a new or already existing method type reference item.
837   */
838  public int newMethodType(final String methodDescriptor) {
839    return symbolTable.addConstantMethodType(methodDescriptor).index;
840  }
841
842  /**
843   * Adds a module reference to the constant pool of the class being build. Does nothing if the
844   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
845   * sub classes, and is normally not needed by class generators or adapters.</i>
846   *
847   * @param moduleName name of the module.
848   * @return the index of a new or already existing module reference item.
849   */
850  public int newModule(final String moduleName) {
851    return symbolTable.addConstantModule(moduleName).index;
852  }
853
854  /**
855   * Adds a package reference to the constant pool of the class being build. Does nothing if the
856   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
857   * sub classes, and is normally not needed by class generators or adapters.</i>
858   *
859   * @param packageName name of the package in its internal form.
860   * @return the index of a new or already existing module reference item.
861   */
862  public int newPackage(final String packageName) {
863    return symbolTable.addConstantPackage(packageName).index;
864  }
865
866  /**
867   * Adds a handle to the constant pool of the class being build. Does nothing if the constant pool
868   * already contains a similar item. <i>This method is intended for {@link Attribute} sub classes,
869   * and is normally not needed by class generators or adapters.</i>
870   *
871   * @param tag the kind of this handle. Must be {@link Opcodes#H_GETFIELD}, {@link
872   *     Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC}, {@link
873   *     Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL},
874   *     {@link Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
875   * @param owner the internal name of the field or method owner class.
876   * @param name the name of the field or method.
877   * @param descriptor the descriptor of the field or method.
878   * @return the index of a new or already existing method type reference item.
879   * @deprecated this method is superseded by {@link #newHandle(int, String, String, String,
880   *     boolean)}.
881   */
882  @Deprecated
883  public int newHandle(
884      final int tag, final String owner, final String name, final String descriptor) {
885    return newHandle(tag, owner, name, descriptor, tag == Opcodes.H_INVOKEINTERFACE);
886  }
887
888  /**
889   * Adds a handle to the constant pool of the class being build. Does nothing if the constant pool
890   * already contains a similar item. <i>This method is intended for {@link Attribute} sub classes,
891   * and is normally not needed by class generators or adapters.</i>
892   *
893   * @param tag the kind of this handle. Must be {@link Opcodes#H_GETFIELD}, {@link
894   *     Opcodes#H_GETSTATIC}, {@link Opcodes#H_PUTFIELD}, {@link Opcodes#H_PUTSTATIC}, {@link
895   *     Opcodes#H_INVOKEVIRTUAL}, {@link Opcodes#H_INVOKESTATIC}, {@link Opcodes#H_INVOKESPECIAL},
896   *     {@link Opcodes#H_NEWINVOKESPECIAL} or {@link Opcodes#H_INVOKEINTERFACE}.
897   * @param owner the internal name of the field or method owner class.
898   * @param name the name of the field or method.
899   * @param descriptor the descriptor of the field or method.
900   * @param isInterface true if the owner is an interface.
901   * @return the index of a new or already existing method type reference item.
902   */
903  public int newHandle(
904      final int tag,
905      final String owner,
906      final String name,
907      final String descriptor,
908      final boolean isInterface) {
909    return symbolTable.addConstantMethodHandle(tag, owner, name, descriptor, isInterface).index;
910  }
911
912  /**
913   * Adds a dynamic constant reference to the constant pool of the class being build. Does nothing
914   * if the constant pool already contains a similar item. <i>This method is intended for {@link
915   * Attribute} sub classes, and is normally not needed by class generators or adapters.</i>
916   *
917   * @param name name of the invoked method.
918   * @param descriptor field descriptor of the constant type.
919   * @param bootstrapMethodHandle the bootstrap method.
920   * @param bootstrapMethodArguments the bootstrap method constant arguments.
921   * @return the index of a new or already existing dynamic constant reference item.
922   */
923  public int newConstantDynamic(
924      final String name,
925      final String descriptor,
926      final Handle bootstrapMethodHandle,
927      final Object... bootstrapMethodArguments) {
928    return symbolTable.addConstantDynamic(
929            name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments)
930        .index;
931  }
932
933  /**
934   * Adds an invokedynamic reference to the constant pool of the class being build. Does nothing if
935   * the constant pool already contains a similar item. <i>This method is intended for {@link
936   * Attribute} sub classes, and is normally not needed by class generators or adapters.</i>
937   *
938   * @param name name of the invoked method.
939   * @param descriptor descriptor of the invoke method.
940   * @param bootstrapMethodHandle the bootstrap method.
941   * @param bootstrapMethodArguments the bootstrap method constant arguments.
942   * @return the index of a new or already existing invokedynamic reference item.
943   */
944  public int newInvokeDynamic(
945      final String name,
946      final String descriptor,
947      final Handle bootstrapMethodHandle,
948      final Object... bootstrapMethodArguments) {
949    return symbolTable.addConstantInvokeDynamic(
950            name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments)
951        .index;
952  }
953
954  /**
955   * Adds a field reference to the constant pool of the class being build. Does nothing if the
956   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
957   * sub classes, and is normally not needed by class generators or adapters.</i>
958   *
959   * @param owner the internal name of the field's owner class.
960   * @param name the field's name.
961   * @param descriptor the field's descriptor.
962   * @return the index of a new or already existing field reference item.
963   */
964  public int newField(final String owner, final String name, final String descriptor) {
965    return symbolTable.addConstantFieldref(owner, name, descriptor).index;
966  }
967
968  /**
969   * Adds a method reference to the constant pool of the class being build. Does nothing if the
970   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
971   * sub classes, and is normally not needed by class generators or adapters.</i>
972   *
973   * @param owner the internal name of the method's owner class.
974   * @param name the method's name.
975   * @param descriptor the method's descriptor.
976   * @param isInterface {@literal true} if {@code owner} is an interface.
977   * @return the index of a new or already existing method reference item.
978   */
979  public int newMethod(
980      final String owner, final String name, final String descriptor, final boolean isInterface) {
981    return symbolTable.addConstantMethodref(owner, name, descriptor, isInterface).index;
982  }
983
984  /**
985   * Adds a name and type to the constant pool of the class being build. Does nothing if the
986   * constant pool already contains a similar item. <i>This method is intended for {@link Attribute}
987   * sub classes, and is normally not needed by class generators or adapters.</i>
988   *
989   * @param name a name.
990   * @param descriptor a type descriptor.
991   * @return the index of a new or already existing name and type item.
992   */
993  public int newNameType(final String name, final String descriptor) {
994    return symbolTable.addConstantNameAndType(name, descriptor);
995  }
996
997  // -----------------------------------------------------------------------------------------------
998  // Default method to compute common super classes when computing stack map frames
999  // -----------------------------------------------------------------------------------------------
1000
1001  /**
1002   * Returns the common super type of the two given types. The default implementation of this method
1003   * <i>loads</i> the two given classes and uses the java.lang.Class methods to find the common
1004   * super class. It can be overridden to compute this common super type in other ways, in
1005   * particular without actually loading any class, or to take into account the class that is
1006   * currently being generated by this ClassWriter, which can of course not be loaded since it is
1007   * under construction.
1008   *
1009   * @param type1 the internal name of a class.
1010   * @param type2 the internal name of another class.
1011   * @return the internal name of the common super class of the two given classes.
1012   */
1013  protected String getCommonSuperClass(final String type1, final String type2) {
1014    ClassLoader classLoader = getClassLoader();
1015    Class<?> class1;
1016    try {
1017      class1 = Class.forName(type1.replace('/', '.'), false, classLoader);
1018    } catch (ClassNotFoundException e) {
1019      throw new TypeNotPresentException(type1, e);
1020    }
1021    Class<?> class2;
1022    try {
1023      class2 = Class.forName(type2.replace('/', '.'), false, classLoader);
1024    } catch (ClassNotFoundException e) {
1025      throw new TypeNotPresentException(type2, e);
1026    }
1027    if (class1.isAssignableFrom(class2)) {
1028      return type1;
1029    }
1030    if (class2.isAssignableFrom(class1)) {
1031      return type2;
1032    }
1033    if (class1.isInterface() || class2.isInterface()) {
1034      return "java/lang/Object";
1035    } else {
1036      do {
1037        class1 = class1.getSuperclass();
1038      } while (!class1.isAssignableFrom(class2));
1039      return class1.getName().replace('.', '/');
1040    }
1041  }
1042
1043  /**
1044   * Returns the {@link ClassLoader} to be used by the default implementation of {@link
1045   * #getCommonSuperClass(String, String)}, that of this {@link ClassWriter}'s runtime type by
1046   * default.
1047   *
1048   * @return ClassLoader
1049   */
1050  protected ClassLoader getClassLoader() {
1051    return getClass().getClassLoader();
1052  }
1053}