KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > classfile > ExceptionTable


1 package com.sun.org.apache.bcel.internal.classfile;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.Constants;
58 import java.io.*;
59
60 /**
61  * This class represents the table of exceptions that are thrown by a
62  * method. This attribute may be used once per method. The name of
63  * this class is <em>ExceptionTable</em> for historical reasons; The
64  * Java Virtual Machine Specification, Second Edition defines this
65  * attribute using the name <em>Exceptions</em> (which is inconsistent
66  * with the other classes).
67  *
68  * @version $Id: ExceptionTable.java,v 1.1.1.1 2001/10/29 20:00:00 jvanzyl Exp $
69  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
70  * @see Code
71  */

72 public final class ExceptionTable extends Attribute {
73   private int number_of_exceptions; // Table of indices into
74
private int[] exception_index_table; // constant pool
75

76   /**
77    * Initialize from another object. Note that both objects use the same
78    * references (shallow copy). Use copy() for a physical copy.
79    */

80   public ExceptionTable(ExceptionTable c) {
81     this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(),
82      c.getConstantPool());
83   }
84
85   /**
86    * @param name_index Index in constant pool
87    * @param length Content length in bytes
88    * @param exception_index_table Table of indices in constant pool
89    * @param constant_pool Array of constants
90    */

91   public ExceptionTable(int name_index, int length,
92             int[] exception_index_table,
93             ConstantPool constant_pool)
94   {
95     super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool);
96     setExceptionIndexTable(exception_index_table);
97   }
98
99   /**
100    * Construct object from file stream.
101    * @param name_index Index in constant pool
102    * @param length Content length in bytes
103    * @param file Input stream
104    * @param constant_pool Array of constants
105    * @throw IOException
106    */

107   ExceptionTable(int name_index, int length, DataInputStream file,
108          ConstantPool constant_pool) throws IOException
109   {
110     this(name_index, length, (int[])null, constant_pool);
111
112     number_of_exceptions = file.readUnsignedShort();
113     exception_index_table = new int[number_of_exceptions];
114
115     for(int i=0; i < number_of_exceptions; i++)
116       exception_index_table[i] = file.readUnsignedShort();
117   }
118
119   /**
120    * Called by objects that are traversing the nodes of the tree implicitely
121    * defined by the contents of a Java class. I.e., the hierarchy of methods,
122    * fields, attributes, etc. spawns a tree of objects.
123    *
124    * @param v Visitor object
125    */

126   public void accept(Visitor v) {
127     v.visitExceptionTable(this);
128   }
129
130   /**
131    * Dump exceptions attribute to file stream in binary format.
132    *
133    * @param file Output file stream
134    * @throw IOException
135    */

136   public final void dump(DataOutputStream file) throws IOException
137   {
138     super.dump(file);
139     file.writeShort(number_of_exceptions);
140     for(int i=0; i < number_of_exceptions; i++)
141       file.writeShort(exception_index_table[i]);
142   }
143
144   /**
145    * @return Array of indices into constant pool of thrown exceptions.
146    */

147   public final int[] getExceptionIndexTable() {return exception_index_table;}
148   /**
149    * @return Length of exception table.
150    */

151   public final int getNumberOfExceptions() { return number_of_exceptions; }
152
153   /**
154    * @return class names of thrown exceptions
155    */

156   public final String JavaDoc[] getExceptionNames() {
157     String JavaDoc[] names = new String JavaDoc[number_of_exceptions];
158     for(int i=0; i < number_of_exceptions; i++)
159       names[i] = constant_pool.getConstantString(exception_index_table[i],
160                          Constants.CONSTANT_Class).
161     replace('/', '.');
162     return names;
163   }
164
165   /**
166    * @param exception_index_table.
167    * Also redefines number_of_exceptions according to table length.
168    */

169   public final void setExceptionIndexTable(int[] exception_index_table) {
170     this.exception_index_table = exception_index_table;
171     number_of_exceptions = (exception_index_table == null)? 0 :
172       exception_index_table.length;
173   }
174   /**
175    * @return String representation, i.e., a list of thrown exceptions.
176    */

177   public final String JavaDoc toString() {
178     StringBuffer JavaDoc buf = new StringBuffer JavaDoc("");
179     String JavaDoc str;
180
181     for(int i=0; i < number_of_exceptions; i++) {
182       str = constant_pool.getConstantString(exception_index_table[i],
183                         Constants.CONSTANT_Class);
184       buf.append(Utility.compactClassName(str, false));
185
186       if(i < number_of_exceptions - 1)
187     buf.append(", ");
188     }
189
190     return buf.toString();
191   }
192
193   /**
194    * @return deep copy of this attribute
195    */

196   public Attribute copy(ConstantPool constant_pool) {
197     ExceptionTable c = (ExceptionTable)clone();
198     c.exception_index_table = (int[])exception_index_table.clone();
199     c.constant_pool = constant_pool;
200     return c;
201   }
202 }
203
Popular Tags