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