KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bcel > classfile > ExceptionTable


1 /*
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.bcel.classfile;
18
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.bcel.Constants;
23
24 /**
25  * This class represents the table of exceptions that are thrown by a
26  * method. This attribute may be used once per method. The name of
27  * this class is <em>ExceptionTable</em> for historical reasons; The
28  * Java Virtual Machine Specification, Second Edition defines this
29  * attribute using the name <em>Exceptions</em> (which is inconsistent
30  * with the other classes).
31  *
32  * @version $Id: ExceptionTable.java 386056 2006-03-15 11:31:56Z tcurdt $
33  * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
34  * @see Code
35  */

36 public final class ExceptionTable extends Attribute {
37
38     private int number_of_exceptions; // Table of indices into
39
private int[] exception_index_table; // constant pool
40

41
42     /**
43      * Initialize from another object. Note that both objects use the same
44      * references (shallow copy). Use copy() for a physical copy.
45      */

46     public ExceptionTable(ExceptionTable c) {
47         this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool());
48     }
49
50
51     /**
52      * @param name_index Index in constant pool
53      * @param length Content length in bytes
54      * @param exception_index_table Table of indices in constant pool
55      * @param constant_pool Array of constants
56      */

57     public ExceptionTable(int name_index, int length, int[] exception_index_table,
58             ConstantPool constant_pool) {
59         super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool);
60         setExceptionIndexTable(exception_index_table);
61     }
62
63
64     /**
65      * Construct object from file stream.
66      * @param name_index Index in constant pool
67      * @param length Content length in bytes
68      * @param file Input stream
69      * @param constant_pool Array of constants
70      * @throws IOException
71      */

72     ExceptionTable(int name_index, int length, DataInputStream JavaDoc file, ConstantPool constant_pool)
73             throws IOException JavaDoc {
74         this(name_index, length, (int[]) null, constant_pool);
75         number_of_exceptions = file.readUnsignedShort();
76         exception_index_table = new int[number_of_exceptions];
77         for (int i = 0; i < number_of_exceptions; i++) {
78             exception_index_table[i] = file.readUnsignedShort();
79         }
80     }
81
82
83     /**
84      * Called by objects that are traversing the nodes of the tree implicitely
85      * defined by the contents of a Java class. I.e., the hierarchy of methods,
86      * fields, attributes, etc. spawns a tree of objects.
87      *
88      * @param v Visitor object
89      */

90     public void accept( Visitor v ) {
91         v.visitExceptionTable(this);
92     }
93
94
95     /**
96      * Dump exceptions attribute to file stream in binary format.
97      *
98      * @param file Output file stream
99      * @throws IOException
100      */

101     public final void dump( DataOutputStream JavaDoc file ) throws IOException JavaDoc {
102         super.dump(file);
103         file.writeShort(number_of_exceptions);
104         for (int i = 0; i < number_of_exceptions; i++) {
105             file.writeShort(exception_index_table[i]);
106         }
107     }
108
109
110     /**
111      * @return Array of indices into constant pool of thrown exceptions.
112      */

113     public final int[] getExceptionIndexTable() {
114         return exception_index_table;
115     }
116
117
118     /**
119      * @return Length of exception table.
120      */

121     public final int getNumberOfExceptions() {
122         return number_of_exceptions;
123     }
124
125
126     /**
127      * @return class names of thrown exceptions
128      */

129     public final String JavaDoc[] getExceptionNames() {
130         String JavaDoc[] names = new String JavaDoc[number_of_exceptions];
131         for (int i = 0; i < number_of_exceptions; i++) {
132             names[i] = constant_pool.getConstantString(exception_index_table[i],
133                     Constants.CONSTANT_Class).replace('/', '.');
134         }
135         return names;
136     }
137
138
139     /**
140      * @param exception_index_table the list of exception indexes
141      * Also redefines number_of_exceptions according to table length.
142      */

143     public final void setExceptionIndexTable( int[] exception_index_table ) {
144         this.exception_index_table = exception_index_table;
145         number_of_exceptions = (exception_index_table == null) ? 0 : exception_index_table.length;
146     }
147
148
149     /**
150      * @return String representation, i.e., a list of thrown exceptions.
151      */

152     public final String JavaDoc toString() {
153         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("");
154         String JavaDoc str;
155         for (int i = 0; i < number_of_exceptions; i++) {
156             str = constant_pool.getConstantString(exception_index_table[i],
157                     Constants.CONSTANT_Class);
158             buf.append(Utility.compactClassName(str, false));
159             if (i < number_of_exceptions - 1) {
160                 buf.append(", ");
161             }
162         }
163         return buf.toString();
164     }
165
166
167     /**
168      * @return deep copy of this attribute
169      */

170     public Attribute copy( ConstantPool _constant_pool ) {
171         ExceptionTable c = (ExceptionTable) clone();
172         if (exception_index_table != null) {
173             c.exception_index_table = new int[exception_index_table.length];
174             System.arraycopy(exception_index_table, 0, c.exception_index_table, 0,
175                     exception_index_table.length);
176         }
177         c.constant_pool = _constant_pool;
178         return c;
179     }
180 }
181
Popular Tags