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.

InnerClassesAttribute.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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.util.Map;
  18. import java.io.IOException;
  19. /**
  20. * <code>InnerClasses_attribute</code>.
  21. */
  22. public class InnerClassesAttribute extends AttributeInfo {
  23. /**
  24. * The name of this attribute <code>"InnerClasses"</code>.
  25. */
  26. public static final String tag = "InnerClasses";
  27. InnerClassesAttribute(ConstPool cp, int n, DataInputStream in)
  28. throws IOException
  29. {
  30. super(cp, n, in);
  31. }
  32. private InnerClassesAttribute(ConstPool cp, byte[] info) {
  33. super(cp, tag, info);
  34. }
  35. /**
  36. * Constructs an empty InnerClasses attribute.
  37. *
  38. * @see #append(String, String, String, int)
  39. */
  40. public InnerClassesAttribute(ConstPool cp) {
  41. super(cp, tag, new byte[2]);
  42. ByteArray.write16bit(0, get(), 0);
  43. }
  44. /**
  45. * Returns <code>number_of_classes</code>.
  46. */
  47. public int tableLength() { return ByteArray.readU16bit(get(), 0); }
  48. /**
  49. * Returns <code>classes[nth].inner_class_info_index</code>.
  50. */
  51. public int innerClassIndex(int nth) {
  52. return ByteArray.readU16bit(get(), nth * 8 + 2);
  53. }
  54. /**
  55. * Returns the class name indicated
  56. * by <code>classes[nth].inner_class_info_index</code>.
  57. *
  58. * @return null or the class name.
  59. */
  60. public String innerClass(int nth) {
  61. int i = innerClassIndex(nth);
  62. if (i == 0)
  63. return null;
  64. else
  65. return constPool.getClassInfo(i);
  66. }
  67. /**
  68. * Sets <code>classes[nth].inner_class_info_index</code> to
  69. * the given index.
  70. */
  71. public void setInnerClassIndex(int nth, int index) {
  72. ByteArray.write16bit(index, get(), nth * 8 + 2);
  73. }
  74. /**
  75. * Returns <code>classes[nth].outer_class_info_index</code>.
  76. */
  77. public int outerClassIndex(int nth) {
  78. return ByteArray.readU16bit(get(), nth * 8 + 4);
  79. }
  80. /**
  81. * Returns the class name indicated
  82. * by <code>classes[nth].outer_class_info_index</code>.
  83. *
  84. * @return null or the class name.
  85. */
  86. public String outerClass(int nth) {
  87. int i = outerClassIndex(nth);
  88. if (i == 0)
  89. return null;
  90. else
  91. return constPool.getClassInfo(i);
  92. }
  93. /**
  94. * Sets <code>classes[nth].outer_class_info_index</code> to
  95. * the given index.
  96. */
  97. public void setOuterClassIndex(int nth, int index) {
  98. ByteArray.write16bit(index, get(), nth * 8 + 4);
  99. }
  100. /**
  101. * Returns <code>classes[nth].inner_name_index</code>.
  102. */
  103. public int innerNameIndex(int nth) {
  104. return ByteArray.readU16bit(get(), nth * 8 + 6);
  105. }
  106. /**
  107. * Returns the simple class name indicated
  108. * by <code>classes[nth].inner_name_index</code>.
  109. *
  110. * @return null or the class name.
  111. */
  112. public String innerName(int nth) {
  113. int i = innerNameIndex(nth);
  114. if (i == 0)
  115. return null;
  116. else
  117. return constPool.getUtf8Info(i);
  118. }
  119. /**
  120. * Sets <code>classes[nth].inner_name_index</code> to
  121. * the given index.
  122. */
  123. public void setInnerNameIndex(int nth, int index) {
  124. ByteArray.write16bit(index, get(), nth * 8 + 6);
  125. }
  126. /**
  127. * Returns <code>classes[nth].inner_class_access_flags</code>.
  128. */
  129. public int accessFlags(int nth) {
  130. return ByteArray.readU16bit(get(), nth * 8 + 8);
  131. }
  132. /**
  133. * Sets <code>classes[nth].inner_class_access_flags</code> to
  134. * the given index.
  135. */
  136. public void setAccessFlags(int nth, int flags) {
  137. ByteArray.write16bit(flags, get(), nth * 8 + 8);
  138. }
  139. /**
  140. * Appends a new entry.
  141. *
  142. * @param inner <code>inner_class_info_index</code>
  143. * @param outer <code>outer_class_info_index</code>
  144. * @param name <code>inner_name_index</code>
  145. * @param flags <code>inner_class_access_flags</code>
  146. */
  147. public void append(String inner, String outer, String name, int flags) {
  148. int i = constPool.addClassInfo(inner);
  149. int o = constPool.addClassInfo(outer);
  150. int n = constPool.addUtf8Info(name);
  151. append(i, o, n, flags);
  152. }
  153. /**
  154. * Appends a new entry.
  155. *
  156. * @param inner <code>inner_class_info_index</code>
  157. * @param outer <code>outer_class_info_index</code>
  158. * @param name <code>inner_name_index</code>
  159. * @param flags <code>inner_class_access_flags</code>
  160. */
  161. public void append(int inner, int outer, int name, int flags) {
  162. byte[] data = get();
  163. int len = data.length;
  164. byte[] newData = new byte[len + 8];
  165. for (int i = 2; i < len; ++i)
  166. newData[i] = data[i];
  167. int n = ByteArray.readU16bit(data, 0);
  168. ByteArray.write16bit(n + 1, newData, 0);
  169. ByteArray.write16bit(inner, newData, len);
  170. ByteArray.write16bit(outer, newData, len + 2);
  171. ByteArray.write16bit(name, newData, len + 4);
  172. ByteArray.write16bit(flags, newData, len + 6);
  173. set(newData);
  174. }
  175. /**
  176. * Makes a copy. Class names are replaced according to the
  177. * given <code>Map</code> object.
  178. *
  179. * @param newCp the constant pool table used by the new copy.
  180. * @param classnames pairs of replaced and substituted
  181. * class names.
  182. */
  183. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  184. byte[] src = get();
  185. byte[] dest = new byte[src.length];
  186. ConstPool cp = getConstPool();
  187. InnerClassesAttribute attr = new InnerClassesAttribute(newCp, dest);
  188. int n = ByteArray.readU16bit(src, 0);
  189. ByteArray.write16bit(n, dest, 0);
  190. int j = 2;
  191. for (int i = 0; i < n; ++i) {
  192. int innerClass = ByteArray.readU16bit(src, j);
  193. int outerClass = ByteArray.readU16bit(src, j + 2);
  194. int innerName = ByteArray.readU16bit(src, j + 4);
  195. int innerAccess = ByteArray.readU16bit(src, j + 6);
  196. if (innerClass != 0)
  197. innerClass = cp.copy(innerClass, newCp, classnames);
  198. ByteArray.write16bit(innerClass, dest, j);
  199. if (outerClass != 0)
  200. outerClass = cp.copy(outerClass, newCp, classnames);
  201. ByteArray.write16bit(outerClass, dest, j + 2);
  202. if (innerName != 0)
  203. innerName = cp.copy(innerName, newCp, classnames);
  204. ByteArray.write16bit(innerName, dest, j + 4);
  205. ByteArray.write16bit(innerAccess, dest, j + 6);
  206. j += 8;
  207. }
  208. return attr;
  209. }
  210. }