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