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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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>InnerClasses_attribute</code>.
  22. */
  23. public class InnerClassesAttribute extends AttributeInfo {
  24. /**
  25. * The name of this attribute <code>"InnerClasses"</code>.
  26. */
  27. public static final String tag = "InnerClasses";
  28. InnerClassesAttribute(ConstPool cp, int n, DataInputStream in)
  29. throws IOException
  30. {
  31. super(cp, n, in);
  32. }
  33. private InnerClassesAttribute(ConstPool cp, byte[] info) {
  34. super(cp, tag, info);
  35. }
  36. /**
  37. * Constructs an empty InnerClasses attribute.
  38. *
  39. * @see #append(String, String, String, int)
  40. */
  41. public InnerClassesAttribute(ConstPool cp) {
  42. super(cp, tag, new byte[2]);
  43. ByteArray.write16bit(0, get(), 0);
  44. }
  45. /**
  46. * Returns <code>number_of_classes</code>.
  47. */
  48. public int tableLength() { return ByteArray.readU16bit(get(), 0); }
  49. /**
  50. * Returns <code>classes[nth].inner_class_info_index</code>.
  51. */
  52. public int innerClassIndex(int nth) {
  53. return ByteArray.readU16bit(get(), nth * 8 + 2);
  54. }
  55. /**
  56. * Returns the class name indicated
  57. * by <code>classes[nth].inner_class_info_index</code>.
  58. * The class name is fully-qualified and separated by dot.
  59. *
  60. * @return null or the class name.
  61. * @see ConstPool#getClassInfo(int)
  62. */
  63. public String innerClass(int nth) {
  64. int i = innerClassIndex(nth);
  65. if (i == 0)
  66. return null;
  67. return constPool.getClassInfo(i);
  68. }
  69. /**
  70. * Sets <code>classes[nth].inner_class_info_index</code> to
  71. * the given index.
  72. */
  73. public void setInnerClassIndex(int nth, int index) {
  74. ByteArray.write16bit(index, get(), nth * 8 + 2);
  75. }
  76. /**
  77. * Returns <code>classes[nth].outer_class_info_index</code>.
  78. */
  79. public int outerClassIndex(int nth) {
  80. return ByteArray.readU16bit(get(), nth * 8 + 4);
  81. }
  82. /**
  83. * Returns the class name indicated
  84. * by <code>classes[nth].outer_class_info_index</code>.
  85. *
  86. * @return null or the class name.
  87. */
  88. public String outerClass(int nth) {
  89. int i = outerClassIndex(nth);
  90. if (i == 0)
  91. return null;
  92. return constPool.getClassInfo(i);
  93. }
  94. /**
  95. * Sets <code>classes[nth].outer_class_info_index</code> to
  96. * the given index.
  97. */
  98. public void setOuterClassIndex(int nth, int index) {
  99. ByteArray.write16bit(index, get(), nth * 8 + 4);
  100. }
  101. /**
  102. * Returns <code>classes[nth].inner_name_index</code>.
  103. */
  104. public int innerNameIndex(int nth) {
  105. return ByteArray.readU16bit(get(), nth * 8 + 6);
  106. }
  107. /**
  108. * Returns the simple class name indicated
  109. * by <code>classes[nth].inner_name_index</code>.
  110. *
  111. * @return null or the class name.
  112. */
  113. public String innerName(int nth) {
  114. int i = innerNameIndex(nth);
  115. if (i == 0)
  116. return null;
  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. * Finds the entry for the given inner class.
  141. *
  142. * @param name the fully-qualified class name separated by dot and $.
  143. * @return the index or -1 if not found.
  144. * @since 3.22
  145. */
  146. public int find(String name) {
  147. int n = tableLength();
  148. for (int i = 0; i < n; i++)
  149. if (name.equals(innerClass(i)))
  150. return i;
  151. return -1;
  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(String inner, String outer, String name, int flags) {
  162. int i = constPool.addClassInfo(inner);
  163. int o = constPool.addClassInfo(outer);
  164. int n = constPool.addUtf8Info(name);
  165. append(i, o, n, flags);
  166. }
  167. /**
  168. * Appends a new entry.
  169. *
  170. * @param inner <code>inner_class_info_index</code>
  171. * @param outer <code>outer_class_info_index</code>
  172. * @param name <code>inner_name_index</code>
  173. * @param flags <code>inner_class_access_flags</code>
  174. */
  175. public void append(int inner, int outer, int name, int flags) {
  176. byte[] data = get();
  177. int len = data.length;
  178. byte[] newData = new byte[len + 8];
  179. for (int i = 2; i < len; ++i)
  180. newData[i] = data[i];
  181. int n = ByteArray.readU16bit(data, 0);
  182. ByteArray.write16bit(n + 1, newData, 0);
  183. ByteArray.write16bit(inner, newData, len);
  184. ByteArray.write16bit(outer, newData, len + 2);
  185. ByteArray.write16bit(name, newData, len + 4);
  186. ByteArray.write16bit(flags, newData, len + 6);
  187. set(newData);
  188. }
  189. /**
  190. * Removes the {@code nth} entry. It does not eliminate
  191. * constant pool items that the removed entry refers to.
  192. * {@link ClassFile#compact()} should be executed to remove
  193. * these unnecessary items.
  194. *
  195. * @param nth 0, 1, 2, ...
  196. * @return the number of items after the removal.
  197. * @see ClassFile#compact()
  198. */
  199. public int remove(int nth) {
  200. byte[] data = get();
  201. int len = data.length;
  202. if (len < 10)
  203. return 0;
  204. int n = ByteArray.readU16bit(data, 0);
  205. int nthPos = 2 + nth * 8;
  206. if (n <= nth)
  207. return n;
  208. byte[] newData = new byte[len - 8];
  209. ByteArray.write16bit(n - 1, newData, 0);
  210. int i = 2, j = 2;
  211. while (i < len)
  212. if (i == nthPos)
  213. i += 8;
  214. else
  215. newData[j++] = data[i++];
  216. set(newData);
  217. return n - 1;
  218. }
  219. /**
  220. * Makes a copy. Class names are replaced according to the
  221. * given <code>Map</code> object.
  222. *
  223. * @param newCp the constant pool table used by the new copy.
  224. * @param classnames pairs of replaced and substituted
  225. * class names.
  226. */
  227. @Override
  228. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  229. byte[] src = get();
  230. byte[] dest = new byte[src.length];
  231. ConstPool cp = getConstPool();
  232. InnerClassesAttribute attr = new InnerClassesAttribute(newCp, dest);
  233. int n = ByteArray.readU16bit(src, 0);
  234. ByteArray.write16bit(n, dest, 0);
  235. int j = 2;
  236. for (int i = 0; i < n; ++i) {
  237. int innerClass = ByteArray.readU16bit(src, j);
  238. int outerClass = ByteArray.readU16bit(src, j + 2);
  239. int innerName = ByteArray.readU16bit(src, j + 4);
  240. int innerAccess = ByteArray.readU16bit(src, j + 6);
  241. if (innerClass != 0)
  242. innerClass = cp.copy(innerClass, newCp, classnames);
  243. ByteArray.write16bit(innerClass, dest, j);
  244. if (outerClass != 0)
  245. outerClass = cp.copy(outerClass, newCp, classnames);
  246. ByteArray.write16bit(outerClass, dest, j + 2);
  247. if (innerName != 0)
  248. innerName = cp.copy(innerName, newCp, classnames);
  249. ByteArray.write16bit(innerName, dest, j + 4);
  250. ByteArray.write16bit(innerAccess, dest, j + 6);
  251. j += 8;
  252. }
  253. return attr;
  254. }
  255. }