View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class represents the table of exceptions that are thrown by a
28   * method. This attribute may be used once per method.  The name of
29   * this class is <em>ExceptionTable</em> for historical reasons; The
30   * Java Virtual Machine Specification, Second Edition defines this
31   * attribute using the name <em>Exceptions</em> (which is inconsistent
32   * with the other classes).
33   *
34   * @see     Code
35   */
36  public final class ExceptionTable extends Attribute {
37  
38      private int[] exceptionIndexTable; // constant pool
39  
40  
41      /**
42       * Initialize from another object. Note that both objects use the same
43       * references (shallow copy). Use copy() for a physical copy.
44       */
45      public ExceptionTableeptionTable.html#ExceptionTable">ExceptionTable(final ExceptionTable c) {
46          this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool());
47      }
48  
49  
50      /**
51       * @param name_index Index in constant pool
52       * @param length Content length in bytes
53       * @param exceptionIndexTable Table of indices in constant pool
54       * @param constant_pool Array of constants
55       */
56      public ExceptionTable(final int name_index, final int length, final int[] exceptionIndexTable,
57              final ConstantPool constant_pool) {
58          super(Const.ATTR_EXCEPTIONS, name_index, length, constant_pool);
59          this.exceptionIndexTable = exceptionIndexTable != null ? exceptionIndexTable : new int[0];
60      }
61  
62  
63      /**
64       * Construct object from input stream.
65       * @param nameIndex Index in constant pool
66       * @param length Content length in bytes
67       * @param input Input stream
68       * @param constantPool Array of constants
69       * @throws IOException
70       */
71      ExceptionTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
72          this(nameIndex, length, (int[]) null, constantPool);
73          final int number_of_exceptions = input.readUnsignedShort();
74          exceptionIndexTable = new int[number_of_exceptions];
75          for (int i = 0; i < number_of_exceptions; i++) {
76              exceptionIndexTable[i] = input.readUnsignedShort();
77          }
78      }
79  
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitely
83       * defined by the contents of a Java class. I.e., the hierarchy of methods,
84       * fields, attributes, etc. spawns a tree of objects.
85       *
86       * @param v Visitor object
87       */
88      @Override
89      public void accept( final Visitor v ) {
90          v.visitExceptionTable(this);
91      }
92  
93  
94      /**
95       * Dump exceptions attribute to file stream in binary format.
96       *
97       * @param file Output file stream
98       * @throws IOException
99       */
100     @Override
101     public void dump( final DataOutputStream file ) throws IOException {
102         super.dump(file);
103         file.writeShort(exceptionIndexTable.length);
104         for (final int index : exceptionIndexTable) {
105             file.writeShort(index);
106         }
107     }
108 
109 
110     /**
111      * @return Array of indices into constant pool of thrown exceptions.
112      */
113     public int[] getExceptionIndexTable() {
114         return exceptionIndexTable;
115     }
116 
117 
118     /**
119      * @return Length of exception table.
120      */
121     public int getNumberOfExceptions() {
122         return exceptionIndexTable == null ? 0 : exceptionIndexTable.length;
123     }
124 
125 
126     /**
127      * @return class names of thrown exceptions
128      */
129     public String[] getExceptionNames() {
130         final String[] names = new String[exceptionIndexTable.length];
131         for (int i = 0; i < exceptionIndexTable.length; i++) {
132             names[i] = super.getConstantPool().getConstantString(exceptionIndexTable[i],
133                     Const.CONSTANT_Class).replace('/', '.');
134         }
135         return names;
136     }
137 
138 
139     /**
140      * @param exceptionIndexTable the list of exception indexes
141      * Also redefines number_of_exceptions according to table length.
142      */
143     public void setExceptionIndexTable( final int[] exceptionIndexTable ) {
144         this.exceptionIndexTable = exceptionIndexTable != null ? exceptionIndexTable : new int[0];
145     }
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 
166 
167     /**
168      * @return deep copy of this attribute
169      */
170     @Override
171     public Attribute copy( final ConstantPool _constant_pool ) {
172         final ExceptionTable../../../org/apache/bcel/classfile/ExceptionTable.html#ExceptionTable">ExceptionTable c = (ExceptionTable) clone();
173         if (exceptionIndexTable != null) {
174             c.exceptionIndexTable = new int[exceptionIndexTable.length];
175             System.arraycopy(exceptionIndexTable, 0, c.exceptionIndexTable, 0,
176                     exceptionIndexTable.length);
177         }
178         c.setConstantPool(_constant_pool);
179         return c;
180     }
181 }