You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExceptionTable.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode;
  17. import java.io.DataInputStream;
  18. import java.io.DataOutputStream;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Map;
  22. class ExceptionTableEntry {
  23. int startPc;
  24. int endPc;
  25. int handlerPc;
  26. int catchType;
  27. ExceptionTableEntry(int start, int end, int handle, int type) {
  28. startPc = start;
  29. endPc = end;
  30. handlerPc = handle;
  31. catchType = type;
  32. }
  33. }
  34. /**
  35. * <code>exception_table[]</code> of <code>Code_attribute</code>.
  36. */
  37. public class ExceptionTable implements Cloneable {
  38. private ConstPool constPool;
  39. private ArrayList entries;
  40. /**
  41. * Constructs an <code>exception_table[]</code>.
  42. *
  43. * @param cp constant pool table.
  44. */
  45. public ExceptionTable(ConstPool cp) {
  46. constPool = cp;
  47. entries = new ArrayList();
  48. }
  49. ExceptionTable(ConstPool cp, DataInputStream in) throws IOException {
  50. constPool = cp;
  51. int length = in.readUnsignedShort();
  52. ArrayList list = new ArrayList(length);
  53. for (int i = 0; i < length; ++i) {
  54. int start = in.readUnsignedShort();
  55. int end = in.readUnsignedShort();
  56. int handle = in.readUnsignedShort();
  57. int type = in.readUnsignedShort();
  58. list.add(new ExceptionTableEntry(start, end, handle, type));
  59. }
  60. entries = list;
  61. }
  62. /**
  63. * Creates and returns a copy of this object.
  64. * The constant pool object is shared between this object
  65. * and the cloned object.
  66. */
  67. public Object clone() throws CloneNotSupportedException {
  68. ExceptionTable r = (ExceptionTable)super.clone();
  69. r.entries = new ArrayList(entries);
  70. return r;
  71. }
  72. /**
  73. * Returns <code>exception_table_length</code>, which is the number
  74. * of entries in the <code>exception_table[]</code>.
  75. */
  76. public int size() {
  77. return entries.size();
  78. }
  79. /**
  80. * Returns <code>startPc</code> of the <i>n</i>-th entry.
  81. *
  82. * @param nth the <i>n</i>-th (&gt;= 0).
  83. */
  84. public int startPc(int nth) {
  85. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  86. return e.startPc;
  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. * Returns <code>endPc</code> of the <i>n</i>-th entry.
  100. *
  101. * @param nth the <i>n</i>-th (&gt;= 0).
  102. */
  103. public int endPc(int nth) {
  104. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  105. return e.endPc;
  106. }
  107. /**
  108. * Sets <code>endPc</code> of the <i>n</i>-th entry.
  109. *
  110. * @param nth the <i>n</i>-th (&gt;= 0).
  111. * @param value new value.
  112. */
  113. public void setEndPc(int nth, int value) {
  114. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  115. e.endPc = value;
  116. }
  117. /**
  118. * Returns <code>handlerPc</code> of the <i>n</i>-th entry.
  119. *
  120. * @param nth the <i>n</i>-th (&gt;= 0).
  121. */
  122. public int handlerPc(int nth) {
  123. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  124. return e.handlerPc;
  125. }
  126. /**
  127. * Sets <code>handlerPc</code> of the <i>n</i>-th entry.
  128. *
  129. * @param nth the <i>n</i>-th (&gt;= 0).
  130. * @param value new value.
  131. */
  132. public void setHandlerPc(int nth, int value) {
  133. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  134. e.handlerPc = value;
  135. }
  136. /**
  137. * Returns <code>catchType</code> of the <i>n</i>-th entry.
  138. *
  139. * @param nth the <i>n</i>-th (&gt;= 0).
  140. * @return an index into the <code>constant_pool</code> table,
  141. * or zero if this exception handler is for all exceptions.
  142. */
  143. public int catchType(int nth) {
  144. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  145. return e.catchType;
  146. }
  147. /**
  148. * Sets <code>catchType</code> of the <i>n</i>-th entry.
  149. *
  150. * @param nth the <i>n</i>-th (&gt;= 0).
  151. * @param value new value.
  152. */
  153. public void setCatchType(int nth, int value) {
  154. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(nth);
  155. e.catchType = value;
  156. }
  157. /**
  158. * Copies the given exception table at the specified position
  159. * in the table.
  160. *
  161. * @param index index (&gt;= 0) at which the entry is to be inserted.
  162. * @param offset the offset added to the code position.
  163. */
  164. public void add(int index, ExceptionTable table, int offset) {
  165. int len = table.size();
  166. while (--len >= 0) {
  167. ExceptionTableEntry e
  168. = (ExceptionTableEntry)table.entries.get(len);
  169. add(index, e.startPc + offset, e.endPc + offset,
  170. e.handlerPc + offset, e.catchType);
  171. }
  172. }
  173. /**
  174. * Adds a new entry at the specified position in the table.
  175. *
  176. * @param index index (&gt;= 0) at which the entry is to be inserted.
  177. * @param start <code>startPc</code>
  178. * @param end <code>endPc</code>
  179. * @param handler <code>handlerPc</code>
  180. * @param type <code>catchType</code>
  181. */
  182. public void add(int index, int start, int end, int handler, int type) {
  183. if (start < end)
  184. entries.add(index,
  185. new ExceptionTableEntry(start, end, handler, type));
  186. }
  187. /**
  188. * Appends a new entry at the end of the table.
  189. *
  190. * @param start <code>startPc</code>
  191. * @param end <code>endPc</code>
  192. * @param handler <code>handlerPc</code>
  193. * @param type <code>catchType</code>
  194. */
  195. public void add(int start, int end, int handler, int type) {
  196. if (start < end)
  197. entries.add(new ExceptionTableEntry(start, end, handler, type));
  198. }
  199. /**
  200. * Removes the entry at the specified position in the table.
  201. *
  202. * @param index the index of the removed entry.
  203. */
  204. public void remove(int index) {
  205. entries.remove(index);
  206. }
  207. /**
  208. * Makes a copy of this <code>exception_table[]</code>.
  209. * Class names are replaced according to the
  210. * given <code>Map</code> object.
  211. *
  212. * @param newCp the constant pool table used by the new copy.
  213. * @param classnames pairs of replaced and substituted
  214. * class names.
  215. */
  216. public ExceptionTable copy(ConstPool newCp, Map classnames) {
  217. ExceptionTable et = new ExceptionTable(newCp);
  218. ConstPool srcCp = constPool;
  219. int len = size();
  220. for (int i = 0; i < len; ++i) {
  221. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
  222. int type = srcCp.copy(e.catchType, newCp, classnames);
  223. et.add(e.startPc, e.endPc, e.handlerPc, type);
  224. }
  225. return et;
  226. }
  227. void shiftPc(int where, int gapLength, boolean exclusive) {
  228. int len = size();
  229. for (int i = 0; i < len; ++i) {
  230. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
  231. e.startPc = shiftPc(e.startPc, where, gapLength, exclusive);
  232. e.endPc = shiftPc(e.endPc, where, gapLength, exclusive);
  233. e.handlerPc = shiftPc(e.handlerPc, where, gapLength, exclusive);
  234. }
  235. }
  236. private static int shiftPc(int pc, int where, int gapLength,
  237. boolean exclusive) {
  238. if (pc > where || (exclusive && pc == where))
  239. pc += gapLength;
  240. return pc;
  241. }
  242. void write(DataOutputStream out) throws IOException {
  243. int len = size();
  244. out.writeShort(len); // exception_table_length
  245. for (int i = 0; i < len; ++i) {
  246. ExceptionTableEntry e = (ExceptionTableEntry)entries.get(i);
  247. out.writeShort(e.startPc);
  248. out.writeShort(e.endPc);
  249. out.writeShort(e.handlerPc);
  250. out.writeShort(e.catchType);
  251. }
  252. }
  253. }