Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

tutorial3.html 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <title>Javassist Tutorial</title>
  5. <link rel="stylesheet" type="text/css" href="brown.css">
  6. </head>
  7. <body>
  8. <div align="right">Getting Started with Javassist</div>
  9. <div align="left"><a href="tutorial2.html">Previous page</a></div>
  10. <p>
  11. <a href="#intro">5. Bytecode level API</a>
  12. <ul>
  13. <li><a href="#classfile">Obtaining a <code>ClassFile</code> object</a>
  14. <br><li><a href="#member">Adding and removing a member</a>
  15. <br><li><a href="#traverse">Traversing a method body</a>
  16. <br><li><a href="#bytecode">Producing a bytecode sequence</a>
  17. <br><li><a href="#annotation">Annotations (Meta tags)</a>
  18. </ul>
  19. <p><br>
  20. <a name="intro">
  21. <h2>5. Bytecode level API</h2>
  22. <p>
  23. Javassist also provides lower-level API for directly editing
  24. a class file. To use this level of API, you need detailed
  25. knowledge of the Java bytecode and the class file format
  26. while this level of API allows you any kind of modification
  27. of class files.
  28. <a name="classfile">
  29. <h3>5.1 Obtaining a <code>ClassFile</code> object</h3>
  30. <p>A <code>javassist.bytecode.ClassFile</code> object represents
  31. a class file. To obtian this object, <code>getClassFile()</code>
  32. in <code>CtClass</code> should be called.
  33. <p>Otherwise, you can construct a
  34. <code>javassist.bytecode.ClassFile</code> directly from a class file.
  35. For example,
  36. <ul><pre>
  37. BufferedInputStream fin
  38. = new BufferedInputStream(new FileInputStream("Point.class"));
  39. ClassFile cf = new ClassFile(new DataInputStream(fin));
  40. </pre></ul>
  41. <p>
  42. This code snippet creats a <code>ClassFile</code> object from
  43. <code>Point.class</code>.
  44. <p>
  45. A <code>ClassFile</code> object can be written back to a
  46. class file. <code>write()</code> in <code>ClassFile</code>
  47. writes the contents of the class file to a given
  48. <code>DataOutputStream</code>.
  49. <p><br>
  50. <a name="member">
  51. <h3>5.2 Adding and removing a member</h3>
  52. <p>
  53. <code>ClassFile</code> provides <code>addField()</code> and
  54. <code>addMethod()</code> for adding a field or a method (note that
  55. a constructor is regarded as a method at the bytecode level).
  56. It also provides <code>addAttribute()</code> for adding an attribute
  57. to the class file.
  58. <p>
  59. Note that <code>FieldInfo</code>, <code>MethodInfo</code>, and
  60. <code>AttributeInfo</code> objects include a link to a
  61. <code>ConstPool</code> (constant pool table) object. The <code>ConstPool</code>
  62. object must be common to the <code>ClassFile</code> object and
  63. a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  64. that is added to that <code>ClassFile</code> object.
  65. In other words, a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  66. must not be shared among different <code>ClassFile</code> objects.
  67. <p>
  68. To remove a field or a method from a <code>ClassFile</code> object,
  69. you must first obtain a <code>java.util.List</code>
  70. object containing all the fields of the class. <code>getFields()</code>
  71. and <code>getMethods()</code> return the lists. A field or a method can
  72. be removed by calling <code>remove()</code> on the <code>List</code> object.
  73. An attribute can be removed in a similar way.
  74. Call <code>getAttributes()</code> in <code>FieldInfo</code> or
  75. <code>MethodInfo</code> to obtain the list of attributes,
  76. and remove one from the list.
  77. <p><br>
  78. <a name="traverse">
  79. <h3>5.3 Traversing a method body</h3>
  80. <p>
  81. To examine every bytecode instruction in a method body,
  82. <code>CodeIterator</code> is useful. To otbain this object,
  83. do as follows:
  84. <ul><pre>
  85. ClassFile cf = ... ;
  86. MethodInfo minfo = cf.getMethod("move"); // we assume move is not overloaded.
  87. CodeAttribute ca = minfo.getCodeAttribute();
  88. CodeIterator i = ca.iterator();
  89. </pre></ul>
  90. <p>
  91. A <code>CodeIterator</code> object allows you to visit every
  92. bytecode instruction one by one from the beginning to the end.
  93. The following methods are part of the methods declared in
  94. <code>CodeIterator</code>:
  95. <ul>
  96. <li><code>void begin()</code><br>
  97. Move to the first instruction.<br>
  98. <li><code>void move(int index)</code><br>
  99. Move to the instruction specified by the given index.<br>
  100. <li><code>boolean hasNext()</code><br>
  101. Returns true if there is more instructions.<br>
  102. <li><code>int next()</code><br>
  103. Returns the index of the next instruction.<br>
  104. <em>Note that it does not return the opcode of the next
  105. instruction.</em><br>
  106. <li><code>int byteAt(int index)</code><br>
  107. Returns the unsigned 8bit value at the index.<br>
  108. <li><code>int u16bitAt(int index)</code><br>
  109. Returns the unsigned 16bit value at the index.<br>
  110. <li><code>int write(byte[] code, int index)</code><br>
  111. Writes a byte array at the index.<br>
  112. <li><code>void insert(int index, byte[] code)</code><br>
  113. Inserts a byte array at the index.
  114. Branch offsets etc. are automatically adjusted.<br>
  115. </ul>
  116. <p>The following code snippet displays all the instructions included
  117. in a method body:
  118. <ul><pre>
  119. CodeIterator ci = ... ;
  120. while (ci.hasNext()) {
  121. int index = ci.next();
  122. int op = ci.byteAt(index);
  123. System.out.println(Mnemonic.OPCODE[op]);
  124. }
  125. </pre></ul>
  126. <p><br>
  127. <a name="bytecode">
  128. <h3>5.4 Producing a bytecode sequence</h3>
  129. <p>
  130. A <code>Bytecode</code> object represents a sequence of bytecode
  131. instructions. It is a growable array of bytecode.
  132. Here is a sample code snippet:
  133. <ul><pre>
  134. ConstPool cp = ...; // constant pool table
  135. Bytecode b = new Bytecode(cp, 1, 0);
  136. b.addIconst(3);
  137. b.addReturn(CtClass.intType);
  138. CodeAttribute ca = b.toCodeAttribute();
  139. </pre></ul>
  140. <p>
  141. This produces the code attribute representing the following sequence:
  142. <ul><pre>
  143. iconst_3
  144. ireturn
  145. </pre></ul>
  146. <p>
  147. You can also obtain a byte array containing this sequence by
  148. calling <code>get()</code> in <code>Bytecode</code>. The
  149. obtained array can be inserted in another code attribute.
  150. <p>
  151. While <code>Bytecode</code> provides a number of methods for adding a
  152. specific instruction to the sequence, it provides
  153. <code>addOpcode()</code> for adding an 8bit opcode and
  154. <code>addIndex()</code> for adding an index.
  155. The 8bit value of each opcode is defined in the <code>Opcode</code>
  156. interface.
  157. <p>
  158. <code>addOpcode()</code> and other methods for adding a specific
  159. instruction are automatically maintain the maximum stack depth
  160. unless the control flow does not include a branch.
  161. This value can be obtained by calling <code>getMaxStack()</code>
  162. on the <code>Bytecode</code> object.
  163. It is also reflected on the <code>CodeAttribute</code> object
  164. constructed from the <code>Bytecode</code> object.
  165. To recompute the maximum stack depth of a method body,
  166. call <code>computeMaxStack()</code> in <code>CodeAttribute</code>.
  167. <p><br>
  168. <a name="annotation">
  169. <h3>5.5 Annotations (Meta tags)</h3>
  170. <p>Annotations are stored in a class file
  171. as runtime invisible (or visible) annotations attribute.
  172. These attributes can be obtained from <code>ClassFile</code>,
  173. <code>MethodInfo</code>, or <code>FieldInfo</code> objects.
  174. Call <code>getAttribute(AnnotationsAttribute.invisibleTag)</code>
  175. on those objects. For more details, see the javadoc manual
  176. of <code>javassist.bytecode.AnnotationsAttribute</code> class
  177. and the <code>javassist.bytecode.annotation</code> package.
  178. <p><br>
  179. <a href="tutorial2.html">Previous page</a>
  180. <hr>
  181. Java(TM) is a trademark of Sun Microsystems, Inc.<br>
  182. Copyright (C) 2000-2005 by Shigeru Chiba, All rights reserved.
  183. </body>
  184. </html>