1 15 16 package javassist.bytecode; 17 18 import java.io.DataInputStream ; 19 import java.io.DataOutputStream ; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Map ; 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 41 public class ExceptionTable { 42 private ConstPool constPool; 43 private ArrayList entries; 44 45 50 public ExceptionTable(ConstPool cp) { 51 constPool = cp; 52 entries = new ArrayList (); 53 } 54 55 ExceptionTable(ConstPool cp, DataInputStream in) throws IOException { 56 constPool = cp; 57 int length = in.readUnsignedShort(); 58 ArrayList list = new ArrayList (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 74 public int size() { 75 return entries.size(); 76 } 77 78 83 public int startPc(int nth) { 84 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 85 return e.startPc; 86 } 87 88 94 public void setStartPc(int nth, int value) { 95 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 96 e.startPc = value; 97 } 98 99 104 public int endPc(int nth) { 105 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 106 return e.endPc; 107 } 108 109 115 public void setEndPc(int nth, int value) { 116 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 117 e.endPc = value; 118 } 119 120 125 public int handlerPc(int nth) { 126 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 127 return e.handlerPc; 128 } 129 130 136 public void setHandlerPc(int nth, int value) { 137 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 138 e.handlerPc = value; 139 } 140 141 146 public int catchType(int nth) { 147 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 148 return e.catchType; 149 } 150 151 157 public void setCatchType(int nth, int value) { 158 ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth); 159 e.catchType = value; 160 } 161 162 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 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 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 212 public void remove(int index) { 213 entries.remove(index); 214 } 215 216 225 public ExceptionTable copy(ConstPool newCp, Map 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 out) throws IOException { 257 int len = size(); 258 out.writeShort(len); 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 |