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