KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > ExceptionTable


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.bytecode;
17
18 import java.io.DataInputStream JavaDoc;
19 import java.io.DataOutputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Map JavaDoc;
23
24 class ExceptionTableEntry {
25     int startPc;
26     int endPc;
27     int handlerPc;
28     int catchType;
29
30     ExceptionTableEntry(int start, int end, int handle, int type) {
31         startPc = start;
32         endPc = end;
33         handlerPc = handle;
34         catchType = type;
35     }
36 }
37
38 /**
39  * <code>exception_table[]</code> of <code>Code_attribute</code>.
40  */

41 public class ExceptionTable {
42     private ConstPool constPool;
43     private ArrayList JavaDoc entries;
44
45     /**
46      * Constructs an <code>exception_table[]</code>.
47      *
48      * @param cp constant pool table.
49      */

50     public ExceptionTable(ConstPool cp) {
51         constPool = cp;
52         entries = new ArrayList JavaDoc();
53     }
54
55     ExceptionTable(ConstPool cp, DataInputStream JavaDoc in) throws IOException JavaDoc {
56         constPool = cp;
57         int length = in.readUnsignedShort();
58         ArrayList JavaDoc list = new ArrayList JavaDoc(length);
59         for (int i = 0; i < length; ++i) {
60             int start = in.readUnsignedShort();
61             int end = in.readUnsignedShort();
62             int handle = in.readUnsignedShort();
63             int type = in.readUnsignedShort();
64             list.add(new ExceptionTableEntry(start, end, handle, type));
65         }
66
67         entries = list;
68     }
69
70     /**
71      * Returns <code>exception_table_length</code>, which is the number
72      * of entries in the <code>exception_table[]</code>.
73      */

74     public int size() {
75         return entries.size();
76     }
77
78     /**
79      * Returns <code>startPc</code> of the <i>n</i>-th entry.
80      *
81      * @param nth the <i>n</i>-th (&gt;= 0).
82      */

83     public int startPc(int nth) {
84         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
85         return e.startPc;
86     }
87
88     /**
89      * Sets <code>startPc</code> of the <i>n</i>-th entry.
90      *
91      * @param nth the <i>n</i>-th (&gt;= 0).
92      * @param value new value.
93      */

94     public void setStartPc(int nth, int value) {
95         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
96         e.startPc = value;
97     }
98
99     /**
100      * Returns <code>endPc</code> of the <i>n</i>-th entry.
101      *
102      * @param nth the <i>n</i>-th (&gt;= 0).
103      */

104     public int endPc(int nth) {
105         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
106         return e.endPc;
107     }
108
109     /**
110      * Sets <code>endPc</code> of the <i>n</i>-th entry.
111      *
112      * @param nth the <i>n</i>-th (&gt;= 0).
113      * @param value new value.
114      */

115     public void setEndPc(int nth, int value) {
116         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
117         e.endPc = value;
118     }
119
120     /**
121      * Returns <code>handlerPc</code> of the <i>n</i>-th entry.
122      *
123      * @param nth the <i>n</i>-th (&gt;= 0).
124      */

125     public int handlerPc(int nth) {
126         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
127         return e.handlerPc;
128     }
129
130     /**
131      * Sets <code>handlerPc</code> of the <i>n</i>-th entry.
132      *
133      * @param nth the <i>n</i>-th (&gt;= 0).
134      * @param value new value.
135      */

136     public void setHandlerPc(int nth, int value) {
137         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
138         e.handlerPc = value;
139     }
140
141     /**
142      * Returns <code>catchType</code> of the <i>n</i>-th entry.
143      *
144      * @param nth the <i>n</i>-th (&gt;= 0).
145      */

146     public int catchType(int nth) {
147         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
148         return e.catchType;
149     }
150
151     /**
152      * Sets <code>catchType</code> of the <i>n</i>-th entry.
153      *
154      * @param nth the <i>n</i>-th (&gt;= 0).
155      * @param value new value.
156      */

157     public void setCatchType(int nth, int value) {
158         ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
159         e.catchType = value;
160     }
161
162     /**
163      * Copies the given exception table at the specified position
164      * in the table.
165      *
166      * @param index index (&gt;= 0) at which the entry is to be inserted.
167      * @param offset the offset added to the code position.
168      */

169     public void add(int index, ExceptionTable table, int offset) {
170         int len = table.size();
171         while (--len >= 0) {
172             ExceptionTableEntry e
173                 = (ExceptionTableEntry)table.entries.get(len);
174             add(index, e.startPc + offset, e.endPc + offset,
175                 e.handlerPc + offset, e.catchType);
176         }
177     }
178
179     /**
180      * Adds a new entry at the specified position in the table.
181      *
182      * @param index index (&gt;= 0) at which the entry is to be inserted.
183      * @param start <code>startPc</code>
184      * @param end <code>endPc</code>
185      * @param handler <code>handlerPc</code>
186      * @param type <code>catchType</code>
187      */

188     public void add(int index, int start, int end, int handler, int type) {
189         if (start < end)
190             entries.add(index,
191                     new ExceptionTableEntry(start, end, handler, type));
192     }
193
194     /**
195      * Appends a new entry at the end of the table.
196      *
197      * @param start <code>startPc</code>
198      * @param end <code>endPc</code>
199      * @param handler <code>handlerPc</code>
200      * @param type <code>catchType</code>
201      */

202     public void add(int start, int end, int handler, int type) {
203         if (start < end)
204             entries.add(new ExceptionTableEntry(start, end, handler, type));
205     }
206
207     /**
208      * Removes the entry at the specified position in the table.
209      *
210      * @param index the index of the removed entry.
211      */

212     public void remove(int index) {
213         entries.remove(index);
214     }
215
216     /**
217      * Makes a copy of this <code>exception_table[]</code>.
218      * Class names are replaced according to the
219      * given <code>Map</code> object.
220      *
221      * @param newCp the constant pool table used by the new copy.
222      * @param classnames pairs of replaced and substituted
223      * class names.
224      */

225     public ExceptionTable copy(ConstPool newCp, Map JavaDoc classnames) {
226         ExceptionTable et = new ExceptionTable(newCp);
227         ConstPool srcCp = constPool;
228         int len = size();
229         for (int i = 0; i < len; ++i) {
230             ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
231             int type = srcCp.copy(e.catchType, newCp, classnames);
232             et.add(e.startPc, e.endPc, e.handlerPc, type);
233         }
234
235         return et;
236     }
237
238     void shiftPc(int where, int gapLength, boolean exclusive) {
239         int len = size();
240         for (int i = 0; i < len; ++i) {
241             ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
242             e.startPc = shiftPc(e.startPc, where, gapLength, exclusive);
243             e.endPc = shiftPc(e.endPc, where, gapLength, exclusive);
244             e.handlerPc = shiftPc(e.handlerPc, where, gapLength, exclusive);
245         }
246     }
247
248     private static int shiftPc(int pc, int where, int gapLength,
249                                boolean exclusive) {
250         if (pc > where || (exclusive && pc == where))
251             pc += gapLength;
252
253         return pc;
254     }
255
256     void write(DataOutputStream JavaDoc out) throws IOException JavaDoc {
257         int len = size();
258         out.writeShort(len); // exception_table_length
259
for (int i = 0; i < len; ++i) {
260             ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
261             out.writeShort(e.startPc);
262             out.writeShort(e.endPc);
263             out.writeShort(e.handlerPc);
264             out.writeShort(e.catchType);
265         }
266     }
267 }
268
Popular Tags