View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.harmony.unpack200.bytecode;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import java.util.List;
24  
25  /**
26   * An entry in an exception table.
27   */
28  public class ExceptionTableEntry {
29  
30      private final int startPC;
31      private final int endPC;
32      private final int handlerPC;
33      private final CPClass catchType;
34  
35      private int startPcRenumbered;
36      private int endPcRenumbered;
37      private int handlerPcRenumbered;
38      private int catchTypeIndex;
39  
40      /**
41       * Constructs a new ExceptionTableEntry. Exception tables are of two kinds: either a normal one (with a Throwable as the catchType) or a finally clause
42       * (which has no catchType). In the class file, the finally clause is represented as catchType == 0.
43       *
44       * To create a finally clause with this method, pass in null for the catchType.
45       *
46       * @param startPC   int
47       * @param endPC     int
48       * @param handlerPC int
49       * @param catchType CPClass (if it's a normal catch) or null (if it's a finally clause).
50       */
51      public ExceptionTableEntry(final int startPC, final int endPC, final int handlerPC, final CPClass catchType) {
52          this.startPC = startPC;
53          this.endPC = endPC;
54          this.handlerPC = handlerPC;
55          this.catchType = catchType;
56      }
57  
58      public CPClass getCatchType() {
59          return catchType;
60      }
61  
62      public void renumber(final List<Integer> byteCodeOffsets) {
63          startPcRenumbered = byteCodeOffsets.get(startPC).intValue();
64          final int endPcIndex = startPC + endPC;
65          endPcRenumbered = byteCodeOffsets.get(endPcIndex).intValue();
66          final int handlerPcIndex = endPcIndex + handlerPC;
67          handlerPcRenumbered = byteCodeOffsets.get(handlerPcIndex).intValue();
68      }
69  
70      public void resolve(final ClassConstantPool pool) {
71          if (catchType == null) {
72              // If the catch type is a finally clause
73              // the index is always 0.
74              catchTypeIndex = 0;
75              return;
76          }
77          catchType.resolve(pool);
78          catchTypeIndex = pool.indexOf(catchType);
79      }
80  
81      public void write(final DataOutputStream dos) throws IOException {
82          dos.writeShort(startPcRenumbered);
83          dos.writeShort(endPcRenumbered);
84          dos.writeShort(handlerPcRenumbered);
85          dos.writeShort(catchTypeIndex);
86      }
87  }