Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LocalVariableAttribute.java 12KB

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