    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     * catchType) or a finally clause (which has no catchType). In the class file, the finally clause is represented
    041     * as catchType == 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 CPClass getCatchType() {
    058        return catchType;
    059    }
    060
    061    public void renumber(final List<Integer> byteCodeOffsets) {
    062        startPcRenumbered = byteCodeOffsets.get(startPC).intValue();
    063        final int endPcIndex = startPC + endPC;
    064        endPcRenumbered = byteCodeOffsets.get(endPcIndex).intValue();
    065        final int handlerPcIndex = endPcIndex + handlerPC;
    066        handlerPcRenumbered = byteCodeOffsets.get(handlerPcIndex).intValue();
    067    }
    068
    069    public void resolve(final ClassConstantPool pool) {
    070        if (catchType == null) {
    071            // If the catch type is a finally clause
    072            // the index is always 0.
    073            catchTypeIndex = 0;
    074            return;
    075        }
    076        catchType.resolve(pool);
    077        catchTypeIndex = pool.indexOf(catchType);
    078    }
    079
    080    public void write(final DataOutputStream dos) throws IOException {
    081        dos.writeShort(startPcRenumbered);
    082        dos.writeShort(endPcRenumbered);
    083        dos.writeShort(handlerPcRenumbered);
    084        dos.writeShort(catchTypeIndex);
    085    }
    086}