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