View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit.asm.constantPool;
7   
8   import edu.umd.cs.findbugs.annotations.NonNull;
9   
10  import mockit.asm.util.ByteVector;
11  
12  import org.checkerframework.checker.index.qual.NonNegative;
13  
14  public abstract class AttributeWriter {
15      @NonNull
16      protected final ConstantPoolGeneration cp;
17  
18      /**
19       * The index of the constant pool item that contains the name of the associated attribute.
20       */
21      @NonNegative
22      protected int attributeIndex;
23  
24      protected AttributeWriter(@NonNull ConstantPoolGeneration cp) {
25          this.cp = cp;
26      }
27  
28      protected AttributeWriter(@NonNull ConstantPoolGeneration cp, @NonNull String attributeName) {
29          this.cp = cp;
30          setAttribute(attributeName);
31      }
32  
33      protected final void setAttribute(@NonNull String attributeName) {
34          attributeIndex = cp.newUTF8(attributeName);
35      }
36  
37      @NonNegative
38      public abstract int getSize();
39  
40      public void put(@NonNull ByteVector out) {
41          put(out, 2);
42      }
43  
44      protected final void put(@NonNull ByteVector out, @NonNegative int size) {
45          out.putShort(attributeIndex).putInt(size);
46      }
47  }