ExceptionTableEntry.java

  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. import java.io.DataOutputStream;
  21. import java.io.IOException;
  22. import java.util.List;

  23. /**
  24.  * An entry in an exception table.
  25.  */
  26. public class ExceptionTableEntry {

  27.     private final int startPC;
  28.     private final int endPC;
  29.     private final int handlerPC;
  30.     private final CPClass catchType;

  31.     private int startPcRenumbered;
  32.     private int endPcRenumbered;
  33.     private int handlerPcRenumbered;
  34.     private int catchTypeIndex;

  35.     /**
  36.      * Constructs a new ExceptionTableEntry. Exception tables are of two kinds: either a normal one (with a Throwable as the catchType) or a finally clause
  37.      * (which has no catchType). In the class file, the finally clause is represented as catchType == 0.
  38.      *
  39.      * To create a finally clause with this method, pass in null for the catchType.
  40.      *
  41.      * @param startPC   int
  42.      * @param endPC     int
  43.      * @param handlerPC int
  44.      * @param catchType CPClass (if it's a normal catch) or null (if it's a finally clause).
  45.      */
  46.     public ExceptionTableEntry(final int startPC, final int endPC, final int handlerPC, final CPClass catchType) {
  47.         this.startPC = startPC;
  48.         this.endPC = endPC;
  49.         this.handlerPC = handlerPC;
  50.         this.catchType = catchType;
  51.     }

  52.     public CPClass getCatchType() {
  53.         return catchType;
  54.     }

  55.     public void renumber(final List<Integer> byteCodeOffsets) {
  56.         startPcRenumbered = byteCodeOffsets.get(startPC).intValue();
  57.         final int endPcIndex = startPC + endPC;
  58.         endPcRenumbered = byteCodeOffsets.get(endPcIndex).intValue();
  59.         final int handlerPcIndex = endPcIndex + handlerPC;
  60.         handlerPcRenumbered = byteCodeOffsets.get(handlerPcIndex).intValue();
  61.     }

  62.     public void resolve(final ClassConstantPool pool) {
  63.         if (catchType == null) {
  64.             // If the catch type is a finally clause
  65.             // the index is always 0.
  66.             catchTypeIndex = 0;
  67.             return;
  68.         }
  69.         catchType.resolve(pool);
  70.         catchTypeIndex = pool.indexOf(catchType);
  71.     }

  72.     public void write(final DataOutputStream dos) throws IOException {
  73.         dos.writeShort(startPcRenumbered);
  74.         dos.writeShort(endPcRenumbered);
  75.         dos.writeShort(handlerPcRenumbered);
  76.         dos.writeShort(catchTypeIndex);
  77.     }
  78. }