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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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<String,String> 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. @Override
  64. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  65. return new ExceptionsAttribute(newCp, this, classnames);
  66. }
  67. /**
  68. * Copies the contents from a source attribute.
  69. * Specified class names are replaced during the copy.
  70. *
  71. * @param srcAttr source Exceptions attribute
  72. * @param classnames pairs of replaced and substituted
  73. * class names.
  74. */
  75. private void copyFrom(ExceptionsAttribute srcAttr, Map<String,String> classnames) {
  76. ConstPool srcCp = srcAttr.constPool;
  77. ConstPool destCp = this.constPool;
  78. byte[] src = srcAttr.info;
  79. int num = src.length;
  80. byte[] dest = new byte[num];
  81. dest[0] = src[0];
  82. dest[1] = src[1]; // the number of elements.
  83. for (int i = 2; i < num; i += 2) {
  84. int index = ByteArray.readU16bit(src, i);
  85. ByteArray.write16bit(srcCp.copy(index, destCp, classnames),
  86. dest, i);
  87. }
  88. this.info = dest;
  89. }
  90. /**
  91. * Returns <code>exception_index_table[]</code>.
  92. */
  93. public int[] getExceptionIndexes() {
  94. byte[] blist = info;
  95. int n = blist.length;
  96. if (n <= 2)
  97. return null;
  98. int[] elist = new int[n / 2 - 1];
  99. int k = 0;
  100. for (int j = 2; j < n; j += 2)
  101. elist[k++] = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff);
  102. return elist;
  103. }
  104. /**
  105. * Returns the names of exceptions that the method may throw.
  106. */
  107. public String[] getExceptions() {
  108. byte[] blist = info;
  109. int n = blist.length;
  110. if (n <= 2)
  111. return null;
  112. String[] elist = new String[n / 2 - 1];
  113. int k = 0;
  114. for (int j = 2; j < n; j += 2) {
  115. int index = ((blist[j] & 0xff) << 8) | (blist[j + 1] & 0xff);
  116. elist[k++] = constPool.getClassInfo(index);
  117. }
  118. return elist;
  119. }
  120. /**
  121. * Sets <code>exception_index_table[]</code>.
  122. */
  123. public void setExceptionIndexes(int[] elist) {
  124. int n = elist.length;
  125. byte[] blist = new byte[n * 2 + 2];
  126. ByteArray.write16bit(n, blist, 0);
  127. for (int i = 0; i < n; ++i)
  128. ByteArray.write16bit(elist[i], blist, i * 2 + 2);
  129. info = blist;
  130. }
  131. /**
  132. * Sets the names of exceptions that the method may throw.
  133. */
  134. public void setExceptions(String[] elist) {
  135. int n = elist.length;
  136. byte[] blist = new byte[n * 2 + 2];
  137. ByteArray.write16bit(n, blist, 0);
  138. for (int i = 0; i < n; ++i)
  139. ByteArray.write16bit(constPool.addClassInfo(elist[i]),
  140. blist, i * 2 + 2);
  141. info = blist;
  142. }
  143. /**
  144. * Returns <code>number_of_exceptions</code>.
  145. */
  146. public int tableLength() { return info.length / 2 - 1; }
  147. /**
  148. * Returns the value of <code>exception_index_table[nth]</code>.
  149. */
  150. public int getException(int nth) {
  151. int index = nth * 2 + 2; // nth >= 0
  152. return ((info[index] & 0xff) << 8) | (info[index + 1] & 0xff);
  153. }
  154. }