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 43KB

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