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.

Bytecode.java 40KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2005 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. import javassist.CtClass;
  17. import javassist.CtPrimitiveType;
  18. class ByteVector implements Cloneable {
  19. private byte[] buffer;
  20. private int size;
  21. public ByteVector() {
  22. buffer = new byte[64];
  23. size = 0;
  24. }
  25. public Object clone() throws CloneNotSupportedException {
  26. ByteVector bv = (ByteVector)super.clone();
  27. bv.buffer = (byte[])buffer.clone();
  28. return bv;
  29. }
  30. public final int getSize() { return size; }
  31. public final byte[] copy() {
  32. byte[] b = new byte[size];
  33. arraycopy(buffer, b, size);
  34. return b;
  35. }
  36. public int read(int offset) {
  37. if (offset < 0 || size <= offset)
  38. throw new ArrayIndexOutOfBoundsException(offset);
  39. return buffer[offset];
  40. }
  41. public void write(int offset, int value) {
  42. if (offset < 0 || size <= offset)
  43. throw new ArrayIndexOutOfBoundsException(offset);
  44. buffer[offset] = (byte)value;
  45. }
  46. public void add(int code) {
  47. addGap(1);
  48. buffer[size - 1] = (byte)code;
  49. }
  50. public void addGap(int length) {
  51. if (size + length > buffer.length) {
  52. int newSize = size << 1;
  53. if (newSize < size + length)
  54. newSize = size + length;
  55. byte[] newBuf = new byte[newSize];
  56. arraycopy(buffer, newBuf, size);
  57. buffer = newBuf;
  58. }
  59. size += length;
  60. }
  61. private static void arraycopy(byte[] src, byte[] dest, int size) {
  62. for (int i = 0; i < size; i++)
  63. dest[i] = src[i];
  64. }
  65. }
  66. /**
  67. * A utility class for producing a bytecode sequence.
  68. *
  69. * <p>A <code>Bytecode</code> object is an unbounded array
  70. * containing bytecode. For example,
  71. *
  72. * <ul><pre>ConstPool cp = ...; // constant pool table
  73. * Bytecode b = new Bytecode(cp, 1, 0);
  74. * b.addIconst(3);
  75. * b.addReturn(CtClass.intType);
  76. * CodeAttribute ca = b.toCodeAttribute();</ul></pre>
  77. *
  78. * <p>This program produces a Code attribute including a bytecode
  79. * sequence:
  80. *
  81. * <ul><pre>iconst_3
  82. * ireturn</pre></ul>
  83. *
  84. * @see ConstPool
  85. * @see CodeAttribute
  86. */
  87. public class Bytecode extends ByteVector implements Cloneable, Opcode {
  88. /**
  89. * Represents the <code>CtClass</code> file using the
  90. * constant pool table given to this <code>Bytecode</code> object.
  91. */
  92. public static final CtClass THIS = ConstPool.THIS;
  93. ConstPool constPool;
  94. int maxStack, maxLocals;
  95. ExceptionTable tryblocks;
  96. private int stackDepth;
  97. /**
  98. * Constructs a <code>Bytecode</code> object with an empty bytecode
  99. * sequence.
  100. *
  101. * <p>The parameters <code>stacksize</code> and <code>localvars</code>
  102. * specify initial values
  103. * of <code>max_stack</code> and <code>max_locals</code>.
  104. * They can be changed later.
  105. *
  106. * @param cp constant pool table.
  107. * @param stacksize <code>max_stack</code>.
  108. * @param localvars <code>max_locals</code>.
  109. */
  110. public Bytecode(ConstPool cp, int stacksize, int localvars) {
  111. constPool = cp;
  112. maxStack = stacksize;
  113. maxLocals = localvars;
  114. tryblocks = new ExceptionTable(cp);
  115. stackDepth = 0;
  116. }
  117. /**
  118. * Constructs a <code>Bytecode</code> object with an empty bytecode
  119. * sequence. The initial values of <code>max_stack</code> and
  120. * <code>max_locals</code> are zero.
  121. *
  122. * @param cp constant pool table.
  123. * @see Bytecode#setMaxStack(int)
  124. * @see Bytecode#setMaxLocals(int)
  125. */
  126. public Bytecode(ConstPool cp) {
  127. this(cp, 0, 0);
  128. }
  129. /**
  130. * Creates and returns a copy of this object.
  131. * The constant pool object is shared between this object
  132. * and the cloned object.
  133. */
  134. public Object clone() {
  135. try {
  136. Bytecode bc = (Bytecode)super.clone();
  137. bc.tryblocks = (ExceptionTable)tryblocks.clone();
  138. return bc;
  139. }
  140. catch (CloneNotSupportedException cnse) {
  141. throw new RuntimeException(cnse);
  142. }
  143. }
  144. /**
  145. * Gets a constant pool table.
  146. */
  147. public ConstPool getConstPool() { return constPool; }
  148. /**
  149. * Returns <code>exception_table</code>.
  150. */
  151. public ExceptionTable getExceptionTable() { return tryblocks; }
  152. /**
  153. * Converts to a <code>CodeAttribute</code>.
  154. */
  155. public CodeAttribute toCodeAttribute() {
  156. return new CodeAttribute(constPool, maxStack, maxLocals,
  157. get(), tryblocks);
  158. }
  159. /**
  160. * Returns the length of the bytecode sequence.
  161. */
  162. public int length() {
  163. return getSize();
  164. }
  165. /**
  166. * Returns the produced bytecode sequence.
  167. */
  168. public byte[] get() {
  169. return copy();
  170. }
  171. /**
  172. * Gets <code>max_stack</code>.
  173. */
  174. public int getMaxStack() { return maxStack; }
  175. /**
  176. * Sets <code>max_stack</code>.
  177. *
  178. * <p>This value may be automatically updated when an instruction
  179. * is appended. A <code>Bytecode</code> object maintains the current
  180. * stack depth whenever an instruction is added
  181. * by <code>addOpcode()</code>. For example, if DUP is appended,
  182. * the current stack depth is increased by one. If the new stack
  183. * depth is more than <code>max_stack</code>, then it is assigned
  184. * to <code>max_stack</code>. However, if branch instructions are
  185. * appended, the current stack depth may not be correctly maintained.
  186. *
  187. * @see #addOpcode(int)
  188. */
  189. public void setMaxStack(int size) {
  190. maxStack = size;
  191. }
  192. /**
  193. * Gets <code>max_locals</code>.
  194. */
  195. public int getMaxLocals() { return maxLocals; }
  196. /**
  197. * Sets <code>max_locals</code>.
  198. */
  199. public void setMaxLocals(int size) {
  200. maxLocals = size;
  201. }
  202. /**
  203. * Sets <code>max_locals</code>.
  204. *
  205. * <p>This computes the number of local variables
  206. * used to pass method parameters and sets <code>max_locals</code>
  207. * to that number plus <code>locals</code>.
  208. *
  209. * @param isStatic true if <code>params</code> must be
  210. * interpreted as parameters to a static method.
  211. * @param params parameter types.
  212. * @param locals the number of local variables excluding
  213. * ones used to pass parameters.
  214. */
  215. public void setMaxLocals(boolean isStatic, CtClass[] params,
  216. int locals) {
  217. if (!isStatic)
  218. ++locals;
  219. if (params != null) {
  220. CtClass doubleType = CtClass.doubleType;
  221. CtClass longType = CtClass.longType;
  222. int n = params.length;
  223. for (int i = 0; i < n; ++i) {
  224. CtClass type = params[i];
  225. if (type == doubleType || type == longType)
  226. locals += 2;
  227. else
  228. ++locals;
  229. }
  230. }
  231. maxLocals = locals;
  232. }
  233. /**
  234. * Increments <code>max_locals</code>.
  235. */
  236. public void incMaxLocals(int diff) {
  237. maxLocals += diff;
  238. }
  239. /**
  240. * Adds a new entry of <code>exception_table</code>.
  241. */
  242. public void addExceptionHandler(int start, int end,
  243. int handler, CtClass type) {
  244. addExceptionHandler(start, end, handler,
  245. constPool.addClassInfo(type));
  246. }
  247. /**
  248. * Adds a new entry of <code>exception_table</code>.
  249. *
  250. * @param type the fully-qualified name of a throwable class.
  251. */
  252. public void addExceptionHandler(int start, int end,
  253. int handler, String type) {
  254. addExceptionHandler(start, end, handler,
  255. constPool.addClassInfo(type));
  256. }
  257. /**
  258. * Adds a new entry of <code>exception_table</code>.
  259. */
  260. public void addExceptionHandler(int start, int end,
  261. int handler, int type) {
  262. tryblocks.add(start, end, handler, type);
  263. }
  264. /**
  265. * Returns the length of bytecode sequence
  266. * that have been added so far.
  267. */
  268. public int currentPc() {
  269. return getSize();
  270. }
  271. /**
  272. * Reads a signed 8bit value at the offset from the beginning of the
  273. * bytecode sequence.
  274. *
  275. * @throws ArrayIndexOutOfBoundsException if offset is invalid.
  276. */
  277. public int read(int offset) {
  278. return super.read(offset);
  279. }
  280. /**
  281. * Reads a signed 16bit value at the offset from the beginning of the
  282. * bytecode sequence.
  283. */
  284. public int read16bit(int offset) {
  285. int v1 = read(offset);
  286. int v2 = read(offset + 1);
  287. return (v1 << 8) + (v2 & 0xff);
  288. }
  289. /**
  290. * Reads a signed 32bit value at the offset from the beginning of the
  291. * bytecode sequence.
  292. */
  293. public int read32bit(int offset) {
  294. int v1 = read16bit(offset);
  295. int v2 = read16bit(offset + 2);
  296. return (v1 << 16) + (v2 & 0xffff);
  297. }
  298. /**
  299. * Writes an 8bit value at the offset from the beginning of the
  300. * bytecode sequence.
  301. *
  302. * @throws ArrayIndexOutOfBoundsException if offset is invalid.
  303. */
  304. public void write(int offset, int value) {
  305. super.write(offset, value);
  306. }
  307. /**
  308. * Writes an 16bit value at the offset from the beginning of the
  309. * bytecode sequence.
  310. */
  311. public void write16bit(int offset, int value) {
  312. write(offset, value >> 8);
  313. write(offset + 1, value);
  314. }
  315. /**
  316. * Writes an 32bit value at the offset from the beginning of the
  317. * bytecode sequence.
  318. */
  319. public void write32bit(int offset, int value) {
  320. write16bit(offset, value >> 16);
  321. write16bit(offset + 2, value);
  322. }
  323. /**
  324. * Appends an 8bit value to the end of the bytecode sequence.
  325. */
  326. public void add(int code) {
  327. super.add(code);
  328. }
  329. /**
  330. * Appends a 32bit value to the end of the bytecode sequence.
  331. */
  332. public void add32bit(int value) {
  333. add(value >> 24);
  334. add(value >> 16);
  335. add(value >> 8);
  336. add(value);
  337. }
  338. /**
  339. * Appends the length-byte gap to the end of the bytecode sequence.
  340. *
  341. * @param length the gap length in byte.
  342. */
  343. public void addGap(int length) {
  344. super.addGap(length);
  345. }
  346. /**
  347. * Appends an 8bit opcode to the end of the bytecode sequence.
  348. * The current stack depth is updated.
  349. * <code>max_stack</code> is updated if the current stack depth
  350. * is the deepest so far.
  351. *
  352. * <p>Note: some instructions such as INVOKEVIRTUAL does not
  353. * update the current stack depth since the increment depends
  354. * on the method signature.
  355. * <code>growStack()</code> must be explicitly called.
  356. */
  357. public void addOpcode(int code) {
  358. add(code);
  359. growStack(STACK_GROW[code]);
  360. }
  361. /**
  362. * Increases the current stack depth.
  363. * It also updates <code>max_stack</code> if the current stack depth
  364. * is the deepest so far.
  365. *
  366. * @param diff the number added to the current stack depth.
  367. */
  368. public void growStack(int diff) {
  369. setStackDepth(stackDepth + diff);
  370. }
  371. /**
  372. * Returns the current stack depth.
  373. */
  374. public int getStackDepth() { return stackDepth; }
  375. /**
  376. * Sets the current stack depth.
  377. * It also updates <code>max_stack</code> if the current stack depth
  378. * is the deepest so far.
  379. *
  380. * @param depth new value.
  381. */
  382. public void setStackDepth(int depth) {
  383. stackDepth = depth;
  384. if (stackDepth > maxStack)
  385. maxStack = stackDepth;
  386. }
  387. /**
  388. * Appends a 16bit value to the end of the bytecode sequence.
  389. * It never changes the current stack depth.
  390. */
  391. public void addIndex(int index) {
  392. add(index >> 8);
  393. add(index);
  394. }
  395. /**
  396. * Appends ALOAD or (WIDE) ALOAD_&lt;n&gt;
  397. *
  398. * @param n an index into the local variable array.
  399. */
  400. public void addAload(int n) {
  401. if (n < 4)
  402. addOpcode(42 + n); // aload_<n>
  403. else if (n < 0x100) {
  404. addOpcode(ALOAD); // aload
  405. add(n);
  406. }
  407. else {
  408. addOpcode(WIDE);
  409. addOpcode(ALOAD);
  410. addIndex(n);
  411. }
  412. }
  413. /**
  414. * Appends ASTORE or (WIDE) ASTORE_&lt;n&gt;
  415. *
  416. * @param n an index into the local variable array.
  417. */
  418. public void addAstore(int n) {
  419. if (n < 4)
  420. addOpcode(75 + n); // astore_<n>
  421. else if (n < 0x100) {
  422. addOpcode(ASTORE); // astore
  423. add(n);
  424. }
  425. else {
  426. addOpcode(WIDE);
  427. addOpcode(ASTORE);
  428. addIndex(n);
  429. }
  430. }
  431. /**
  432. * Appends ICONST or ICONST_&lt;n&gt;
  433. *
  434. * @param n the pushed integer constant.
  435. */
  436. public void addIconst(int n) {
  437. if (n < 6 && -2 < n)
  438. addOpcode(3 + n); // iconst_<i> -1..5
  439. else if (n <= 127 && -128 <= n) {
  440. addOpcode(16); // bipush
  441. add(n);
  442. }
  443. else if (n <= 32767 && -32768 <= n) {
  444. addOpcode(17); // sipush
  445. add(n >> 8);
  446. add(n);
  447. }
  448. else
  449. addLdc(constPool.addIntegerInfo(n));
  450. }
  451. /**
  452. * Appends an instruction for pushing zero or null on the stack.
  453. * If the type is void, this method does not append any instruction.
  454. *
  455. * @param type the type of the zero value (or null).
  456. */
  457. public void addConstZero(CtClass type) {
  458. if (type.isPrimitive()) {
  459. if (type == CtClass.longType)
  460. addOpcode(LCONST_0);
  461. else if (type == CtClass.floatType)
  462. addOpcode(FCONST_0);
  463. else if (type == CtClass.doubleType)
  464. addOpcode(DCONST_0);
  465. else if (type == CtClass.voidType)
  466. throw new RuntimeException("void type?");
  467. else
  468. addOpcode(ICONST_0);
  469. }
  470. else
  471. addOpcode(ACONST_NULL);
  472. }
  473. /**
  474. * Appends ILOAD or (WIDE) ILOAD_&lt;n&gt;
  475. *
  476. * @param n an index into the local variable array.
  477. */
  478. public void addIload(int n) {
  479. if (n < 4)
  480. addOpcode(26 + n); // iload_<n>
  481. else if (n < 0x100) {
  482. addOpcode(ILOAD); // iload
  483. add(n);
  484. }
  485. else {
  486. addOpcode(WIDE);
  487. addOpcode(ILOAD);
  488. addIndex(n);
  489. }
  490. }
  491. /**
  492. * Appends ISTORE or (WIDE) ISTORE_&lt;n&gt;
  493. *
  494. * @param n an index into the local variable array.
  495. */
  496. public void addIstore(int n) {
  497. if (n < 4)
  498. addOpcode(59 + n); // istore_<n>
  499. else if (n < 0x100) {
  500. addOpcode(ISTORE); // istore
  501. add(n);
  502. }
  503. else {
  504. addOpcode(WIDE);
  505. addOpcode(ISTORE);
  506. addIndex(n);
  507. }
  508. }
  509. /**
  510. * Appends LCONST or LCONST_&lt;n&gt;
  511. *
  512. * @param n the pushed long integer constant.
  513. */
  514. public void addLconst(long n) {
  515. if (n == 0 || n == 1)
  516. addOpcode(9 + (int)n); // lconst_<n>
  517. else
  518. addLdc2w(n);
  519. }
  520. /**
  521. * Appends LLOAD or (WIDE) LLOAD_&lt;n&gt;
  522. *
  523. * @param n an index into the local variable array.
  524. */
  525. public void addLload(int n) {
  526. if (n < 4)
  527. addOpcode(30 + n); // lload_<n>
  528. else if (n < 0x100) {
  529. addOpcode(LLOAD); // lload
  530. add(n);
  531. }
  532. else {
  533. addOpcode(WIDE);
  534. addOpcode(LLOAD);
  535. addIndex(n);
  536. }
  537. }
  538. /**
  539. * Appends LSTORE or LSTORE_&lt;n&gt;
  540. *
  541. * @param n an index into the local variable array.
  542. */
  543. public void addLstore(int n) {
  544. if (n < 4)
  545. addOpcode(63 + n); // lstore_<n>
  546. else if (n < 0x100) {
  547. addOpcode(LSTORE); // lstore
  548. add(n);
  549. }
  550. else {
  551. addOpcode(WIDE);
  552. addOpcode(LSTORE);
  553. addIndex(n);
  554. }
  555. }
  556. /**
  557. * Appends DCONST or DCONST_&lt;n&gt;
  558. *
  559. * @param d the pushed double constant.
  560. */
  561. public void addDconst(double d) {
  562. if (d == 0.0 || d == 1.0)
  563. addOpcode(14 + (int)d); // dconst_<n>
  564. else
  565. addLdc2w(d);
  566. }
  567. /**
  568. * Appends DLOAD or (WIDE) DLOAD_&lt;n&gt;
  569. *
  570. * @param n an index into the local variable array.
  571. */
  572. public void addDload(int n) {
  573. if (n < 4)
  574. addOpcode(38 + n); // dload_<n>
  575. else if (n < 0x100) {
  576. addOpcode(DLOAD); // dload
  577. add(n);
  578. }
  579. else {
  580. addOpcode(WIDE);
  581. addOpcode(DLOAD);
  582. addIndex(n);
  583. }
  584. }
  585. /**
  586. * Appends DSTORE or (WIDE) DSTORE_&lt;n&gt;
  587. *
  588. * @param n an index into the local variable array.
  589. */
  590. public void addDstore(int n) {
  591. if (n < 4)
  592. addOpcode(71 + n); // dstore_<n>
  593. else if (n < 0x100) {
  594. addOpcode(DSTORE); // dstore
  595. add(n);
  596. }
  597. else {
  598. addOpcode(WIDE);
  599. addOpcode(DSTORE);
  600. addIndex(n);
  601. }
  602. }
  603. /**
  604. * Appends FCONST or FCONST_&lt;n&gt;
  605. *
  606. * @param f the pushed float constant.
  607. */
  608. public void addFconst(float f) {
  609. if (f == 0.0f || f == 1.0f || f == 2.0f)
  610. addOpcode(11 + (int)f); // fconst_<n>
  611. else
  612. addLdc(constPool.addFloatInfo(f));
  613. }
  614. /**
  615. * Appends FLOAD or (WIDE) FLOAD_&lt;n&gt;
  616. *
  617. * @param n an index into the local variable array.
  618. */
  619. public void addFload(int n) {
  620. if (n < 4)
  621. addOpcode(34 + n); // fload_<n>
  622. else if (n < 0x100) {
  623. addOpcode(FLOAD); // fload
  624. add(n);
  625. }
  626. else {
  627. addOpcode(WIDE);
  628. addOpcode(FLOAD);
  629. addIndex(n);
  630. }
  631. }
  632. /**
  633. * Appends FSTORE or FSTORE_&lt;n&gt;
  634. *
  635. * @param n an index into the local variable array.
  636. */
  637. public void addFstore(int n) {
  638. if (n < 4)
  639. addOpcode(67 + n); // fstore_<n>
  640. else if (n < 0x100) {
  641. addOpcode(FSTORE); // fstore
  642. add(n);
  643. }
  644. else {
  645. addOpcode(WIDE);
  646. addOpcode(FSTORE);
  647. addIndex(n);
  648. }
  649. }
  650. /**
  651. * Appends an instruction for loading a value from the
  652. * local variable at the index <code>n</code>.
  653. *
  654. * @param n the index.
  655. * @param type the type of the loaded value.
  656. * @return the size of the value (1 or 2 word).
  657. */
  658. public int addLoad(int n, CtClass type) {
  659. if (type.isPrimitive()) {
  660. if (type == CtClass.booleanType || type == CtClass.charType
  661. || type == CtClass.byteType || type == CtClass.shortType
  662. || type == CtClass.intType)
  663. addIload(n);
  664. else if (type == CtClass.longType) {
  665. addLload(n);
  666. return 2;
  667. }
  668. else if(type == CtClass.floatType)
  669. addFload(n);
  670. else if(type == CtClass.doubleType) {
  671. addDload(n);
  672. return 2;
  673. }
  674. else
  675. throw new RuntimeException("void type?");
  676. }
  677. else
  678. addAload(n);
  679. return 1;
  680. }
  681. /**
  682. * Appends an instruction for storing a value into the
  683. * local variable at the index <code>n</code>.
  684. *
  685. * @param n the index.
  686. * @param type the type of the stored value.
  687. * @return 2 if the type is long or double. Otherwise 1.
  688. */
  689. public int addStore(int n, CtClass type) {
  690. if (type.isPrimitive()) {
  691. if (type == CtClass.booleanType || type == CtClass.charType
  692. || type == CtClass.byteType || type == CtClass.shortType
  693. || type == CtClass.intType)
  694. addIstore(n);
  695. else if (type == CtClass.longType) {
  696. addLstore(n);
  697. return 2;
  698. }
  699. else if (type == CtClass.floatType)
  700. addFstore(n);
  701. else if (type == CtClass.doubleType) {
  702. addDstore(n);
  703. return 2;
  704. }
  705. else
  706. throw new RuntimeException("void type?");
  707. }
  708. else
  709. addAstore(n);
  710. return 1;
  711. }
  712. /**
  713. * Appends instructions for loading all the parameters onto the
  714. * operand stack.
  715. *
  716. * @param offset the index of the first parameter. It is 0
  717. * if the method is static. Otherwise, it is 1.
  718. */
  719. public int addLoadParameters(CtClass[] params, int offset) {
  720. int stacksize = 0;
  721. if (params != null) {
  722. int n = params.length;
  723. for (int i = 0; i < n; ++i)
  724. stacksize += addLoad(stacksize + offset, params[i]);
  725. }
  726. return stacksize;
  727. }
  728. /**
  729. * Appends CHECKCAST.
  730. *
  731. * @param c the type.
  732. */
  733. public void addCheckcast(CtClass c) {
  734. addOpcode(CHECKCAST);
  735. addIndex(constPool.addClassInfo(c));
  736. }
  737. /**
  738. * Appends CHECKCAST.
  739. *
  740. * @param classname a fully-qualified class name.
  741. */
  742. public void addCheckcast(String classname) {
  743. addOpcode(CHECKCAST);
  744. addIndex(constPool.addClassInfo(classname));
  745. }
  746. /**
  747. * Appends INSTANCEOF.
  748. *
  749. * @param classname the class name.
  750. */
  751. public void addInstanceof(String classname) {
  752. addOpcode(INSTANCEOF);
  753. addIndex(constPool.addClassInfo(classname));
  754. }
  755. /**
  756. * Appends GETFIELD.
  757. *
  758. * @param c the class.
  759. * @param name the field name.
  760. * @param type the descriptor of the field type.
  761. *
  762. * @see Descriptor#of(CtClass)
  763. */
  764. public void addGetfield(CtClass c, String name, String type) {
  765. add(GETFIELD);
  766. int ci = constPool.addClassInfo(c);
  767. addIndex(constPool.addFieldrefInfo(ci, name, type));
  768. growStack(Descriptor.dataSize(type) - 1);
  769. }
  770. /**
  771. * Appends GETFIELD.
  772. *
  773. * @param c the fully-qualified class name.
  774. * @param name the field name.
  775. * @param type the descriptor of the field type.
  776. *
  777. * @see Descriptor#of(CtClass)
  778. */
  779. public void addGetfield(String c, String name, String type) {
  780. add(GETFIELD);
  781. int ci = constPool.addClassInfo(c);
  782. addIndex(constPool.addFieldrefInfo(ci, name, type));
  783. growStack(Descriptor.dataSize(type) - 1);
  784. }
  785. /**
  786. * Appends GETSTATIC.
  787. *
  788. * @param c the class
  789. * @param name the field name
  790. * @param type the descriptor of the field type.
  791. *
  792. * @see Descriptor#of(CtClass)
  793. */
  794. public void addGetstatic(CtClass c, String name, String type) {
  795. add(GETSTATIC);
  796. int ci = constPool.addClassInfo(c);
  797. addIndex(constPool.addFieldrefInfo(ci, name, type));
  798. growStack(Descriptor.dataSize(type));
  799. }
  800. /**
  801. * Appends GETSTATIC.
  802. *
  803. * @param c the fully-qualified class name
  804. * @param name the field name
  805. * @param type the descriptor of the field type.
  806. *
  807. * @see Descriptor#of(CtClass)
  808. */
  809. public void addGetstatic(String c, String name, String type) {
  810. add(GETSTATIC);
  811. int ci = constPool.addClassInfo(c);
  812. addIndex(constPool.addFieldrefInfo(ci, name, type));
  813. growStack(Descriptor.dataSize(type));
  814. }
  815. /**
  816. * Appends INVOKESPECIAL.
  817. *
  818. * @param clazz the target class.
  819. * @param name the method name.
  820. * @param returnType the return type.
  821. * @param paramTypes the parameter types.
  822. */
  823. public void addInvokespecial(CtClass clazz, String name,
  824. CtClass returnType, CtClass[] paramTypes) {
  825. String desc = Descriptor.ofMethod(returnType, paramTypes);
  826. addInvokespecial(clazz, name, desc);
  827. }
  828. /**
  829. * Appends INVOKESPECIAL.
  830. *
  831. * @param clazz the target class.
  832. * @param name the method name
  833. * @param desc the descriptor of the method signature.
  834. *
  835. * @see Descriptor#ofMethod(CtClass,CtClass[])
  836. * @see Descriptor#ofConstructor(CtClass[])
  837. */
  838. public void addInvokespecial(CtClass clazz, String name, String desc) {
  839. addInvokespecial(constPool.addClassInfo(clazz), name, desc);
  840. }
  841. /**
  842. * Appends INVOKESPECIAL.
  843. *
  844. * @param clazz the fully-qualified class name.
  845. * @param name the method name
  846. * @param desc the descriptor of the method signature.
  847. *
  848. * @see Descriptor#ofMethod(CtClass,CtClass[])
  849. * @see Descriptor#ofConstructor(CtClass[])
  850. */
  851. public void addInvokespecial(String clazz, String name, String desc) {
  852. addInvokespecial(constPool.addClassInfo(clazz), name, desc);
  853. }
  854. /**
  855. * Appends INVOKESPECIAL.
  856. *
  857. * @param clazz the index of <code>CONSTANT_Class_info</code>
  858. * structure.
  859. * @param name the method name
  860. * @param desc the descriptor of the method signature.
  861. *
  862. * @see Descriptor#ofMethod(CtClass,CtClass[])
  863. * @see Descriptor#ofConstructor(CtClass[])
  864. */
  865. public void addInvokespecial(int clazz, String name, String desc) {
  866. add(INVOKESPECIAL);
  867. addIndex(constPool.addMethodrefInfo(clazz, name, desc));
  868. growStack(Descriptor.dataSize(desc) - 1);
  869. }
  870. /**
  871. * Appends INVOKESTATIC.
  872. *
  873. * @param clazz the target class.
  874. * @param name the method name
  875. * @param returnType the return type.
  876. * @param paramTypes the parameter types.
  877. */
  878. public void addInvokestatic(CtClass clazz, String name,
  879. CtClass returnType, CtClass[] paramTypes) {
  880. String desc = Descriptor.ofMethod(returnType, paramTypes);
  881. addInvokestatic(clazz, name, desc);
  882. }
  883. /**
  884. * Appends INVOKESTATIC.
  885. *
  886. * @param clazz the target class.
  887. * @param name the method name
  888. * @param desc the descriptor of the method signature.
  889. *
  890. * @see Descriptor#ofMethod(CtClass,CtClass[])
  891. */
  892. public void addInvokestatic(CtClass clazz, String name, String desc) {
  893. addInvokestatic(constPool.addClassInfo(clazz), name, desc);
  894. }
  895. /**
  896. * Appends INVOKESTATIC.
  897. *
  898. * @param classname the fully-qualified class name.
  899. * @param name the method name
  900. * @param desc the descriptor of the method signature.
  901. *
  902. * @see Descriptor#ofMethod(CtClass,CtClass[])
  903. */
  904. public void addInvokestatic(String classname, String name, String desc) {
  905. addInvokestatic(constPool.addClassInfo(classname), name, desc);
  906. }
  907. /**
  908. * Appends INVOKESTATIC.
  909. *
  910. * @param clazz the index of <code>CONSTANT_Class_info</code>
  911. * structure.
  912. * @param name the method name
  913. * @param desc the descriptor of the method signature.
  914. *
  915. * @see Descriptor#ofMethod(CtClass,CtClass[])
  916. */
  917. public void addInvokestatic(int clazz, String name, String desc) {
  918. add(INVOKESTATIC);
  919. addIndex(constPool.addMethodrefInfo(clazz, name, desc));
  920. growStack(Descriptor.dataSize(desc));
  921. }
  922. /**
  923. * Appends INVOKEVIRTUAL.
  924. *
  925. * <p>The specified method must not be an inherited method.
  926. * It must be directly declared in the class specified
  927. * in <code>clazz</code>.
  928. *
  929. * @param clazz the target class.
  930. * @param name the method name
  931. * @param returnType the return type.
  932. * @param paramTypes the parameter types.
  933. */
  934. public void addInvokevirtual(CtClass clazz, String name,
  935. CtClass returnType, CtClass[] paramTypes) {
  936. String desc = Descriptor.ofMethod(returnType, paramTypes);
  937. addInvokevirtual(clazz, name, desc);
  938. }
  939. /**
  940. * Appends INVOKEVIRTUAL.
  941. *
  942. * <p>The specified method must not be an inherited method.
  943. * It must be directly declared in the class specified
  944. * in <code>clazz</code>.
  945. *
  946. * @param clazz the target class.
  947. * @param name the method name
  948. * @param desc the descriptor of the method signature.
  949. *
  950. * @see Descriptor#ofMethod(CtClass,CtClass[])
  951. */
  952. public void addInvokevirtual(CtClass clazz, String name, String desc) {
  953. addInvokevirtual(constPool.addClassInfo(clazz), name, desc);
  954. }
  955. /**
  956. * Appends INVOKEVIRTUAL.
  957. *
  958. * <p>The specified method must not be an inherited method.
  959. * It must be directly declared in the class specified
  960. * in <code>classname</code>.
  961. *
  962. * @param classname the fully-qualified class name.
  963. * @param name the method name
  964. * @param desc the descriptor of the method signature.
  965. *
  966. * @see Descriptor#ofMethod(CtClass,CtClass[])
  967. */
  968. public void addInvokevirtual(String classname, String name, String desc) {
  969. addInvokevirtual(constPool.addClassInfo(classname), name, desc);
  970. }
  971. /**
  972. * Appends INVOKEVIRTUAL.
  973. *
  974. * <p>The specified method must not be an inherited method.
  975. * It must be directly declared in the class specified
  976. * by <code>clazz</code>.
  977. *
  978. * @param clazz the index of <code>CONSTANT_Class_info</code>
  979. * structure.
  980. * @param name the method name
  981. * @param desc the descriptor of the method signature.
  982. *
  983. * @see Descriptor#ofMethod(CtClass,CtClass[])
  984. */
  985. public void addInvokevirtual(int clazz, String name, String desc) {
  986. add(INVOKEVIRTUAL);
  987. addIndex(constPool.addMethodrefInfo(clazz, name, desc));
  988. growStack(Descriptor.dataSize(desc) - 1);
  989. }
  990. /**
  991. * Appends INVOKEINTERFACE.
  992. *
  993. * @param clazz the target class.
  994. * @param name the method name
  995. * @param returnType the return type.
  996. * @param paramTypes the parameter types.
  997. * @param count the count operand of the instruction.
  998. */
  999. public void addInvokeinterface(CtClass clazz, String name,
  1000. CtClass returnType, CtClass[] paramTypes,
  1001. int count) {
  1002. String desc = Descriptor.ofMethod(returnType, paramTypes);
  1003. addInvokeinterface(clazz, name, desc, count);
  1004. }
  1005. /**
  1006. * Appends INVOKEINTERFACE.
  1007. *
  1008. * @param clazz the target class.
  1009. * @param name the method name
  1010. * @param desc the descriptor of the method signature.
  1011. * @param count the count operand of the instruction.
  1012. *
  1013. * @see Descriptor#ofMethod(CtClass,CtClass[])
  1014. */
  1015. public void addInvokeinterface(CtClass clazz, String name,
  1016. String desc, int count) {
  1017. addInvokeinterface(constPool.addClassInfo(clazz), name, desc,
  1018. count);
  1019. }
  1020. /**
  1021. * Appends INVOKEINTERFACE.
  1022. *
  1023. * @param classname the fully-qualified class name.
  1024. * @param name the method name
  1025. * @param desc the descriptor of the method signature.
  1026. * @param count the count operand of the instruction.
  1027. *
  1028. * @see Descriptor#ofMethod(CtClass,CtClass[])
  1029. */
  1030. public void addInvokeinterface(String classname, String name,
  1031. String desc, int count) {
  1032. addInvokeinterface(constPool.addClassInfo(classname), name, desc,
  1033. count);
  1034. }
  1035. /**
  1036. * Appends INVOKEINTERFACE.
  1037. *
  1038. * @param clazz the index of <code>CONSTANT_Class_info</code>
  1039. * structure.
  1040. * @param name the method name
  1041. * @param desc the descriptor of the method signature.
  1042. * @param count the count operand of the instruction.
  1043. *
  1044. * @see Descriptor#ofMethod(CtClass,CtClass[])
  1045. */
  1046. public void addInvokeinterface(int clazz, String name,
  1047. String desc, int count) {
  1048. add(INVOKEINTERFACE);
  1049. addIndex(constPool.addInterfaceMethodrefInfo(clazz, name, desc));
  1050. add(count);
  1051. add(0);
  1052. growStack(Descriptor.dataSize(desc) - 1);
  1053. }
  1054. /**
  1055. * Appends LDC or LDC_W. The pushed item is a <code>String</code>
  1056. * object.
  1057. *
  1058. * @param s the character string pushed by LDC or LDC_W.
  1059. */
  1060. public void addLdc(String s) {
  1061. addLdc(constPool.addStringInfo(s));
  1062. }
  1063. /**
  1064. * Appends LDC or LDC_W.
  1065. *
  1066. * @param i index into the constant pool.
  1067. */
  1068. public void addLdc(int i) {
  1069. if (i > 0xFF) {
  1070. addOpcode(LDC_W);
  1071. addIndex(i);
  1072. }
  1073. else {
  1074. addOpcode(LDC);
  1075. add(i);
  1076. }
  1077. }
  1078. /**
  1079. * Appends LDC2_W. The pushed item is a long value.
  1080. */
  1081. public void addLdc2w(long l) {
  1082. addOpcode(LDC2_W);
  1083. addIndex(constPool.addLongInfo(l));
  1084. }
  1085. /**
  1086. * Appends LDC2_W. The pushed item is a double value.
  1087. */
  1088. public void addLdc2w(double d) {
  1089. addOpcode(LDC2_W);
  1090. addIndex(constPool.addDoubleInfo(d));
  1091. }
  1092. /**
  1093. * Appends NEW.
  1094. *
  1095. * @param clazz the class of the created instance.
  1096. */
  1097. public void addNew(CtClass clazz) {
  1098. addOpcode(NEW);
  1099. addIndex(constPool.addClassInfo(clazz));
  1100. }
  1101. /**
  1102. * Appends NEW.
  1103. *
  1104. * @param classname the fully-qualified class name.
  1105. */
  1106. public void addNew(String classname) {
  1107. addOpcode(NEW);
  1108. addIndex(constPool.addClassInfo(classname));
  1109. }
  1110. /**
  1111. * Appends ANEWARRAY.
  1112. *
  1113. * @param classname the qualified class name of the element type.
  1114. */
  1115. public void addAnewarray(String classname) {
  1116. addOpcode(ANEWARRAY);
  1117. addIndex(constPool.addClassInfo(classname));
  1118. }
  1119. /**
  1120. * Appends ICONST and ANEWARRAY.
  1121. *
  1122. * @param clazz the elememnt type.
  1123. * @param length the array length.
  1124. */
  1125. public void addAnewarray(CtClass clazz, int length) {
  1126. addIconst(length);
  1127. addOpcode(ANEWARRAY);
  1128. addIndex(constPool.addClassInfo(clazz));
  1129. }
  1130. /**
  1131. * Appends NEWARRAY for primitive types.
  1132. *
  1133. * @param atype <code>T_BOOLEAN</code>, <code>T_CHAR</code>, ...
  1134. * @see Opcode
  1135. */
  1136. public void addNewarray(int atype, int length) {
  1137. addIconst(length);
  1138. addOpcode(NEWARRAY);
  1139. add(atype);
  1140. }
  1141. /**
  1142. * Appends MULTINEWARRAY.
  1143. *
  1144. * @param clazz the array type.
  1145. * @param dimensions the sizes of all dimensions.
  1146. * @return the length of <code>dimensions</code>.
  1147. */
  1148. public int addMultiNewarray(CtClass clazz, int[] dimensions) {
  1149. int len = dimensions.length;
  1150. for (int i = 0; i < len; ++i)
  1151. addIconst(dimensions[i]);
  1152. growStack(len);
  1153. return addMultiNewarray(clazz, len);
  1154. }
  1155. /**
  1156. * Appends MULTINEWARRAY. The size of every dimension must have been
  1157. * already pushed on the stack.
  1158. *
  1159. * @param clazz the array type.
  1160. * @param dim the number of the dimensions.
  1161. * @return the value of <code>dim</code>.
  1162. */
  1163. public int addMultiNewarray(CtClass clazz, int dim) {
  1164. add(MULTIANEWARRAY);
  1165. addIndex(constPool.addClassInfo(clazz));
  1166. add(dim);
  1167. growStack(1 - dim);
  1168. return dim;
  1169. }
  1170. /**
  1171. * Appends MULTINEWARRAY.
  1172. *
  1173. * @param desc the type descriptor of the created array.
  1174. * @param dim dimensions.
  1175. * @return the value of <code>dim</code>.
  1176. */
  1177. public int addMultiNewarray(String desc, int dim) {
  1178. add(MULTIANEWARRAY);
  1179. addIndex(constPool.addClassInfo(desc));
  1180. add(dim);
  1181. growStack(1 - dim);
  1182. return dim;
  1183. }
  1184. /**
  1185. * Appends PUTFIELD.
  1186. *
  1187. * @param c the target class.
  1188. * @param name the field name.
  1189. * @param desc the descriptor of the field type.
  1190. */
  1191. public void addPutfield(CtClass c, String name, String desc) {
  1192. addPutfield0(c, null, name, desc);
  1193. }
  1194. /**
  1195. * Appends PUTFIELD.
  1196. *
  1197. * @param classname the fully-qualified name of the target class.
  1198. * @param name the field name.
  1199. * @param desc the descriptor of the field type.
  1200. */
  1201. public void addPutfield(String classname, String name, String desc) {
  1202. // if classnaem is null, the target class is THIS.
  1203. addPutfield0(null, classname, name, desc);
  1204. }
  1205. private void addPutfield0(CtClass target, String classname,
  1206. String name, String desc) {
  1207. add(PUTFIELD);
  1208. // target is null if it represents THIS.
  1209. int ci = classname == null ? constPool.addClassInfo(target)
  1210. : constPool.addClassInfo(classname);
  1211. addIndex(constPool.addFieldrefInfo(ci, name, desc));
  1212. growStack(-1 - Descriptor.dataSize(desc));
  1213. }
  1214. /**
  1215. * Appends PUTSTATIC.
  1216. *
  1217. * @param c the target class.
  1218. * @param name the field name.
  1219. * @param desc the descriptor of the field type.
  1220. */
  1221. public void addPutstatic(CtClass c, String name, String desc) {
  1222. addPutstatic0(c, null, name, desc);
  1223. }
  1224. /**
  1225. * Appends PUTSTATIC.
  1226. *
  1227. * @param classname the fully-qualified name of the target class.
  1228. * @param fieldName the field name.
  1229. * @param desc the descriptor of the field type.
  1230. */
  1231. public void addPutstatic(String classname, String fieldName, String desc) {
  1232. // if classname is null, the target class is THIS.
  1233. addPutstatic0(null, classname, fieldName, desc);
  1234. }
  1235. private void addPutstatic0(CtClass target, String classname,
  1236. String fieldName, String desc) {
  1237. add(PUTSTATIC);
  1238. // target is null if it represents THIS.
  1239. int ci = classname == null ? constPool.addClassInfo(target)
  1240. : constPool.addClassInfo(classname);
  1241. addIndex(constPool.addFieldrefInfo(ci, fieldName, desc));
  1242. growStack(-Descriptor.dataSize(desc));
  1243. }
  1244. /**
  1245. * Appends ARETURN, IRETURN, .., or RETURN.
  1246. *
  1247. * @param type the return type.
  1248. */
  1249. public void addReturn(CtClass type) {
  1250. if (type == null)
  1251. addOpcode(RETURN);
  1252. else if (type.isPrimitive()) {
  1253. CtPrimitiveType ptype = (CtPrimitiveType)type;
  1254. addOpcode(ptype.getReturnOp());
  1255. }
  1256. else
  1257. addOpcode(ARETURN);
  1258. }
  1259. /**
  1260. * Appends RET.
  1261. *
  1262. * @param var local variable
  1263. */
  1264. public void addRet(int var) {
  1265. if (var < 0x100) {
  1266. addOpcode(RET);
  1267. add(var);
  1268. }
  1269. else {
  1270. addOpcode(WIDE);
  1271. addOpcode(RET);
  1272. addIndex(var);
  1273. }
  1274. }
  1275. /**
  1276. * Appends instructions for executing
  1277. * <code>java.lang.System.println(<i>message</i>)</code>.
  1278. *
  1279. * @param message printed message.
  1280. */
  1281. public void addPrintln(String message) {
  1282. addGetstatic("java.lang.System", "err", "Ljava/io/PrintStream;");
  1283. addLdc(message);
  1284. addInvokevirtual("java.io.PrintStream",
  1285. "println", "(Ljava/lang/String;)V");
  1286. }
  1287. }