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.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  import org.apache.bcel.Constants;
24  
25  /** 
26   * This class represents the table of exceptions that are thrown by a
27   * method. This attribute may be used once per method.  The name of
28   * this class is <em>ExceptionTable</em> for historical reasons; The
29   * Java Virtual Machine Specification, Second Edition defines this
30   * attribute using the name <em>Exceptions</em> (which is inconsistent
31   * with the other classes).
32   *
33   * @version $Id: ExceptionTable.java 1152077 2011-07-29 02:29:42Z dbrosius $
34   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
35   * @see     Code
36   */
37  public final class ExceptionTable extends Attribute {
38  
39      private static final long serialVersionUID = 2045358830660883220L;
40      private int number_of_exceptions; // Table of indices into
41      private int[] exception_index_table; // constant pool
42  
43  
44      /**
45       * Initialize from another object. Note that both objects use the same
46       * references (shallow copy). Use copy() for a physical copy.
47       */
48      public ExceptionTable(ExceptionTable c) {
49          this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool());
50      }
51  
52  
53      /**
54       * @param name_index Index in constant pool
55       * @param length Content length in bytes
56       * @param exception_index_table Table of indices in constant pool
57       * @param constant_pool Array of constants
58       */
59      public ExceptionTable(int name_index, int length, int[] exception_index_table,
60              ConstantPool constant_pool) {
61          super(Constants.ATTR_EXCEPTIONS, name_index, length, constant_pool);
62          setExceptionIndexTable(exception_index_table);
63      }
64  
65  
66      /**
67       * Construct object from file stream.
68       * @param name_index Index in constant pool
69       * @param length Content length in bytes
70       * @param file Input stream
71       * @param constant_pool Array of constants
72       * @throws IOException
73       */
74      ExceptionTable(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
75              throws IOException {
76          this(name_index, length, (int[]) null, constant_pool);
77          number_of_exceptions = file.readUnsignedShort();
78          exception_index_table = new int[number_of_exceptions];
79          for (int i = 0; i < number_of_exceptions; i++) {
80              exception_index_table[i] = file.readUnsignedShort();
81          }
82      }
83  
84  
85      /**
86       * Called by objects that are traversing the nodes of the tree implicitely
87       * defined by the contents of a Java class. I.e., the hierarchy of methods,
88       * fields, attributes, etc. spawns a tree of objects.
89       *
90       * @param v Visitor object
91       */
92      @Override
93      public void accept( Visitor v ) {
94          v.visitExceptionTable(this);
95      }
96  
97  
98      /**
99       * Dump exceptions attribute to file stream in binary format.
100      *
101      * @param file Output file stream
102      * @throws IOException
103      */
104     @Override
105     public final void dump( DataOutputStream file ) throws IOException {
106         super.dump(file);
107         file.writeShort(number_of_exceptions);
108         for (int i = 0; i < number_of_exceptions; i++) {
109             file.writeShort(exception_index_table[i]);
110         }
111     }
112 
113 
114     /**
115      * @return Array of indices into constant pool of thrown exceptions.
116      */
117     public final int[] getExceptionIndexTable() {
118         return exception_index_table;
119     }
120 
121 
122     /**
123      * @return Length of exception table.
124      */
125     public final int getNumberOfExceptions() {
126         return number_of_exceptions;
127     }
128 
129 
130     /**
131      * @return class names of thrown exceptions
132      */
133     public final String[] getExceptionNames() {
134         String[] names = new String[number_of_exceptions];
135         for (int i = 0; i < number_of_exceptions; i++) {
136             names[i] = constant_pool.getConstantString(exception_index_table[i],
137                     Constants.CONSTANT_Class).replace('/', '.');
138         }
139         return names;
140     }
141 
142 
143     /**
144      * @param exception_index_table the list of exception indexes
145      * Also redefines number_of_exceptions according to table length.
146      */
147     public final void setExceptionIndexTable( int[] exception_index_table ) {
148         this.exception_index_table = exception_index_table;
149         number_of_exceptions = (exception_index_table == null) ? 0 : exception_index_table.length;
150     }
151 
152 
153     /**
154      * @return String representation, i.e., a list of thrown exceptions.
155      */
156     @Override
157     public final String toString() {
158         StringBuilder buf = new StringBuilder();
159         String str;
160         for (int i = 0; i < number_of_exceptions; i++) {
161             str = constant_pool.getConstantString(exception_index_table[i],
162                     Constants.CONSTANT_Class);
163             buf.append(Utility.compactClassName(str, false));
164             if (i < number_of_exceptions - 1) {
165                 buf.append(", ");
166             }
167         }
168         return buf.toString();
169     }
170 
171 
172     /**
173      * @return deep copy of this attribute
174      */
175     @Override
176     public Attribute copy( ConstantPool _constant_pool ) {
177         ExceptionTable c = (ExceptionTable) clone();
178         if (exception_index_table != null) {
179             c.exception_index_table = new int[exception_index_table.length];
180             System.arraycopy(exception_index_table, 0, c.exception_index_table, 0,
181                     exception_index_table.length);
182         }
183         c.constant_pool = _constant_pool;
184         return c;
185     }
186 }