    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 */
    017package org.apache.commons.compress.harmony.unpack200.bytecode;
    018
    019import java.io.DataOutputStream;
    020import java.io.IOException;
    021import java.util.List;
    022
    023/**
    024 * An entry in an exception table.
    025 */
    026public class ExceptionTableEntry {
    027
    028    private final int startPC;
    029    private final int endPC;
    030    private final int handlerPC;
    031    private final CPClass catchType;
    032
    033    private int startPcRenumbered;
    034    private int endPcRenumbered;
    035    private int handlerPcRenumbered;
    036    private int catchTypeIndex;
    037
    038    /**
    039     * Constructs a new ExceptionTableEntry. Exception tables are of two kinds: either a normal one (with a Throwable as the catchType) or a finally clause
    040     * (which has no catchType). In the class file, the finally clause is represented as catchType == 0.
    041     *
    042     * To create a finally clause with this method, pass in null for the catchType.
    043     *
    044     * @param startPC   int
    045     * @param endPC     int
    046     * @param handlerPC int
    047     * @param catchType CPClass (if it's a normal catch) or null (if it's a finally clause).
    048     */
    049    public ExceptionTableEntry(final int startPC, final int endPC, final int handlerPC, final CPClass catchType) {
    050        this.startPC = startPC;
    051        this.endPC = endPC;
    052        this.handlerPC = handlerPC;
    053        this.catchType = catchType;
    054    }
    055
    056    public CPClass getCatchType() {
    057        return catchType;
    058    }
    059
    060    public void renumber(final List<Integer> byteCodeOffsets) {
    061        startPcRenumbered = byteCodeOffsets.get(startPC).intValue();
    062        final int endPcIndex = startPC + endPC;
    063        endPcRenumbered = byteCodeOffsets.get(endPcIndex).intValue();
    064        final int handlerPcIndex = endPcIndex + handlerPC;
    065        handlerPcRenumbered = byteCodeOffsets.get(handlerPcIndex).intValue();
    066    }
    067
    068    public void resolve(final ClassConstantPool pool) {
    069        if (catchType == null) {
    070            // If the catch type is a finally clause
    071            // the index is always 0.
    072            catchTypeIndex = 0;
    073            return;
    074        }
    075        catchType.resolve(pool);
    076        catchTypeIndex = pool.indexOf(catchType);
    077    }
    078
    079    public void write(final DataOutputStream dos) throws IOException {
    080        dos.writeShort(startPcRenumbered);
    081        dos.writeShort(endPcRenumbered);
    082        dos.writeShort(handlerPcRenumbered);
    083        dos.writeShort(catchTypeIndex);
    084    }
    085}