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