ExceptionTableEntry.java

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

  21. /**
  22.  * An entry in an exception table.
  23.  */
  24. public class ExceptionTableEntry {

  25.     private final int startPC;
  26.     private final int endPC;
  27.     private final int handlerPC;
  28.     private final CPClass catchType;

  29.     private int startPcRenumbered;
  30.     private int endPcRenumbered;
  31.     private int handlerPcRenumbered;
  32.     private int catchTypeIndex;

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

  50.     public CPClass getCatchType() {
  51.         return catchType;
  52.     }

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

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

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