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.

StackMapTable.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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.DataOutputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.PrintWriter;
  21. import java.io.IOException;
  22. import java.util.Map;
  23. import javassist.CannotCompileException;
  24. /**
  25. * <code>stack_map</code> attribute.
  26. *
  27. * <p>This is an entry in the attributes table of a Code attribute.
  28. * It was introduced by J2SE 6 for the process of verification by
  29. * typechecking.
  30. *
  31. * @see StackMap
  32. * @since 3.4
  33. */
  34. public class StackMapTable extends AttributeInfo {
  35. /**
  36. * The name of this attribute <code>"StackMapTable"</code>.
  37. */
  38. public static final String tag = "StackMapTable";
  39. /**
  40. * Constructs a <code>stack_map</code> attribute.
  41. */
  42. StackMapTable(ConstPool cp, byte[] newInfo) {
  43. super(cp, tag, newInfo);
  44. }
  45. StackMapTable(ConstPool cp, int name_id, DataInputStream in)
  46. throws IOException
  47. {
  48. super(cp, name_id, in);
  49. }
  50. /**
  51. * Makes a copy.
  52. *
  53. * @exception RuntimeCopyException if a <code>BadBytecode</code>
  54. * exception is thrown while copying,
  55. * it is converted into
  56. * <code>RuntimeCopyException</code>.
  57. *
  58. */
  59. public AttributeInfo copy(ConstPool newCp, Map classnames)
  60. throws RuntimeCopyException
  61. {
  62. try {
  63. return new StackMapTable(newCp,
  64. new Copier(this.constPool, info, newCp).doit());
  65. }
  66. catch (BadBytecode e) {
  67. throw new RuntimeCopyException("bad bytecode. fatal?");
  68. }
  69. }
  70. /**
  71. * An exception that may be thrown by <code>copy()</code>
  72. * in <code>StackMapTable</code>.
  73. */
  74. public static class RuntimeCopyException extends RuntimeException {
  75. /**
  76. * Constructs an exception.
  77. */
  78. public RuntimeCopyException(String s) {
  79. super(s);
  80. }
  81. }
  82. void write(DataOutputStream out) throws IOException {
  83. super.write(out);
  84. }
  85. /**
  86. * <code>Top_variable_info.tag</code>.
  87. */
  88. public static final int TOP = 0;
  89. /**
  90. * <code>Integer_variable_info.tag</code>.
  91. */
  92. public static final int INTEGER = 1;
  93. /**
  94. * <code>Float_variable_info.tag</code>.
  95. */
  96. public static final int FLOAT = 2;
  97. /**
  98. * <code>Double_variable_info.tag</code>.
  99. */
  100. public static final int DOUBLE = 3;
  101. /**
  102. * <code>Long_variable_info.tag</code>.
  103. */
  104. public static final int LONG = 4;
  105. /**
  106. * <code>Null_variable_info.tag</code>.
  107. */
  108. public static final int NULL = 5;
  109. /**
  110. * <code>UninitializedThis_variable_info.tag</code>.
  111. */
  112. public static final int THIS = 6;
  113. /**
  114. * <code>Object_variable_info.tag</code>.
  115. */
  116. public static final int OBJECT = 7;
  117. /**
  118. * <code>Uninitialized_variable_info.tag</code>.
  119. */
  120. public static final int UNINIT = 8;
  121. /**
  122. * A code walker for a StackMapTable attribute.
  123. */
  124. public static class Walker {
  125. byte[] info;
  126. int numOfEntries;
  127. /**
  128. * Constructs a walker.
  129. *
  130. * @param smt the StackMapTable that this walker
  131. * walks around.
  132. */
  133. public Walker(StackMapTable smt) {
  134. this(smt.get());
  135. }
  136. /**
  137. * Constructs a walker.
  138. *
  139. * @param data the <code>info</code> field of the
  140. * <code>attribute_info</code> structure.
  141. * It can be obtained by <code>get()</code>
  142. * in the <code>AttributeInfo</code> class.
  143. */
  144. public Walker(byte[] data) {
  145. info = data;
  146. numOfEntries = ByteArray.readU16bit(data, 0);
  147. }
  148. /**
  149. * Returns the number of the entries.
  150. */
  151. public final int size() { return numOfEntries; }
  152. /**
  153. * Visits each entry of the stack map frames.
  154. */
  155. public void parse() throws BadBytecode {
  156. int n = numOfEntries;
  157. int pos = 2;
  158. for (int i = 0; i < n; i++)
  159. pos = stackMapFrames(pos, i);
  160. }
  161. /**
  162. * Invoked when the next entry of the stack map frames is visited.
  163. *
  164. * @param pos the position of the frame in the <code>info</code>
  165. * field of <code>attribute_info</code> structure.
  166. * @param nth the frame is the N-th
  167. * (0, 1st, 2nd, 3rd, 4th, ...) entry.
  168. * @return the position of the next frame.
  169. */
  170. int stackMapFrames(int pos, int nth) throws BadBytecode {
  171. int type = info[pos] & 0xff;
  172. if (type < 64) {
  173. sameFrame(pos, type);
  174. pos++;
  175. }
  176. else if (type < 128)
  177. pos = sameLocals(pos, type);
  178. else if (type < 247)
  179. throw new BadBytecode("bad frame_type in StackMapTable");
  180. else if (type == 247) // SAME_LOCALS_1_STACK_ITEM_EXTENDED
  181. pos = sameLocals(pos, type);
  182. else if (type < 251) {
  183. int offset = ByteArray.readU16bit(info, pos + 1);
  184. chopFrame(pos, offset, 251 - type);
  185. pos += 3;
  186. }
  187. else if (type == 251) { // SAME_FRAME_EXTENDED
  188. int offset = ByteArray.readU16bit(info, pos + 1);
  189. sameFrame(pos, offset);
  190. pos += 3;
  191. }
  192. else if (type < 255)
  193. pos = appendFrame(pos, type);
  194. else // FULL_FRAME
  195. pos = fullFrame(pos);
  196. return pos;
  197. }
  198. /**
  199. * Invoked if the visited frame is a <code>same_frame</code> or
  200. * a <code>same_frame_extended</code>.
  201. *
  202. * @param pos the position of this frame in the <code>info</code>
  203. * field of <code>attribute_info</code> structure.
  204. * @param offsetDelta
  205. */
  206. public void sameFrame(int pos, int offsetDelta) throws BadBytecode {}
  207. private int sameLocals(int pos, int type) throws BadBytecode {
  208. int top = pos;
  209. int offset;
  210. if (type < 128)
  211. offset = type - 64;
  212. else { // type == 247
  213. offset = ByteArray.readU16bit(info, pos + 1);
  214. pos += 2;
  215. }
  216. int tag = info[pos + 1] & 0xff;
  217. int data = 0;
  218. if (tag == OBJECT || tag == UNINIT) {
  219. data = ByteArray.readU16bit(info, pos + 2);
  220. pos += 2;
  221. }
  222. sameLocals(top, offset, tag, data);
  223. return pos + 2;
  224. }
  225. /**
  226. * Invoked if the visited frame is a <code>same_locals_1_stack_item_frame</code>
  227. * or a <code>same_locals_1_stack_item_frame_extended</code>.
  228. *
  229. * @param pos the position.
  230. * @param offsetDelta
  231. * @param stackTag <code>stack[0].tag</code>.
  232. * @param stackData <code>stack[0].cpool_index</code>
  233. * if the tag is <code>OBJECT</code>,
  234. * or <code>stack[0].offset</code>
  235. * if the tag is <code>UNINIT</code>.
  236. */
  237. public void sameLocals(int pos, int offsetDelta, int stackTag, int stackData)
  238. throws BadBytecode {}
  239. /**
  240. * Invoked if the visited frame is a <code>chop_frame</code>.
  241. *
  242. * @param pos the position.
  243. * @param offsetDelta
  244. * @param k the <cod>k</code> last locals are absent.
  245. */
  246. public void chopFrame(int pos, int offsetDelta, int k) throws BadBytecode {}
  247. private int appendFrame(int pos, int type) throws BadBytecode {
  248. int k = type - 251;
  249. int offset = ByteArray.readU16bit(info, pos + 1);
  250. int[] tags = new int[k];
  251. int[] data = new int[k];
  252. int p = pos + 3;
  253. for (int i = 0; i < k; i++) {
  254. int tag = info[p] & 0xff;
  255. tags[i] = tag;
  256. if (tag == OBJECT || tag == UNINIT) {
  257. data[i] = ByteArray.readU16bit(info, p + 1);
  258. p += 3;
  259. }
  260. else {
  261. data[i] = 0;
  262. p++;
  263. }
  264. }
  265. appendFrame(pos, offset, tags, data);
  266. return p;
  267. }
  268. /**
  269. * Invoked if the visited frame is a <code>append_frame</code>.
  270. *
  271. * @param pos the position.
  272. * @param offsetDelta
  273. * @param tags <code>locals[i].tag</code>.
  274. * @param data <code>locals[i].cpool_index</code>
  275. * or <cod>locals[i].offset</code>.
  276. */
  277. public void appendFrame(int pos, int offsetDelta, int[] tags, int[] data)
  278. throws BadBytecode {}
  279. private int fullFrame(int pos) throws BadBytecode {
  280. int offset = ByteArray.readU16bit(info, pos + 1);
  281. int numOfLocals = ByteArray.readU16bit(info, pos + 3);
  282. int[] localsTags = new int[numOfLocals];
  283. int[] localsData = new int[numOfLocals];
  284. int p = verifyTypeInfo(pos + 5, numOfLocals, localsTags, localsData);
  285. int numOfItems = ByteArray.readU16bit(info, p);
  286. int[] itemsTags = new int[numOfItems];
  287. int[] itemsData = new int[numOfItems];
  288. p = verifyTypeInfo(p + 2, numOfItems, itemsTags, itemsData);
  289. fullFrame(pos, offset, localsTags, localsData, itemsTags, itemsData);
  290. return p;
  291. }
  292. /**
  293. * Invoked if the visited frame is <code>full_frame</code>.
  294. *
  295. * @param pos the position.
  296. * @param offsetDelta
  297. * @param localTags <code>locals[i].tag</code>
  298. * @param localData <code>locals[i].cpool_index</code>
  299. * or <code>locals[i].offset</code>
  300. * @param stackTags <code>stack[i].tag</code>
  301. * @param stackData <code>stack[i].cpool_index</code>
  302. * or <code>stack[i].offset</code>
  303. */
  304. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  305. int[] stackTags, int[] stackData)
  306. throws BadBytecode {}
  307. private int verifyTypeInfo(int pos, int n, int[] tags, int[] data) {
  308. for (int i = 0; i < n; i++) {
  309. int tag = info[pos++] & 0xff;
  310. tags[i] = tag;
  311. if (tag == OBJECT || tag == UNINIT) {
  312. data[i] = ByteArray.readU16bit(info, pos);
  313. pos += 2;
  314. }
  315. }
  316. return pos;
  317. }
  318. }
  319. static class SimpleCopy extends Walker {
  320. private Writer writer;
  321. public SimpleCopy(byte[] data) {
  322. super(data);
  323. writer = new Writer(data.length);
  324. }
  325. public byte[] doit() throws BadBytecode {
  326. parse();
  327. return writer.toByteArray();
  328. }
  329. public void sameFrame(int pos, int offsetDelta) {
  330. writer.sameFrame(offsetDelta);
  331. }
  332. public void sameLocals(int pos, int offsetDelta, int stackTag, int stackData) {
  333. writer.sameLocals(offsetDelta, stackTag, copyData(stackTag, stackData));
  334. }
  335. public void chopFrame(int pos, int offsetDelta, int k) {
  336. writer.chopFrame(offsetDelta, k);
  337. }
  338. public void appendFrame(int pos, int offsetDelta, int[] tags, int[] data) {
  339. writer.appendFrame(offsetDelta, tags, copyData(tags, data));
  340. }
  341. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  342. int[] stackTags, int[] stackData) {
  343. writer.fullFrame(offsetDelta, localTags, copyData(localTags, localData),
  344. stackTags, copyData(stackTags, stackData));
  345. }
  346. protected int copyData(int tag, int data) {
  347. return data;
  348. }
  349. protected int[] copyData(int[] tags, int[] data) {
  350. return data;
  351. }
  352. }
  353. static class Copier extends SimpleCopy {
  354. private ConstPool srcPool, destPool;
  355. public Copier(ConstPool src, byte[] data, ConstPool dest) {
  356. super(data);
  357. srcPool = src;
  358. destPool = dest;
  359. }
  360. protected int copyData(int tag, int data) {
  361. if (tag == OBJECT)
  362. return srcPool.copy(data, destPool, null);
  363. else
  364. return data;
  365. }
  366. protected int[] copyData(int[] tags, int[] data) {
  367. int[] newData = new int[data.length];
  368. for (int i = 0; i < data.length; i++)
  369. if (tags[i] == OBJECT)
  370. newData[i] = srcPool.copy(data[i], destPool, null);
  371. else
  372. newData[i] = data[i];
  373. return newData;
  374. }
  375. }
  376. /**
  377. * Updates this stack map table when a new local variable is inserted
  378. * for a new parameter.
  379. *
  380. * @param index the index of the added local variable.
  381. * @param tag the type tag of that local variable.
  382. * @param classInfo the index of the <code>CONSTANT_Class_info</code> structure
  383. * in a constant pool table. This should be zero unless the tag
  384. * is <code>ITEM_Object</code>.
  385. *
  386. * @see javassist.CtBehavior#addParameter(javassist.CtClass)
  387. * @see #typeTagOf(char)
  388. * @see ConstPool
  389. */
  390. public void insertLocal(int index, int tag, int classInfo)
  391. throws BadBytecode
  392. {
  393. byte[] data = new InsertLocal(this.get(), index, tag, classInfo).doit();
  394. this.set(data);
  395. }
  396. /**
  397. * Returns the tag of the type specified by the
  398. * descriptor. This method returns <code>INTEGER</code>
  399. * unless the descriptor is either D (double), F (float),
  400. * J (long), L (class type), or [ (array).
  401. *
  402. * @param descriptor the type descriptor.
  403. * @see Descriptor
  404. */
  405. public static int typeTagOf(char descriptor) {
  406. switch (descriptor) {
  407. case 'D' :
  408. return DOUBLE;
  409. case 'F' :
  410. return FLOAT;
  411. case 'J' :
  412. return LONG;
  413. case 'L' :
  414. case '[' :
  415. return OBJECT;
  416. // case 'V' :
  417. default :
  418. return INTEGER;
  419. }
  420. }
  421. /* This implementation assumes that a local variable initially
  422. * holding a parameter value is never changed to be a different
  423. * type.
  424. *
  425. */
  426. static class InsertLocal extends SimpleCopy {
  427. private int varIndex;
  428. private int varTag, varData;
  429. public InsertLocal(byte[] data, int varIndex, int varTag, int varData) {
  430. super(data);
  431. this.varIndex = varIndex;
  432. this.varTag = varTag;
  433. this.varData = varData;
  434. }
  435. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  436. int[] stackTags, int[] stackData) {
  437. int len = localTags.length;
  438. if (len < varIndex) {
  439. super.fullFrame(pos, offsetDelta, localTags, localData, stackTags, stackData);
  440. return;
  441. }
  442. int typeSize = (varTag == LONG || varTag == DOUBLE) ? 2 : 1;
  443. int[] localTags2 = new int[len + typeSize];
  444. int[] localData2 = new int[len + typeSize];
  445. int index = varIndex;
  446. int j = 0;
  447. for (int i = 0; i < len; i++) {
  448. if (j == index)
  449. j += typeSize;
  450. localTags2[j] = localTags[i];
  451. localData2[j++] = localData[i];
  452. }
  453. localTags2[index] = varTag;
  454. localData2[index] = varData;
  455. if (typeSize > 1) {
  456. localTags2[index + 1] = TOP;
  457. localData2[index + 1] = 0;
  458. }
  459. super.fullFrame(pos, offsetDelta, localTags2, localData2, stackTags, stackData);
  460. }
  461. }
  462. /**
  463. * A writer of stack map tables.
  464. */
  465. public static class Writer {
  466. ByteArrayOutputStream output;
  467. int numOfEntries;
  468. /**
  469. * Constructs a writer.
  470. * @param size the initial buffer size.
  471. */
  472. public Writer(int size) {
  473. output = new ByteArrayOutputStream(size);
  474. numOfEntries = 0;
  475. output.write(0); // u2 number_of_entries
  476. output.write(0);
  477. }
  478. /**
  479. * Returns the stack map table written out.
  480. */
  481. public byte[] toByteArray() {
  482. byte[] b = output.toByteArray();
  483. ByteArray.write16bit(numOfEntries, b, 0);
  484. return b;
  485. }
  486. /**
  487. * Constructs and a return a stack map table containing
  488. * the written stack map entries.
  489. *
  490. * @param cp the constant pool used to write
  491. * the stack map entries.
  492. */
  493. public StackMapTable toStackMapTable(ConstPool cp) {
  494. return new StackMapTable(cp, toByteArray());
  495. }
  496. /**
  497. * Writes a <code>same_frame</code> or a <code>same_frame_extended</code>.
  498. */
  499. public void sameFrame(int offsetDelta) {
  500. numOfEntries++;
  501. if (offsetDelta < 64)
  502. output.write(offsetDelta);
  503. else {
  504. output.write(251); // SAME_FRAME_EXTENDED
  505. write16(offsetDelta);
  506. }
  507. }
  508. /**
  509. * Writes a <code>same_locals_1_stack_item</code>
  510. * or a <code>same_locals_1_stack_item_extended</code>.
  511. *
  512. * @param tag <code>stack[0].tag</code>.
  513. * @param data <code>stack[0].cpool_index</code>
  514. * if the tag is <code>OBJECT</code>,
  515. * or <cod>stack[0].offset</code>
  516. * if the tag is <code>UNINIT</code>.
  517. * Otherwise, this parameter is not used.
  518. */
  519. public void sameLocals(int offsetDelta, int tag, int data) {
  520. numOfEntries++;
  521. if (offsetDelta < 64)
  522. output.write(offsetDelta + 64);
  523. else {
  524. output.write(247); // SAME_LOCALS_1_STACK_ITEM_EXTENDED
  525. write16(offsetDelta);
  526. }
  527. writeTypeInfo(tag, data);
  528. }
  529. /**
  530. * Writes a <code>chop_frame</code>.
  531. *
  532. * @param k the number of absent locals. 1, 2, or 3.
  533. */
  534. public void chopFrame(int offsetDelta, int k) {
  535. numOfEntries++;
  536. output.write(251 - k);
  537. write16(offsetDelta);
  538. }
  539. /**
  540. * Writes a <code>append_frame</code>. The number of the appended
  541. * locals is specified by the length of <code>tags</code>.
  542. *
  543. * @param tags <code>locals[].tag</code>.
  544. * The length of this array must be
  545. * either 1, 2, or 3.
  546. * @param data <code>locals[].cpool_index</code>
  547. * if the tag is <code>OBJECT</code>,
  548. * or <cod>locals[].offset</code>
  549. * if the tag is <code>UNINIT</code>.
  550. * Otherwise, this parameter is not used.
  551. */
  552. public void appendFrame(int offsetDelta, int[] tags, int[] data) {
  553. numOfEntries++;
  554. int k = tags.length; // k is 1, 2, or 3
  555. output.write(k + 251);
  556. write16(offsetDelta);
  557. for (int i = 0; i < k; i++)
  558. writeTypeInfo(tags[i], data[i]);
  559. }
  560. /**
  561. * Writes a <code>full_frame</code>.
  562. * <code>number_of_locals</code> and <code>number_of_stack_items</code>
  563. * are specified by the the length of <code>localTags</code> and
  564. * <code>stackTags</code>.
  565. *
  566. * @param localTags <code>locals[].tag</code>.
  567. * @param localData <code>locals[].cpool_index</code>
  568. * if the tag is <code>OBJECT</code>,
  569. * or <cod>locals[].offset</code>
  570. * if the tag is <code>UNINIT</code>.
  571. * Otherwise, this parameter is not used.
  572. * @param stackTags <code>stack[].tag</code>.
  573. * @param stackData <code>stack[].cpool_index</code>
  574. * if the tag is <code>OBJECT</code>,
  575. * or <cod>stack[].offset</code>
  576. * if the tag is <code>UNINIT</code>.
  577. * Otherwise, this parameter is not used.
  578. */
  579. public void fullFrame(int offsetDelta, int[] localTags, int[] localData,
  580. int[] stackTags, int[] stackData) {
  581. numOfEntries++;
  582. output.write(255); // FULL_FRAME
  583. write16(offsetDelta);
  584. int n = localTags.length;
  585. write16(n);
  586. for (int i = 0; i < n; i++)
  587. writeTypeInfo(localTags[i], localData[i]);
  588. n = stackTags.length;
  589. write16(n);
  590. for (int i = 0; i < n; i++)
  591. writeTypeInfo(stackTags[i], stackData[i]);
  592. }
  593. private void writeTypeInfo(int tag, int data) {
  594. output.write(tag);
  595. if (tag == OBJECT || tag == UNINIT)
  596. write16(data);
  597. }
  598. private void write16(int value) {
  599. output.write((value >>> 8) & 0xff);
  600. output.write(value & 0xff);
  601. }
  602. }
  603. /**
  604. * Prints the stack table map.
  605. */
  606. public void println(PrintWriter w) {
  607. Printer.print(this, w);
  608. }
  609. /**
  610. * Prints the stack table map.
  611. *
  612. * @param ps a print stream such as <code>System.out</code>.
  613. */
  614. public void println(java.io.PrintStream ps) {
  615. Printer.print(this, new java.io.PrintWriter(ps, true));
  616. }
  617. static class Printer extends Walker {
  618. private PrintWriter writer;
  619. private int offset;
  620. /**
  621. * Prints the stack table map.
  622. */
  623. public static void print(StackMapTable smt, PrintWriter writer) {
  624. try {
  625. new Printer(smt.get(), writer).parse();
  626. }
  627. catch (BadBytecode e) {
  628. writer.println(e.getMessage());
  629. }
  630. }
  631. Printer(byte[] data, PrintWriter pw) {
  632. super(data);
  633. writer = pw;
  634. offset = -1;
  635. }
  636. public void sameFrame(int pos, int offsetDelta) {
  637. offset += offsetDelta + 1;
  638. writer.println(offset + " same frame: " + offsetDelta);
  639. }
  640. public void sameLocals(int pos, int offsetDelta, int stackTag, int stackData) {
  641. offset += offsetDelta + 1;
  642. writer.println(offset + " same locals: " + offsetDelta);
  643. printTypeInfo(stackTag, stackData);
  644. }
  645. public void chopFrame(int pos, int offsetDelta, int k) {
  646. offset += offsetDelta + 1;
  647. writer.println(offset + " chop frame: " + offsetDelta + ", " + k + " last locals");
  648. }
  649. public void appendFrame(int pos, int offsetDelta, int[] tags, int[] data) {
  650. offset += offsetDelta + 1;
  651. writer.println(offset + " append frame: " + offsetDelta);
  652. for (int i = 0; i < tags.length; i++)
  653. printTypeInfo(tags[i], data[i]);
  654. }
  655. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  656. int[] stackTags, int[] stackData) {
  657. offset += offsetDelta + 1;
  658. writer.println(offset + " full frame: " + offsetDelta);
  659. writer.println("[locals]");
  660. for (int i = 0; i < localTags.length; i++)
  661. printTypeInfo(localTags[i], localData[i]);
  662. writer.println("[stack]");
  663. for (int i = 0; i < stackTags.length; i++)
  664. printTypeInfo(stackTags[i], stackData[i]);
  665. }
  666. private void printTypeInfo(int tag, int data) {
  667. String msg = null;
  668. switch (tag) {
  669. case TOP :
  670. msg = "top";
  671. break;
  672. case INTEGER :
  673. msg = "integer";
  674. break;
  675. case FLOAT :
  676. msg = "float";
  677. break;
  678. case DOUBLE :
  679. msg = "double";
  680. break;
  681. case LONG :
  682. msg = "long";
  683. break;
  684. case NULL :
  685. msg = "null";
  686. break;
  687. case THIS :
  688. msg = "this";
  689. break;
  690. case OBJECT :
  691. msg = "object (cpool_index " + data + ")";
  692. break;
  693. case UNINIT :
  694. msg = "uninitialized (offset " + data + ")";
  695. break;
  696. }
  697. writer.print(" ");
  698. writer.println(msg);
  699. }
  700. }
  701. void shiftPc(int where, int gapSize, boolean exclusive)
  702. throws BadBytecode
  703. {
  704. new Shifter(this, where, gapSize, exclusive).doit();
  705. }
  706. static class Shifter extends Walker {
  707. private StackMapTable stackMap;
  708. private int where, gap;
  709. private int position;
  710. private byte[] updatedInfo;
  711. private boolean exclusive;
  712. public Shifter(StackMapTable smt, int where, int gap, boolean exclusive) {
  713. super(smt);
  714. stackMap = smt;
  715. this.where = where;
  716. this.gap = gap;
  717. this.position = 0;
  718. this.updatedInfo = null;
  719. this.exclusive = exclusive;
  720. }
  721. public void doit() throws BadBytecode {
  722. parse();
  723. if (updatedInfo != null)
  724. stackMap.set(updatedInfo);
  725. }
  726. public void sameFrame(int pos, int offsetDelta) {
  727. update(pos, offsetDelta, 0, 251);
  728. }
  729. public void sameLocals(int pos, int offsetDelta, int stackTag, int stackData) {
  730. update(pos, offsetDelta, 64, 247);
  731. }
  732. private void update(int pos, int offsetDelta, int base, int entry) {
  733. int oldPos = position;
  734. position = oldPos + offsetDelta + (oldPos == 0 ? 0 : 1);
  735. boolean match;
  736. if (exclusive)
  737. match = oldPos < where && where <= position;
  738. else
  739. match = oldPos <= where && where < position;
  740. if (match) {
  741. int newDelta = offsetDelta + gap;
  742. position += gap;
  743. if (newDelta < 64)
  744. info[pos] = (byte)(newDelta + base);
  745. else if (offsetDelta < 64) {
  746. byte[] newinfo = insertGap(info, pos, 2);
  747. newinfo[pos] = (byte)entry;
  748. ByteArray.write16bit(newDelta, newinfo, pos + 1);
  749. updatedInfo = newinfo;
  750. }
  751. else
  752. ByteArray.write16bit(newDelta, info, pos + 1);
  753. }
  754. }
  755. private static byte[] insertGap(byte[] info, int where, int gap) {
  756. int len = info.length;
  757. byte[] newinfo = new byte[len + gap];
  758. for (int i = 0; i < len; i++)
  759. newinfo[i + (i < where ? 0 : gap)] = info[i];
  760. return newinfo;
  761. }
  762. public void chopFrame(int pos, int offsetDelta, int k) {
  763. update(pos, offsetDelta);
  764. }
  765. public void appendFrame(int pos, int offsetDelta, int[] tags, int[] data) {
  766. update(pos, offsetDelta);
  767. }
  768. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  769. int[] stackTags, int[] stackData) {
  770. update(pos, offsetDelta);
  771. }
  772. private void update(int pos, int offsetDelta) {
  773. int oldPos = position;
  774. position = oldPos + offsetDelta + (oldPos == 0 ? 0 : 1);
  775. boolean match;
  776. if (exclusive)
  777. match = oldPos < where && where <= position;
  778. else
  779. match = oldPos <= where && where < position;
  780. if (match) {
  781. int newDelta = offsetDelta + gap;
  782. ByteArray.write16bit(newDelta, info, pos + 1);
  783. position += gap;
  784. }
  785. }
  786. }
  787. /**
  788. * Undocumented method. Do not use; internal-use only.
  789. *
  790. * <p>This method is for javassist.convert.TransformNew.
  791. * It is called to update the stack map table when
  792. * the NEW opcode (and the following DUP) is removed.
  793. *
  794. * @param where the position of the removed NEW opcode.
  795. */
  796. public void removeNew(int where) throws CannotCompileException {
  797. try {
  798. byte[] data = new NewRemover(this.get(), where).doit();
  799. this.set(data);
  800. }
  801. catch (BadBytecode e) {
  802. throw new CannotCompileException("bad stack map table", e);
  803. }
  804. }
  805. static class NewRemover extends SimpleCopy {
  806. int posOfNew;
  807. public NewRemover(byte[] data, int pos) {
  808. super(data);
  809. posOfNew = pos;
  810. }
  811. public void sameLocals(int pos, int offsetDelta, int stackTag, int stackData) {
  812. if (stackTag == UNINIT && stackData == posOfNew)
  813. super.sameFrame(pos, offsetDelta);
  814. else
  815. super.sameLocals(pos, offsetDelta, stackTag, stackData);
  816. }
  817. public void fullFrame(int pos, int offsetDelta, int[] localTags, int[] localData,
  818. int[] stackTags, int[] stackData) {
  819. int n = stackTags.length - 1;
  820. for (int i = 0; i < n; i++)
  821. if (stackTags[i] == UNINIT && stackData[i] == posOfNew
  822. && stackTags[i + 1] == UNINIT && stackData[i + 1] == posOfNew) {
  823. n++;
  824. int[] stackTags2 = new int[n - 2];
  825. int[] stackData2 = new int[n - 2];
  826. int k = 0;
  827. for (int j = 0; j < n; j++)
  828. if (j == i)
  829. j++;
  830. else {
  831. stackTags2[k] = stackTags[j];
  832. stackData2[k++] = stackData[j];
  833. }
  834. stackTags = stackTags2;
  835. stackData = stackData2;
  836. break;
  837. }
  838. super.fullFrame(pos, offsetDelta, localTags, localData, stackTags, stackData);
  839. }
  840. }
  841. }