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.

CodeIterator.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2006 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. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.bytecode;
  16. /**
  17. * An iterator for editing a code attribute.
  18. *
  19. * <p>This iterator does not provide <code>remove()</code>.
  20. * If a piece of code in a <code>Code_attribute</code> is unnecessary,
  21. * it should be overwritten with <code>NOP</code>.
  22. *
  23. * @see CodeAttribute#iterator()
  24. */
  25. public class CodeIterator implements Opcode {
  26. protected CodeAttribute codeAttr;
  27. protected byte[] bytecode;
  28. protected int endPos;
  29. protected int currentPos;
  30. CodeIterator(CodeAttribute ca) {
  31. codeAttr = ca;
  32. bytecode = ca.getCode();
  33. begin();
  34. }
  35. /**
  36. * Moves to the first instruction.
  37. */
  38. public void begin() {
  39. currentPos = 0;
  40. endPos = getCodeLength();
  41. }
  42. /**
  43. * Moves to the given index.
  44. *
  45. * <p>The index of the next instruction is set to the given index.
  46. * The successive call to <code>next()</code>
  47. * returns the index that has been given to <code>move()</code>.
  48. *
  49. * <p>Note that the index is into the byte array returned by
  50. * <code>get().getCode()</code>.
  51. *
  52. * @see CodeAttribute#getCode()
  53. */
  54. public void move(int index) {
  55. currentPos = index;
  56. }
  57. /**
  58. * Returns a Code attribute read with this iterator.
  59. */
  60. public CodeAttribute get() {
  61. return codeAttr;
  62. }
  63. /**
  64. * Returns <code>code_length</code> of <code>Code_attribute</code>.
  65. */
  66. public int getCodeLength() {
  67. return bytecode.length;
  68. }
  69. /**
  70. * Returns the unsigned 8bit value at the given index.
  71. */
  72. public int byteAt(int index) { return bytecode[index] & 0xff; }
  73. /**
  74. * Writes an 8bit value at the given index.
  75. */
  76. public void writeByte(int value, int index) {
  77. bytecode[index] = (byte)value;
  78. }
  79. /**
  80. * Returns the unsigned 16bit value at the given index.
  81. */
  82. public int u16bitAt(int index) {
  83. return ByteArray.readU16bit(bytecode, index);
  84. }
  85. /**
  86. * Returns the signed 16bit value at the given index.
  87. */
  88. public int s16bitAt(int index) {
  89. return ByteArray.readS16bit(bytecode, index);
  90. }
  91. /**
  92. * Writes a 16 bit integer at the index.
  93. */
  94. public void write16bit(int value, int index) {
  95. ByteArray.write16bit(value, bytecode, index);
  96. }
  97. /**
  98. * Returns the signed 32bit value at the given index.
  99. */
  100. public int s32bitAt(int index) {
  101. return ByteArray.read32bit(bytecode, index);
  102. }
  103. /**
  104. * Writes a 32bit integer at the index.
  105. */
  106. public void write32bit(int value, int index) {
  107. ByteArray.write32bit(value, bytecode, index);
  108. }
  109. /**
  110. * Writes a byte array at the index.
  111. *
  112. * @param code may be a zero-length array.
  113. */
  114. public void write(byte[] code, int index) {
  115. int len = code.length;
  116. for (int j = 0; j < len; ++j)
  117. bytecode[index++] = code[j];
  118. }
  119. /**
  120. * Returns true if there is more instructions.
  121. */
  122. public boolean hasNext() { return currentPos < endPos; }
  123. /**
  124. * Returns the index of the next instruction
  125. * (not the operand following the current opcode).
  126. *
  127. * <p>Note that the index is into the byte array returned by
  128. * <code>get().getCode()</code>.
  129. *
  130. * @see CodeAttribute#getCode()
  131. * @see CodeIterator#byteAt(int)
  132. */
  133. public int next() throws BadBytecode {
  134. int pos = currentPos;
  135. currentPos = nextOpcode(bytecode, pos);
  136. return pos;
  137. }
  138. /**
  139. * Obtains the value that the next call
  140. * to <code>next()</code> will return.
  141. *
  142. * <p>This method is side-effects free.
  143. * Successive calls to <code>lookAhead()</code> return the
  144. * same value until <code>next()</code> is called.
  145. */
  146. public int lookAhead() {
  147. return currentPos;
  148. }
  149. /**
  150. * Moves to the instruction for
  151. * either <code>super()</code> or <code>this()</code>.
  152. *
  153. * <p>This method skips all the instructions for computing arguments
  154. * to <code>super()</code> or <code>this()</code>, which should be
  155. * placed at the beginning of a constructor body.
  156. *
  157. * <p>This method returns the index of INVOKESPECIAL instruction
  158. * executing <code>super()</code> or <code>this()</code>.
  159. * A successive call to <code>next()</code> returns the
  160. * index of the next instruction following that INVOKESPECIAL.
  161. *
  162. * <p>This method works only for a constructor.
  163. *
  164. * @return the index of the INVOKESPECIAL instruction, or -1
  165. * if a constructor invocation is not found.
  166. */
  167. public int skipConstructor() throws BadBytecode {
  168. return skipSuperConstructor0(-1);
  169. }
  170. /**
  171. * Moves to the instruction for <code>super()</code>.
  172. *
  173. * <p>This method skips all the instructions for computing arguments to
  174. * <code>super()</code>, which should be
  175. * placed at the beginning of a constructor body.
  176. *
  177. * <p>This method returns the index of INVOKESPECIAL instruction
  178. * executing <code>super()</code>.
  179. * A successive call to <code>next()</code> returns the
  180. * index of the next instruction following that INVOKESPECIAL.
  181. *
  182. * <p>This method works only for a constructor.
  183. *
  184. * @return the index of the INVOKESPECIAL instruction, or -1
  185. * if a super constructor invocation is not found
  186. * but <code>this()</code> is found.
  187. */
  188. public int skipSuperConstructor() throws BadBytecode {
  189. return skipSuperConstructor0(0);
  190. }
  191. /**
  192. * Moves to the instruction for <code>this()</code>.
  193. *
  194. * <p>This method skips all the instructions for computing arguments to
  195. * <code>this()</code>, which should be
  196. * placed at the beginning of a constructor body.
  197. *
  198. * <p>This method returns the index of INVOKESPECIAL instruction
  199. * executing <code>this()</code>.
  200. * A successive call to <code>next()</code> returns the
  201. * index of the next instruction following that INVOKESPECIAL.
  202. *
  203. * <p>This method works only for a constructor.
  204. *
  205. * @return the index of the INVOKESPECIAL instruction, or -1
  206. * if a explicit constructor invocation is not found
  207. * but <code>super()</code> is found.
  208. */
  209. public int skipThisConstructor() throws BadBytecode {
  210. return skipSuperConstructor0(1);
  211. }
  212. /* skipSuper 1: this(), 0: super(), -1: both.
  213. */
  214. private int skipSuperConstructor0(int skipThis) throws BadBytecode {
  215. begin();
  216. ConstPool cp = codeAttr.getConstPool();
  217. String thisClassName = codeAttr.getDeclaringClass();
  218. int nested = 0;
  219. while (hasNext()) {
  220. int index = next();
  221. int c = byteAt(index);
  222. if (c == NEW)
  223. ++nested;
  224. else if (c == INVOKESPECIAL) {
  225. int mref = ByteArray.readU16bit(bytecode, index + 1);
  226. if (cp.getMethodrefName(mref).equals(MethodInfo.nameInit))
  227. if (--nested < 0) {
  228. if (skipThis < 0)
  229. return index;
  230. String cname = cp.getMethodrefClassName(mref);
  231. if (cname.equals(thisClassName) == (skipThis > 0))
  232. return index;
  233. else
  234. break;
  235. }
  236. }
  237. }
  238. begin();
  239. return -1;
  240. }
  241. /**
  242. * Inserts the given bytecode sequence
  243. * before the next instruction that would be returned by
  244. * <code>next()</code> (not before the instruction returned
  245. * by tha last call to <code>next()</code>).
  246. * Branch offsets and the exception table are also updated.
  247. *
  248. * <p>If the next instruction is at the beginning of a block statement,
  249. * then the bytecode is inserted within that block.
  250. *
  251. * <p>An extra gap may be inserted at the end of the inserted
  252. * bytecode sequence for adjusting alignment if the code attribute
  253. * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
  254. *
  255. * @param code inserted bytecode sequence.
  256. * @return the index indicating the first byte of the
  257. * inserted byte sequence.
  258. */
  259. public int insert(byte[] code)
  260. throws BadBytecode
  261. {
  262. int pos = currentPos;
  263. insert0(currentPos, code, false);
  264. return pos;
  265. }
  266. /**
  267. * Inserts the given bytecode sequence
  268. * before the instruction at the given index <code>pos</code>.
  269. * Branch offsets and the exception table are also updated.
  270. *
  271. * <p>If the instruction at the given index is at the beginning
  272. * of a block statement,
  273. * then the bytecode is inserted within that block.
  274. *
  275. * <p>An extra gap may be inserted at the end of the inserted
  276. * bytecode sequence for adjusting alignment if the code attribute
  277. * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
  278. *
  279. * @param pos the index at which a byte sequence is inserted.
  280. * @param code inserted bytecode sequence.
  281. */
  282. public void insert(int pos, byte[] code) throws BadBytecode {
  283. insert0(pos, code, false);
  284. }
  285. /**
  286. * Inserts the given bytecode sequence exclusively
  287. * before the next instruction that would be returned by
  288. * <code>next()</code> (not before the instruction returned
  289. * by tha last call to <code>next()</code>).
  290. * Branch offsets and the exception table are also updated.
  291. *
  292. * <p>If the next instruction is at the beginning of a block statement,
  293. * then the bytecode is excluded from that block.
  294. *
  295. * <p>An extra gap may be inserted at the end of the inserted
  296. * bytecode sequence for adjusting alignment if the code attribute
  297. * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
  298. *
  299. * @param code inserted bytecode sequence.
  300. * @return the index indicating the first byte of the
  301. * inserted byte sequence.
  302. */
  303. public int insertEx(byte[] code)
  304. throws BadBytecode
  305. {
  306. int pos = currentPos;
  307. insert0(currentPos, code, true);
  308. return pos;
  309. }
  310. /**
  311. * Inserts the given bytecode sequence exclusively
  312. * before the instruction at the given index <code>pos</code>.
  313. * Branch offsets and the exception table are also updated.
  314. *
  315. * <p>If the instruction at the given index is at the beginning
  316. * of a block statement,
  317. * then the bytecode is excluded from that block.
  318. *
  319. * <p>An extra gap may be inserted at the end of the inserted
  320. * bytecode sequence for adjusting alignment if the code attribute
  321. * includes <code>LOOKUPSWITCH</code> or <code>TABLESWITCH</code>.
  322. *
  323. * @param pos the index at which a byte sequence is inserted.
  324. * @param code inserted bytecode sequence.
  325. */
  326. public void insertEx(int pos, byte[] code) throws BadBytecode {
  327. insert0(pos, code, true);
  328. }
  329. private void insert0(int pos, byte[] code, boolean exclusive)
  330. throws BadBytecode
  331. {
  332. int len = code.length;
  333. if (len <= 0)
  334. return;
  335. insertGapCore(pos, len, exclusive); // currentPos will change.
  336. for (int j = 0; j < len; ++j)
  337. bytecode[pos++] = code[j];
  338. }
  339. /**
  340. * Inserts a gap
  341. * before the next instruction that would be returned by
  342. * <code>next()</code> (not before the instruction returned
  343. * by tha last call to <code>next()</code>).
  344. * Branch offsets and the exception table are also updated.
  345. * The inserted gap is filled with NOP. The gap length may be
  346. * extended to a multiple of 4.
  347. *
  348. * <p>If the next instruction is at the beginning of a block statement,
  349. * then the gap is inserted within that block.
  350. *
  351. * @param length gap length
  352. * @return the index indicating the first byte of the inserted gap.
  353. */
  354. public int insertGap(int length) throws BadBytecode {
  355. int pos = currentPos;
  356. insertGapCore(currentPos, length, false);
  357. return pos;
  358. }
  359. /**
  360. * Inserts a gap in front of the instruction at the given
  361. * index <code>pos</code>.
  362. * Branch offsets and the exception table are also updated.
  363. * The inserted gap is filled with NOP. The gap length may be
  364. * extended to a multiple of 4.
  365. *
  366. * <p>If the instruction at the given index is at the beginning
  367. * of a block statement,
  368. * then the gap is inserted within that block.
  369. *
  370. * @param pos the index at which a gap is inserted.
  371. * @param length gap length.
  372. * @return the length of the inserted gap.
  373. * It might be bigger than <code>length</code>.
  374. */
  375. public int insertGap(int pos, int length) throws BadBytecode {
  376. return insertGapCore(pos, length, false);
  377. }
  378. /**
  379. * Inserts an exclusive gap
  380. * before the next instruction that would be returned by
  381. * <code>next()</code> (not before the instruction returned
  382. * by tha last call to <code>next()</code>).
  383. * Branch offsets and the exception table are also updated.
  384. * The inserted gap is filled with NOP. The gap length may be
  385. * extended to a multiple of 4.
  386. *
  387. * <p>If the next instruction is at the beginning of a block statement,
  388. * then the gap is excluded from that block.
  389. *
  390. * @param length gap length
  391. * @return the index indicating the first byte of the inserted gap.
  392. */
  393. public int insertExGap(int length) throws BadBytecode {
  394. int pos = currentPos;
  395. insertGapCore(currentPos, length, true);
  396. return pos;
  397. }
  398. /**
  399. * Inserts an exclusive gap in front of the instruction at the given
  400. * index <code>pos</code>.
  401. * Branch offsets and the exception table are also updated.
  402. * The inserted gap is filled with NOP. The gap length may be
  403. * extended to a multiple of 4.
  404. *
  405. * <p>If the instruction at the given index is at the beginning
  406. * of a block statement,
  407. * then the gap is excluded from that block.
  408. *
  409. * @param pos the index at which a gap is inserted.
  410. * @param length gap length.
  411. * @return the length of the inserted gap.
  412. * It might be bigger than <code>length</code>.
  413. */
  414. public int insertExGap(int pos, int length) throws BadBytecode {
  415. return insertGapCore(pos, length, true);
  416. }
  417. /**
  418. * @return the length of the really inserted gap.
  419. */
  420. private int insertGapCore(int pos, int length, boolean exclusive)
  421. throws BadBytecode
  422. {
  423. if (length <= 0)
  424. return 0;
  425. int cur = currentPos;
  426. byte[] c = insertGap(bytecode, pos, length, exclusive,
  427. get().getExceptionTable(), codeAttr);
  428. int length2 = c.length - bytecode.length;
  429. if (cur >= pos)
  430. currentPos = cur + length2;
  431. codeAttr.setCode(c);
  432. bytecode = c;
  433. endPos = getCodeLength();
  434. return length2;
  435. }
  436. /**
  437. * Copies and inserts the entries in the given exception table
  438. * at the beginning of the exception table in the code attribute
  439. * edited by this object.
  440. *
  441. * @param offset the value added to the code positions included
  442. * in the entries.
  443. */
  444. public void insert(ExceptionTable et, int offset) {
  445. codeAttr.getExceptionTable().add(0, et, offset);
  446. }
  447. /**
  448. * Appends the given bytecode sequence at the end.
  449. *
  450. * @param code the bytecode appended.
  451. * @return the position of the first byte of the appended bytecode.
  452. */
  453. public int append(byte[] code) {
  454. int size = getCodeLength();
  455. int len = code.length;
  456. if (len <= 0)
  457. return size;
  458. appendGap(len);
  459. byte[] dest = bytecode;
  460. for (int i = 0; i < len; ++i)
  461. dest[i + size] = code[i];
  462. return size;
  463. }
  464. /**
  465. * Appends a gap at the end of the bytecode sequence.
  466. *
  467. * @param gapLength gap length
  468. */
  469. public void appendGap(int gapLength) {
  470. byte[] code = bytecode;
  471. int codeLength = code.length;
  472. byte[] newcode = new byte[codeLength + gapLength];
  473. int i;
  474. for (i = 0; i < codeLength; ++i)
  475. newcode[i] = code[i];
  476. for (i = codeLength; i < codeLength + gapLength; ++i)
  477. newcode[i] = NOP;
  478. codeAttr.setCode(newcode);
  479. bytecode = newcode;
  480. endPos = getCodeLength();
  481. }
  482. /**
  483. * Copies and appends the entries in the given exception table
  484. * at the end of the exception table in the code attribute
  485. * edited by this object.
  486. *
  487. * @param offset the value added to the code positions included
  488. * in the entries.
  489. */
  490. public void append(ExceptionTable et, int offset) {
  491. ExceptionTable table = codeAttr.getExceptionTable();
  492. table.add(table.size(), et, offset);
  493. }
  494. /* opcodeLegth is used for implementing nextOpcode().
  495. */
  496. private static final int opcodeLength[] = {
  497. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 2, 3,
  498. 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  499. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1,
  500. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  501. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  502. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  503. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1,
  504. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3,
  505. 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 0, 0, 1, 1, 1, 1, 1, 1, 3, 3,
  506. 3, 3, 3, 3, 3, 5, 0, 3, 2, 3, 1, 1, 3, 3, 1, 1, 0, 4, 3, 3,
  507. 5, 5
  508. };
  509. // 0 .. UNUSED (186), LOOKUPSWITCH, TABLESWITCH, WIDE
  510. /**
  511. * Calculates the index of the next opcode.
  512. */
  513. static int nextOpcode(byte[] code, int index)
  514. throws BadBytecode
  515. {
  516. int opcode;
  517. try {
  518. opcode = code[index] & 0xff;
  519. }
  520. catch (IndexOutOfBoundsException e) {
  521. throw new BadBytecode("invalid opcode address");
  522. }
  523. try {
  524. int len = opcodeLength[opcode];
  525. if (len > 0)
  526. return index + len;
  527. else if (opcode == WIDE)
  528. if (code[index + 1] == (byte)IINC) // WIDE IINC
  529. return index + 6;
  530. else
  531. return index + 4; // WIDE ...
  532. else {
  533. int index2 = (index & ~3) + 8;
  534. if (opcode == LOOKUPSWITCH) {
  535. int npairs = ByteArray.read32bit(code, index2);
  536. return index2 + npairs * 8 + 4;
  537. }
  538. else if (opcode == TABLESWITCH) {
  539. int low = ByteArray.read32bit(code, index2);
  540. int high = ByteArray.read32bit(code, index2 + 4);
  541. return index2 + (high - low + 1) * 4 + 8;
  542. }
  543. // else
  544. // throw new BadBytecode(opcode);
  545. }
  546. }
  547. catch (IndexOutOfBoundsException e) {
  548. }
  549. // opcode is UNUSED or an IndexOutOfBoundsException was thrown.
  550. throw new BadBytecode(opcode);
  551. }
  552. // methods for implementing insertGap().
  553. /* If "where" is the beginning of a block statement, then the inserted
  554. * gap is also included in the block statement.
  555. * "where" must indicate the first byte of an opcode.
  556. * The inserted gap is filled with NOP. gapLength may be extended to
  557. * a multiple of 4.
  558. */
  559. static byte[] insertGap(byte[] code, int where, int gapLength,
  560. boolean exclusive, ExceptionTable etable, CodeAttribute ca)
  561. throws BadBytecode
  562. {
  563. if (gapLength <= 0)
  564. return code;
  565. try {
  566. return insertGap0(code, where, gapLength, exclusive, etable, ca);
  567. }
  568. catch (AlignmentException e) {
  569. try {
  570. return insertGap0(code, where, (gapLength + 3) & ~3,
  571. exclusive, etable, ca);
  572. }
  573. catch (AlignmentException e2) {
  574. throw new RuntimeException("fatal error?");
  575. }
  576. }
  577. }
  578. private static byte[] insertGap0(byte[] code, int where, int gapLength,
  579. boolean exclusive, ExceptionTable etable,
  580. CodeAttribute ca)
  581. throws BadBytecode, AlignmentException
  582. {
  583. int codeLength = code.length;
  584. byte[] newcode = new byte[codeLength + gapLength];
  585. insertGap2(code, where, gapLength, codeLength, newcode, exclusive);
  586. etable.shiftPc(where, gapLength, exclusive);
  587. LineNumberAttribute na
  588. = (LineNumberAttribute)ca.getAttribute(LineNumberAttribute.tag);
  589. if (na != null)
  590. na.shiftPc(where, gapLength, exclusive);
  591. LocalVariableAttribute va = (LocalVariableAttribute)ca.getAttribute(
  592. LocalVariableAttribute.tag);
  593. if (va != null)
  594. va.shiftPc(where, gapLength, exclusive);
  595. LocalVariableAttribute vta
  596. = (LocalVariableAttribute)ca.getAttribute(
  597. LocalVariableAttribute.typeTag);
  598. if (vta != null)
  599. vta.shiftPc(where, gapLength, exclusive);
  600. return newcode;
  601. }
  602. private static void insertGap2(byte[] code, int where, int gapLength,
  603. int endPos, byte[] newcode, boolean exclusive)
  604. throws BadBytecode, AlignmentException
  605. {
  606. int nextPos;
  607. int i = 0;
  608. int j = 0;
  609. for (; i < endPos; i = nextPos) {
  610. if (i == where) {
  611. int j2 = j + gapLength;
  612. while (j < j2)
  613. newcode[j++] = NOP;
  614. }
  615. nextPos = nextOpcode(code, i);
  616. int inst = code[i] & 0xff;
  617. // if<cond>, if_icmp<cond>, if_acmp<cond>, goto, jsr
  618. if ((153 <= inst && inst <= 168)
  619. || inst == IFNULL || inst == IFNONNULL) {
  620. /* 2bytes *signed* offset */
  621. int offset = (code[i + 1] << 8) | (code[i + 2] & 0xff);
  622. offset = newOffset(i, offset, where, gapLength, exclusive);
  623. newcode[j] = code[i];
  624. ByteArray.write16bit(offset, newcode, j + 1);
  625. j += 3;
  626. }
  627. else if (inst == GOTO_W || inst == JSR_W) {
  628. /* 4bytes offset */
  629. int offset = ByteArray.read32bit(code, i + 1);
  630. offset = newOffset(i, offset, where, gapLength, exclusive);
  631. newcode[j++] = code[i];
  632. ByteArray.write32bit(offset, newcode, j);
  633. j += 4;
  634. }
  635. else if (inst == TABLESWITCH) {
  636. if (i != j && (gapLength & 3) != 0)
  637. throw new AlignmentException();
  638. int i0 = i;
  639. int i2 = (i & ~3) + 4; // 0-3 byte padding
  640. while (i0 < i2)
  641. newcode[j++] = code[i0++];
  642. int defaultbyte = newOffset(i, ByteArray.read32bit(code, i2),
  643. where, gapLength, exclusive);
  644. ByteArray.write32bit(defaultbyte, newcode, j);
  645. int lowbyte = ByteArray.read32bit(code, i2 + 4);
  646. ByteArray.write32bit(lowbyte, newcode, j + 4);
  647. int highbyte = ByteArray.read32bit(code, i2 + 8);
  648. ByteArray.write32bit(highbyte, newcode, j + 8);
  649. j += 12;
  650. i0 = i2 + 12;
  651. i2 = i0 + (highbyte - lowbyte + 1) * 4;
  652. while (i0 < i2) {
  653. int offset = newOffset(i, ByteArray.read32bit(code, i0),
  654. where, gapLength, exclusive);
  655. ByteArray.write32bit(offset, newcode, j);
  656. j += 4;
  657. i0 += 4;
  658. }
  659. }
  660. else if (inst == LOOKUPSWITCH) {
  661. if (i != j && (gapLength & 3) != 0)
  662. throw new AlignmentException();
  663. int i0 = i;
  664. int i2 = (i & ~3) + 4; // 0-3 byte padding
  665. while (i0 < i2)
  666. newcode[j++] = code[i0++];
  667. int defaultbyte = newOffset(i, ByteArray.read32bit(code, i2),
  668. where, gapLength, exclusive);
  669. ByteArray.write32bit(defaultbyte, newcode, j);
  670. int npairs = ByteArray.read32bit(code, i2 + 4);
  671. ByteArray.write32bit(npairs, newcode, j + 4);
  672. j += 8;
  673. i0 = i2 + 8;
  674. i2 = i0 + npairs * 8;
  675. while (i0 < i2) {
  676. ByteArray.copy32bit(code, i0, newcode, j);
  677. int offset = newOffset(i,
  678. ByteArray.read32bit(code, i0 + 4),
  679. where, gapLength, exclusive);
  680. ByteArray.write32bit(offset, newcode, j + 4);
  681. j += 8;
  682. i0 += 8;
  683. }
  684. }
  685. else
  686. while (i < nextPos)
  687. newcode[j++] = code[i++];
  688. }
  689. }
  690. private static int newOffset(int i, int offset, int where,
  691. int gapLength, boolean exclusive) {
  692. int target = i + offset;
  693. if (i < where) {
  694. if (where < target || (exclusive && where == target))
  695. offset += gapLength;
  696. }
  697. else
  698. if (target < where || (!exclusive && where == target))
  699. offset -= gapLength;
  700. return offset;
  701. }
  702. }
  703. class AlignmentException extends Exception {
  704. }