    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     * Create a new ExceptionTableEntry. Exception tables are of two kinds: either a normal one (with a Throwable as the
    040     * catch_type) or a finally clause (which has no catch_type). In the class file, the finally clause is represented
    041     * as catch_type == 0.
    042     *
    043     * To create a finally clause with this method, pass in null for the catchType.
    044     *
    045     * @param startPC int
    046     * @param endPC int
    047     * @param handlerPC int
    048     * @param catchType CPClass (if it's a normal catch) or null (if it's a finally clause).
    049     */
    050    public ExceptionTableEntry(final int startPC, final int endPC, final int handlerPC, final CPClass catchType) {
    051        this.startPC = startPC;
    052        this.endPC = endPC;
    053        this.handlerPC = handlerPC;
    054        this.catchType = catchType;
    055    }
    056
    057    public void write(final DataOutputStream dos) throws IOException {
    058        dos.writeShort(startPcRenumbered);
    059        dos.writeShort(endPcRenumbered);
    060        dos.writeShort(handlerPcRenumbered);
    061        dos.writeShort(catchTypeIndex);
    062    }
    063
    064    public void renumber(final List byteCodeOffsets) {
    065        startPcRenumbered = ((Integer) byteCodeOffsets.get(startPC)).intValue();
    066        final int endPcIndex = startPC + endPC;
    067        endPcRenumbered = ((Integer) byteCodeOffsets.get(endPcIndex)).intValue();
    068        final int handlerPcIndex = endPcIndex + handlerPC;
    069        handlerPcRenumbered = ((Integer) byteCodeOffsets.get(handlerPcIndex)).intValue();
    070    }
    071
    072    public CPClass getCatchType() {
    073        return catchType;
    074    }
    075
    076    public void resolve(final ClassConstantPool pool) {
    077        if (catchType == null) {
    078            // If the catch type is a finally clause
    079            // the index is always 0.
    080            catchTypeIndex = 0;
    081            return;
    082        }
    083        catchType.resolve(pool);
    084        catchTypeIndex = pool.indexOf(catchType);
    085    }
    086}