1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.classfile;
21
22 import java.io.DataInput;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import java.util.Arrays;
26
27 import org.apache.bcel.Const;
28 import org.apache.bcel.util.Args;
29 import org.apache.commons.lang3.ArrayUtils;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public final class ExceptionTable extends Attribute {
48
49 private int[] exceptionIndexTable;
50
51
52
53
54
55
56
57 public ExceptionTable(final ExceptionTable c) {
58 this(c.getNameIndex(), c.getLength(), c.getExceptionIndexTable(), c.getConstantPool());
59 }
60
61
62
63
64
65
66
67
68
69
70 ExceptionTable(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
71 this(nameIndex, length, (int[]) null, constantPool);
72 final int exceptionCount = input.readUnsignedShort();
73 exceptionIndexTable = new int[exceptionCount];
74 for (int i = 0; i < exceptionCount; i++) {
75 exceptionIndexTable[i] = input.readUnsignedShort();
76 }
77 }
78
79
80
81
82
83
84
85 public ExceptionTable(final int nameIndex, final int length, final int[] exceptionIndexTable, final ConstantPool constantPool) {
86 super(Const.ATTR_EXCEPTIONS, nameIndex, length, constantPool);
87 this.exceptionIndexTable = ArrayUtils.nullToEmpty(exceptionIndexTable);
88 Args.requireU2(this.exceptionIndexTable.length, "exceptionIndexTable.length");
89 }
90
91
92
93
94
95
96
97 @Override
98 public void accept(final Visitor v) {
99 v.visitExceptionTable(this);
100 }
101
102
103
104
105 @Override
106 public Attribute copy(final ConstantPool constantPool) {
107 final ExceptionTable c = (ExceptionTable) clone();
108 if (exceptionIndexTable != null) {
109 c.exceptionIndexTable = exceptionIndexTable.clone();
110 }
111 c.setConstantPool(constantPool);
112 return c;
113 }
114
115
116
117
118
119
120
121 @Override
122 public void dump(final DataOutputStream file) throws IOException {
123 super.dump(file);
124 file.writeShort(exceptionIndexTable.length);
125 for (final int index : exceptionIndexTable) {
126 file.writeShort(index);
127 }
128 }
129
130
131
132
133 public int[] getExceptionIndexTable() {
134 return exceptionIndexTable;
135 }
136
137
138
139
140 public String[] getExceptionNames() {
141 final String[] names = new String[exceptionIndexTable.length];
142 Arrays.setAll(names, i -> Utility.pathToPackage(super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class)));
143 return names;
144 }
145
146
147
148
149 public int getNumberOfExceptions() {
150 return exceptionIndexTable == null ? 0 : exceptionIndexTable.length;
151 }
152
153
154
155
156
157 public void setExceptionIndexTable(final int[] exceptionIndexTable) {
158 this.exceptionIndexTable = ArrayUtils.nullToEmpty(exceptionIndexTable);
159 }
160
161
162
163
164 @Override
165 public String toString() {
166 final StringBuilder buf = new StringBuilder();
167 String str;
168 buf.append("Exceptions: ");
169 for (int i = 0; i < exceptionIndexTable.length; i++) {
170 str = super.getConstantPool().getConstantString(exceptionIndexTable[i], Const.CONSTANT_Class);
171 buf.append(Utility.compactClassName(str, false));
172 if (i < exceptionIndexTable.length - 1) {
173 buf.append(", ");
174 }
175 }
176 return buf.toString();
177 }
178 }