Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ExceptionsAttribute.java 5.1KB

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