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.2KB

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