    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 */
    017
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023import java.util.Arrays;
    024
    025import org.apache.bcel.Const;
    026import org.apache.commons.lang3.ArrayUtils;
    027
    028/**
    029 * This class represents the table of exceptions that are thrown by a method. This attribute may be used once per
    030 * method. The name of this class is <em>ExceptionTable</em> for historical reasons; The Java Virtual Machine
    031 * Specification, Second Edition defines this attribute using the name <em>Exceptions</em> (which is inconsistent with
    032 * the other classes).
    033 *
    034 * @see Code
    035 */
    036public final class ExceptionTable extends Attribute {
    037
    038    private int[] exceptionIndexTable; // constant pool
    039
    040    /**
    041     * Initialize from another object. Note that both objects use the same references (shallow copy). Use copy() for a
    042     * physical copy.
    043     */
    044    public ExceptionTable(final ExceptionTable c) {
    045        this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool());
    046    }
    047
    048    /**
    049     * Construct object from input stream.
    050     *
    051     * @param nameIndex Index in constant pool
    052     * @param length Content length in bytes
    053     * @param input Input stream
    054     * @param constantPool Array of constants
    055     * @throws IOException if an I/O error occurs.
    056     */
    057    ExceptionTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
    058        this(nameIndex, length, (int[]) null, constantPool);
    059        final int number_of_exceptions = input.readUnsignedShort();
    060        exceptionIndexTable = new int[number_of_exceptions];
    061        for (int i = 0; i < number_of_exceptions; i++) {
    062            exceptionIndexTable[i] = input.readUnsignedShort();
    063        }
    064    }
    065
    066    /**
    067     * @param name_index Index in constant pool
    068     * @param length Content length in bytes
    069     * @param exceptionIndexTable Table of indices in constant pool
    070     * @param constant_pool Array of constants
    071     */
    072    public ExceptionTable(final int name_index, final int length, final int[] exceptionIndexTable, final ConstantPool constant_pool) {
    073        super(Const.ATTR_EXCEPTIONS, name_index, length, constant_pool);
    074        this.exceptionIndexTable = exceptionIndexTable != null ? exceptionIndexTable : ArrayUtils.EMPTY_INT_ARRAY;
    075    }
    076
    077    /**
    078     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
    079     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
    080     *
    081     * @param v Visitor object
    082     */
    083    @Override
    084    public void accept(final Visitor v) {
    085        v.visitExceptionTable(this);
    086    }
    087
    088    /**
    089     * @return deep copy of this attribute
    090     */
    091    @Override
    092    public Attribute copy(final ConstantPool constantPool) {
    093        final ExceptionTable c = (ExceptionTable) clone();
    094        if (exceptionIndexTable != null) {
    095            c.exceptionIndexTable = new int[exceptionIndexTable.length];
    096            System.arraycopy(exceptionIndexTable, 0, c.exceptionIndexTable, 0, exceptionIndexTable.length);
    097        }
    098        c.setConstantPool(constantPool);
    099        return c;
    100    }
    101
    102    /**
    103     * Dump exceptions attribute to file stream in binary format.
    104     *
    105     * @param file Output file stream
    106     * @throws IOException if an I/O error occurs.
    107     */
    108    @Override
    109    public void dump(final DataOutputStream file) throws IOException {
    110        super.dump(file);
    111        file.writeShort(exceptionIndexTable.length);
    112        for (final int index : exceptionIndexTable) {
    113            file.writeShort(index);
    114        }
    115    }
    116
    117    /**
    118     * @return Array of indices into constant pool of thrown exceptions.
    119     */
    120    public int[] getExceptionIndexTable() {
    121        return exceptionIndexTable;
    122    }
    123
    124    /**
    125     * @return class names of thrown exceptions
    126     */
    127    public String[] getExceptionNames() {
    128        final String[] names = new String[exceptionIndexTable.length];
    129        Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class)));
    130        return names;
    131    }
    132
    133    /**
    134     * @return Length of exception table.
    135     */
    136    public int getNumberOfExceptions() {
    137        return exceptionIndexTable == null ? 0 : exceptionIndexTable.length;
    138    }
    139
    140    /**
    141     * @param exceptionIndexTable the list of exception indexes Also redefines number_of_exceptions according to table
    142     *        length.
    143     */
    144    public void setExceptionIndexTable(final int[] exceptionIndexTable) {
    145        this.exceptionIndexTable = exceptionIndexTable != null ? exceptionIndexTable : ArrayUtils.EMPTY_INT_ARRAY;
    146    }
    147
    148    /**
    149     * @return String representation, i.e., a list of thrown exceptions.
    150     */
    151    @Override
    152    public String toString() {
    153        final StringBuilder buf = new StringBuilder();
    154        String str;
    155        buf.append("Exceptions: ");
    156        for (int i = 0; i < exceptionIndexTable.length; i++) {
    157            str = super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class);
    158            buf.append(Utility.compactClassName(str, false));
    159            if (i < exceptionIndexTable.length - 1) {
    160                buf.append(", ");
    161            }
    162        }
    163        return buf.toString();
    164    }
    165}