    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 exceptionCount = input.readUnsignedShort();
    060        exceptionIndexTable = new int[exceptionCount];
    061        for (int i = 0; i < exceptionCount; i++) {
    062            exceptionIndexTable[i] = input.readUnsignedShort();
    063        }
    064    }
    065
    066    /**
    067     * @param nameIndex Index in constant pool
    068     * @param length Content length in bytes
    069     * @param exceptionIndexTable Table of indices in constant pool
    070     * @param constantPool Array of constants
    071     */
    072    public ExceptionTable(final int nameIndex, final int length, final int[] exceptionIndexTable, final ConstantPool constantPool) {
    073        super(Const.ATTR_EXCEPTIONS, nameIndex, length, constantPool);
    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 = exceptionIndexTable.clone();
    096        }
    097        c.setConstantPool(constantPool);
    098        return c;
    099    }
    100
    101    /**
    102     * Dump exceptions attribute to file stream in binary format.
    103     *
    104     * @param file Output file stream
    105     * @throws IOException if an I/O error occurs.
    106     */
    107    @Override
    108    public void dump(final DataOutputStream file) throws IOException {
    109        super.dump(file);
    110        file.writeShort(exceptionIndexTable.length);
    111        for (final int index : exceptionIndexTable) {
    112            file.writeShort(index);
    113        }
    114    }
    115
    116    /**
    117     * @return Array of indices into constant pool of thrown exceptions.
    118     */
    119    public int[] getExceptionIndexTable() {
    120        return exceptionIndexTable;
    121    }
    122
    123    /**
    124     * @return class names of thrown exceptions
    125     */
    126    public String[] getExceptionNames() {
    127        final String[] names = new String[exceptionIndexTable.length];
    128        Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class)));
    129        return names;
    130    }
    131
    132    /**
    133     * @return Length of exception table.
    134     */
    135    public int getNumberOfExceptions() {
    136        return exceptionIndexTable == null ? 0 : exceptionIndexTable.length;
    137    }
    138
    139    /**
    140     * @param exceptionIndexTable the list of exception indexes Also redefines number_of_exceptions according to table
    141     *        length.
    142     */
    143    public void setExceptionIndexTable(final int[] exceptionIndexTable) {
    144        this.exceptionIndexTable = exceptionIndexTable != null ? exceptionIndexTable : ArrayUtils.EMPTY_INT_ARRAY;
    145    }
    146
    147    /**
    148     * @return String representation, i.e., a list of thrown exceptions.
    149     */
    150    @Override
    151    public String toString() {
    152        final StringBuilder buf = new StringBuilder();
    153        String str;
    154        buf.append("Exceptions: ");
    155        for (int i = 0; i < exceptionIndexTable.length; i++) {
    156            str = super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class);
    157            buf.append(Utility.compactClassName(str, false));
    158            if (i < exceptionIndexTable.length - 1) {
    159                buf.append(", ");
    160            }
    161        }
    162        return buf.toString();
    163    }
    164}