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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.util.Map;
  19. import java.io.IOException;
  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. *
  59. * @return null or the class name.
  60. */
  61. public String innerClass(int nth) {
  62. int i = innerClassIndex(nth);
  63. if (i == 0)
  64. return null;
  65. else
  66. return constPool.getClassInfo(i);
  67. }
  68. /**
  69. * Sets <code>classes[nth].inner_class_info_index</code> to
  70. * the given index.
  71. */
  72. public void setInnerClassIndex(int nth, int index) {
  73. ByteArray.write16bit(index, get(), nth * 8 + 2);
  74. }
  75. /**
  76. * Returns <code>classes[nth].outer_class_info_index</code>.
  77. */
  78. public int outerClassIndex(int nth) {
  79. return ByteArray.readU16bit(get(), nth * 8 + 4);
  80. }
  81. /**
  82. * Returns the class name indicated
  83. * by <code>classes[nth].outer_class_info_index</code>.
  84. *
  85. * @return null or the class name.
  86. */
  87. public String outerClass(int nth) {
  88. int i = outerClassIndex(nth);
  89. if (i == 0)
  90. return null;
  91. else
  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. else
  118. return constPool.getUtf8Info(i);
  119. }
  120. /**
  121. * Sets <code>classes[nth].inner_name_index</code> to
  122. * the given index.
  123. */
  124. public void setInnerNameIndex(int nth, int index) {
  125. ByteArray.write16bit(index, get(), nth * 8 + 6);
  126. }
  127. /**
  128. * Returns <code>classes[nth].inner_class_access_flags</code>.
  129. */
  130. public int accessFlags(int nth) {
  131. return ByteArray.readU16bit(get(), nth * 8 + 8);
  132. }
  133. /**
  134. * Sets <code>classes[nth].inner_class_access_flags</code> to
  135. * the given index.
  136. */
  137. public void setAccessFlags(int nth, int flags) {
  138. ByteArray.write16bit(flags, get(), nth * 8 + 8);
  139. }
  140. /**
  141. * Appends a new entry.
  142. *
  143. * @param inner <code>inner_class_info_index</code>
  144. * @param outer <code>outer_class_info_index</code>
  145. * @param name <code>inner_name_index</code>
  146. * @param flags <code>inner_class_access_flags</code>
  147. */
  148. public void append(String inner, String outer, String name, int flags) {
  149. int i = constPool.addClassInfo(inner);
  150. int o = constPool.addClassInfo(outer);
  151. int n = constPool.addUtf8Info(name);
  152. append(i, o, n, flags);
  153. }
  154. /**
  155. * Appends a new entry.
  156. *
  157. * @param inner <code>inner_class_info_index</code>
  158. * @param outer <code>outer_class_info_index</code>
  159. * @param name <code>inner_name_index</code>
  160. * @param flags <code>inner_class_access_flags</code>
  161. */
  162. public void append(int inner, int outer, int name, int flags) {
  163. byte[] data = get();
  164. int len = data.length;
  165. byte[] newData = new byte[len + 8];
  166. for (int i = 2; i < len; ++i)
  167. newData[i] = data[i];
  168. int n = ByteArray.readU16bit(data, 0);
  169. ByteArray.write16bit(n + 1, newData, 0);
  170. ByteArray.write16bit(inner, newData, len);
  171. ByteArray.write16bit(outer, newData, len + 2);
  172. ByteArray.write16bit(name, newData, len + 4);
  173. ByteArray.write16bit(flags, newData, len + 6);
  174. set(newData);
  175. }
  176. /**
  177. * Makes a copy. Class names are replaced according to the
  178. * given <code>Map</code> object.
  179. *
  180. * @param newCp the constant pool table used by the new copy.
  181. * @param classnames pairs of replaced and substituted
  182. * class names.
  183. */
  184. public AttributeInfo copy(ConstPool newCp, Map classnames) {
  185. byte[] src = get();
  186. byte[] dest = new byte[src.length];
  187. ConstPool cp = getConstPool();
  188. InnerClassesAttribute attr = new InnerClassesAttribute(newCp, dest);
  189. int n = ByteArray.readU16bit(src, 0);
  190. ByteArray.write16bit(n, dest, 0);
  191. int j = 2;
  192. for (int i = 0; i < n; ++i) {
  193. int innerClass = ByteArray.readU16bit(src, j);
  194. int outerClass = ByteArray.readU16bit(src, j + 2);
  195. int innerName = ByteArray.readU16bit(src, j + 4);
  196. int innerAccess = ByteArray.readU16bit(src, j + 6);
  197. if (innerClass != 0)
  198. innerClass = cp.copy(innerClass, newCp, classnames);
  199. ByteArray.write16bit(innerClass, dest, j);
  200. if (outerClass != 0)
  201. outerClass = cp.copy(outerClass, newCp, classnames);
  202. ByteArray.write16bit(outerClass, dest, j + 2);
  203. if (innerName != 0)
  204. innerName = cp.copy(innerName, newCp, classnames);
  205. ByteArray.write16bit(innerName, dest, j + 4);
  206. ByteArray.write16bit(innerAccess, dest, j + 6);
  207. j += 8;
  208. }
  209. return attr;
  210. }
  211. }