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.

LocalVariableAttribute.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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>LocalVariableTable_attribute</code>.
  22. */
  23. public class LocalVariableAttribute extends AttributeInfo {
  24. /**
  25. * The name of this attribute <code>"LocalVariableTable"</code>.
  26. */
  27. public static final String tag = "LocalVariableTable";
  28. /**
  29. * The name of the attribute <code>"LocalVariableTypeTable"</code>.
  30. */
  31. public static final String typeTag = "LocalVariableTypeTable";
  32. /**
  33. * Constructs an empty LocalVariableTable.
  34. */
  35. public LocalVariableAttribute(ConstPool cp) {
  36. super(cp, tag, new byte[2]);
  37. ByteArray.write16bit(0, info, 0);
  38. }
  39. /**
  40. * Constructs an empty LocalVariableTable.
  41. *
  42. * @param name the attribute name.
  43. * <code>LocalVariableAttribute.tag</code> or
  44. * <code>LocalVariableAttribute.typeTag</code>.
  45. * @see #tag
  46. * @see #typeTag
  47. * @since 3.1
  48. * @deprecated
  49. */
  50. @Deprecated
  51. public LocalVariableAttribute(ConstPool cp, String name) {
  52. super(cp, name, new byte[2]);
  53. ByteArray.write16bit(0, info, 0);
  54. }
  55. LocalVariableAttribute(ConstPool cp, int n, DataInputStream in)
  56. throws IOException
  57. {
  58. super(cp, n, in);
  59. }
  60. LocalVariableAttribute(ConstPool cp, String name, byte[] i) {
  61. super(cp, name, i);
  62. }
  63. /**
  64. * Appends a new entry to <code>local_variable_table</code>.
  65. *
  66. * @param startPc <code>start_pc</code>
  67. * @param length <code>length</code>
  68. * @param nameIndex <code>name_index</code>
  69. * @param descriptorIndex <code>descriptor_index</code>
  70. * @param index <code>index</code>
  71. */
  72. public void addEntry(int startPc, int length, int nameIndex,
  73. int descriptorIndex, int index) {
  74. int size = info.length;
  75. byte[] newInfo = new byte[size + 10];
  76. ByteArray.write16bit(tableLength() + 1, newInfo, 0);
  77. for (int i = 2; i < size; ++i)
  78. newInfo[i] = info[i];
  79. ByteArray.write16bit(startPc, newInfo, size);
  80. ByteArray.write16bit(length, newInfo, size + 2);
  81. ByteArray.write16bit(nameIndex, newInfo, size + 4);
  82. ByteArray.write16bit(descriptorIndex, newInfo, size + 6);
  83. ByteArray.write16bit(index, newInfo, size + 8);
  84. info = newInfo;
  85. }
  86. @Override
  87. void renameClass(String oldname, String newname) {
  88. ConstPool cp = getConstPool();
  89. int n = tableLength();
  90. for (int i = 0; i < n; ++i) {
  91. int pos = i * 10 + 2;
  92. int index = ByteArray.readU16bit(info, pos + 6);
  93. if (index != 0) {
  94. String desc = cp.getUtf8Info(index);
  95. desc = renameEntry(desc, oldname, newname);
  96. ByteArray.write16bit(cp.addUtf8Info(desc), info, pos + 6);
  97. }
  98. }
  99. }
  100. String renameEntry(String desc, String oldname, String newname) {
  101. return Descriptor.rename(desc, oldname, newname);
  102. }
  103. @Override
  104. void renameClass(Map<String,String> classnames) {
  105. ConstPool cp = getConstPool();
  106. int n = tableLength();
  107. for (int i = 0; i < n; ++i) {
  108. int pos = i * 10 + 2;
  109. int index = ByteArray.readU16bit(info, pos + 6);
  110. if (index != 0) {
  111. String desc = cp.getUtf8Info(index);
  112. desc = renameEntry(desc, classnames);
  113. ByteArray.write16bit(cp.addUtf8Info(desc), info, pos + 6);
  114. }
  115. }
  116. }
  117. String renameEntry(String desc, Map<String,String> classnames) {
  118. return Descriptor.rename(desc, classnames);
  119. }
  120. /**
  121. * For each <code>local_variable_table[i].index</code>,
  122. * this method increases <code>index</code> by <code>delta</code>.
  123. *
  124. * @param lessThan the index does not change if it
  125. * is less than this value.
  126. */
  127. public void shiftIndex(int lessThan, int delta) {
  128. int size = info.length;
  129. for (int i = 2; i < size; i += 10){
  130. int org = ByteArray.readU16bit(info, i + 8);
  131. if (org >= lessThan)
  132. ByteArray.write16bit(org + delta, info, i + 8);
  133. }
  134. }
  135. /**
  136. * Returns <code>local_variable_table_length</code>.
  137. * This represents the number of entries in the table.
  138. */
  139. public int tableLength() {
  140. return ByteArray.readU16bit(info, 0);
  141. }
  142. /**
  143. * Returns <code>local_variable_table[i].start_pc</code>.
  144. * This represents the index into the code array from which the local
  145. * variable is effective.
  146. *
  147. * @param i the i-th entry.
  148. */
  149. public int startPc(int i) {
  150. return ByteArray.readU16bit(info, i * 10 + 2);
  151. }
  152. /**
  153. * Returns <code>local_variable_table[i].length</code>.
  154. * This represents the length of the code region in which the local
  155. * variable is effective.
  156. *
  157. * @param i the i-th entry.
  158. */
  159. public int codeLength(int i) {
  160. return ByteArray.readU16bit(info, i * 10 + 4);
  161. }
  162. /**
  163. * Adjusts start_pc and length if bytecode is inserted in a method body.
  164. */
  165. void shiftPc(int where, int gapLength, boolean exclusive) {
  166. int n = tableLength();
  167. for (int i = 0; i < n; ++i) {
  168. int pos = i * 10 + 2;
  169. int pc = ByteArray.readU16bit(info, pos);
  170. int len = ByteArray.readU16bit(info, pos + 2);
  171. /* if pc == 0, then the local variable is a method parameter.
  172. */
  173. if (pc > where || (exclusive && pc == where && pc != 0))
  174. ByteArray.write16bit(pc + gapLength, info, pos);
  175. else if (pc + len > where || (exclusive && pc + len == where))
  176. ByteArray.write16bit(len + gapLength, info, pos + 2);
  177. }
  178. }
  179. /**
  180. * Returns the value of <code>local_variable_table[i].name_index</code>.
  181. * This represents the name of the local variable.
  182. *
  183. * @param i the i-th entry.
  184. */
  185. public int nameIndex(int i) {
  186. return ByteArray.readU16bit(info, i * 10 + 6);
  187. }
  188. /**
  189. * Returns the name of the local variable
  190. * specified by <code>local_variable_table[i].name_index</code>.
  191. *
  192. * @param i the i-th entry.
  193. */
  194. public String variableName(int i) {
  195. return getConstPool().getUtf8Info(nameIndex(i));
  196. }
  197. /**
  198. * Returns the name of the local variable with given index.
  199. * If you want to get the parameter name of method with correct order,
  200. * you should using this method.
  201. *
  202. * @param index the index of the local variable.
  203. */
  204. public String variableNameByIndex(int index) {
  205. for (int i = 0; i < tableLength(); i++) {
  206. if (index(i) == index) {
  207. return variableName(i);
  208. }
  209. }
  210. throw new ArrayIndexOutOfBoundsException();
  211. }
  212. /**
  213. * Returns the value of
  214. * <code>local_variable_table[i].descriptor_index</code>.
  215. * This represents the type descriptor of the local variable.
  216. * <p>
  217. * If this attribute represents a LocalVariableTypeTable attribute,
  218. * this method returns the value of
  219. * <code>local_variable_type_table[i].signature_index</code>.
  220. * It represents the type of the local variable.
  221. *
  222. * @param i the i-th entry.
  223. */
  224. public int descriptorIndex(int i) {
  225. return ByteArray.readU16bit(info, i * 10 + 8);
  226. }
  227. /**
  228. * This method is equivalent to <code>descriptorIndex()</code>.
  229. * If this attribute represents a LocalVariableTypeTable attribute,
  230. * this method should be used instead of <code>descriptorIndex()</code>
  231. * since the method name is more appropriate.
  232. *
  233. * @param i the i-th entry.
  234. * @see #descriptorIndex(int)
  235. * @see SignatureAttribute#toFieldSignature(String)
  236. */
  237. public int signatureIndex(int i) {
  238. return descriptorIndex(i);
  239. }
  240. /**
  241. * Returns the type descriptor of the local variable
  242. * specified by <code>local_variable_table[i].descriptor_index</code>.
  243. * <p>
  244. * If this attribute represents a LocalVariableTypeTable attribute,
  245. * this method returns the type signature of the local variable
  246. * specified by <code>local_variable_type_table[i].signature_index</code>.
  247. *
  248. * @param i the i-th entry.
  249. */
  250. public String descriptor(int i) {
  251. return getConstPool().getUtf8Info(descriptorIndex(i));
  252. }
  253. /**
  254. * This method is equivalent to <code>descriptor()</code>.
  255. * If this attribute represents a LocalVariableTypeTable attribute,
  256. * this method should be used instead of <code>descriptor()</code>
  257. * since the method name is more appropriate.
  258. *
  259. * <p>To parse the string, call <code>toFieldSignature(String)</code>
  260. * in <code>SignatureAttribute</code>.
  261. *
  262. * @param i the i-th entry.
  263. * @see #descriptor(int)
  264. * @see SignatureAttribute#toFieldSignature(String)
  265. */
  266. public String signature(int i) {
  267. return descriptor(i);
  268. }
  269. /**
  270. * Returns <code>local_variable_table[i].index</code>.
  271. * This represents the index of the local variable.
  272. *
  273. * @param i the i-th entry.
  274. */
  275. public int index(int i) {
  276. return ByteArray.readU16bit(info, i * 10 + 10);
  277. }
  278. /**
  279. * Makes a copy.
  280. *
  281. * @param newCp the constant pool table used by the new copy.
  282. * @param classnames should be null.
  283. */
  284. @Override
  285. public AttributeInfo copy(ConstPool newCp, Map<String,String> classnames) {
  286. byte[] src = get();
  287. byte[] dest = new byte[src.length];
  288. ConstPool cp = getConstPool();
  289. LocalVariableAttribute attr = makeThisAttr(newCp, dest);
  290. int n = ByteArray.readU16bit(src, 0);
  291. ByteArray.write16bit(n, dest, 0);
  292. int j = 2;
  293. for (int i = 0; i < n; ++i) {
  294. int start = ByteArray.readU16bit(src, j);
  295. int len = ByteArray.readU16bit(src, j + 2);
  296. int name = ByteArray.readU16bit(src, j + 4);
  297. int type = ByteArray.readU16bit(src, j + 6);
  298. int index = ByteArray.readU16bit(src, j + 8);
  299. ByteArray.write16bit(start, dest, j);
  300. ByteArray.write16bit(len, dest, j + 2);
  301. if (name != 0)
  302. name = cp.copy(name, newCp, null);
  303. ByteArray.write16bit(name, dest, j + 4);
  304. if (type != 0) {
  305. String sig = cp.getUtf8Info(type);
  306. sig = Descriptor.rename(sig, classnames);
  307. type = newCp.addUtf8Info(sig);
  308. }
  309. ByteArray.write16bit(type, dest, j + 6);
  310. ByteArray.write16bit(index, dest, j + 8);
  311. j += 10;
  312. }
  313. return attr;
  314. }
  315. // LocalVariableTypeAttribute overrides this method.
  316. LocalVariableAttribute makeThisAttr(ConstPool cp, byte[] dest) {
  317. return new LocalVariableAttribute(cp, tag, dest);
  318. }
  319. }