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.

ExceptionsAttribute.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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.IOException;
  19. import java.util.Map;
  20. /**
  21. * <code>Exceptions_attribute</code>.
  22. */
  23. public class ExceptionsAttribute extends AttributeInfo {
  24. /**
  25. * The name of this attribute <code>"Exceptions"</code>.
  26. */
  27. public static final String tag = "Exceptions";
  28. ExceptionsAttribute(ConstPool cp, int n, DataInputStream in)
  29. throws IOException
  30. {
  31. super(cp, n, in);
  32. }
  33. /**
  34. * Constructs a copy of an exceptions attribute.
  35. *
  36. * @param cp constant pool table.
  37. * @param src source attribute.
  38. */
  39. private ExceptionsAttribute(ConstPool cp, ExceptionsAttribute src,
  40. Map classnames) {
  41. super(cp, tag);
  42. copyFrom(src, classnames);
  43. }
  44. /**
  45. * Constructs a new exceptions attribute.
  46. *
  47. * @param cp constant pool table.
  48. */
  49. public ExceptionsAttribute(ConstPool cp) {
  50. super(cp, tag);
  51. byte[] data = new byte[2];
  52. data[0] = data[1] = 0; // empty
  53. this.info = data;
  54. }
  55. /**
  56. * Makes a copy. Class names are replaced according to the
  57. * given <code>Map</code> object.
  58. *
  59. * @param newCp the constant pool table used by the new copy.
  60. * @param classnames pairs of replaced and substituted
  61. * class names. It can be <code>null</code>.
  62. */
  63. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  64. return new ExceptionsAttribute(newCp, this, classnames);
  65. }
  66. /**
  67. * Copies the contents from a source attribute.
  68. * Specified class names are replaced during the copy.
  69. *
  70. * @param srcAttr source Exceptions attribute
  71. * @param classnames pairs of replaced and substituted
  72. * class names.
  73. */
  74. private void copyFrom(ExceptionsAttribute srcAttr, Map classnames) {
  75. ConstPool srcCp = srcAttr.constPool;
  76. ConstPool destCp = this.constPool;
  77. byte[] src = srcAttr.info;
  78. int num = src.length;
  79. byte[] dest = new byte[num];
  80. dest[0] = src[0];
  81. dest[1] = src[1]; // the number of elements.
  82. for (int i = 2; i < num; i += 2) {
  83. int index = ByteArray.readU16bit(src, i);
  84. ByteArray.write16bit(srcCp.copy(index, destCp, classnames),
  85. dest, i);
  86. }
  87. this.info = dest;
  88. }
  89. /**
  90. * Returns <code>exception_index_table[]</code>.
  91. */
  92. public int[] getExceptionIndexes() {
  93. byte[] blist = info;
  94. int n = blist.length;
  95. if (n <= 2)
  96. return null;
  97. int[] elist = new int[n / 2 - 1];
  98. int k = 0;
  99. for (int j = 2; j < n; j += 2)
  100. elist[k++] = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff);
  101. return elist;
  102. }
  103. /**
  104. * Returns the names of exceptions that the method may throw.
  105. */
  106. public String[] getExceptions() {
  107. byte[] blist = info;
  108. int n = blist.length;
  109. if (n <= 2)
  110. return null;
  111. String[] elist = new String[n / 2 - 1];
  112. int k = 0;
  113. for (int j = 2; j < n; j += 2) {
  114. int index = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff);
  115. elist[k++] = constPool.getClassInfo(index);
  116. }
  117. return elist;
  118. }
  119. /**
  120. * Sets <code>exception_index_table[]</code>.
  121. */
  122. public void setExceptionIndexes(int[] elist) {
  123. int n = elist.length;
  124. byte[] blist = new byte[n * 2 + 2];
  125. ByteArray.write16bit(n, blist, 0);
  126. for (int i = 0; i < n; ++i)
  127. ByteArray.write16bit(elist[i], blist, i * 2 + 2);
  128. info = blist;
  129. }
  130. /**
  131. * Sets the names of exceptions that the method may throw.
  132. */
  133. public void setExceptions(String[] elist) {
  134. int n = elist.length;
  135. byte[] blist = new byte[n * 2 + 2];
  136. ByteArray.write16bit(n, blist, 0);
  137. for (int i = 0; i < n; ++i)
  138. ByteArray.write16bit(constPool.addClassInfo(elist[i]),
  139. blist, i * 2 + 2);
  140. info = blist;
  141. }
  142. /**
  143. * Returns <code>number_of_exceptions</code>.
  144. */
  145. public int tableLength() { return info.length / 2 - 1; }
  146. /**
  147. * Returns the value of <code>exception_index_table[nth]</code>.
  148. */
  149. public int getException(int nth) {
  150. int index = nth * 2 + 2; // nth >= 0
  151. return ((info[index] & 0xff) << 8) | (info[index + 1] & 0xff);
  152. }
  153. }