View Javadoc
1   package mockit.asm.methods;
2   
3   import edu.umd.cs.findbugs.annotations.NonNull;
4   
5   import mockit.asm.constantPool.AttributeWriter;
6   import mockit.asm.constantPool.ConstantPoolGeneration;
7   import mockit.asm.util.ByteVector;
8   
9   import org.checkerframework.checker.index.qual.NonNegative;
10  
11  /**
12   * Stores the exceptions that can be thrown by a method/constructor, and writes it to the "Exceptions" attribute. For
13   * each thrown exception, stores the index of the constant pool item containing the internal name of the thrown
14   * exception class.
15   */
16  final class ExceptionsWriter extends AttributeWriter {
17      @NonNull
18      private final int[] exceptionIndices;
19  
20      ExceptionsWriter(@NonNull ConstantPoolGeneration cp, @NonNull String[] exceptionTypeDescs) {
21          super(cp, "Exceptions");
22          int n = exceptionTypeDescs.length;
23          exceptionIndices = new int[n];
24  
25          for (int i = 0; i < n; i++) {
26              exceptionIndices[i] = cp.newClass(exceptionTypeDescs[i]);
27          }
28      }
29  
30      @NonNegative
31      @Override
32      public int getSize() {
33          return 8 + 2 * exceptionIndices.length;
34      }
35  
36      @Override
37      public void put(@NonNull ByteVector out) {
38          int n = exceptionIndices.length;
39          put(out, 2 + 2 * n);
40          out.putShort(n);
41  
42          for (int exceptionIndex : exceptionIndices) {
43              out.putShort(exceptionIndex);
44          }
45      }
46  }