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.

tutorial3.html 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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><a href="#generics">6. Generics</a>
  20. <p><br>
  21. <a name="intro">
  22. <h2>5. Bytecode level API</h2>
  23. <p>
  24. Javassist also provides lower-level API for directly editing
  25. a class file. To use this level of API, you need detailed
  26. knowledge of the Java bytecode and the class file format
  27. while this level of API allows you any kind of modification
  28. of class files.
  29. <a name="classfile">
  30. <h3>5.1 Obtaining a <code>ClassFile</code> object</h3>
  31. <p>A <code>javassist.bytecode.ClassFile</code> object represents
  32. a class file. To obtian this object, <code>getClassFile()</code>
  33. in <code>CtClass</code> should be called.
  34. <p>Otherwise, you can construct a
  35. <code>javassist.bytecode.ClassFile</code> directly from a class file.
  36. For example,
  37. <ul><pre>
  38. BufferedInputStream fin
  39. = new BufferedInputStream(new FileInputStream("Point.class"));
  40. ClassFile cf = new ClassFile(new DataInputStream(fin));
  41. </pre></ul>
  42. <p>
  43. This code snippet creats a <code>ClassFile</code> object from
  44. <code>Point.class</code>.
  45. <p>
  46. A <code>ClassFile</code> object can be written back to a
  47. class file. <code>write()</code> in <code>ClassFile</code>
  48. writes the contents of the class file to a given
  49. <code>DataOutputStream</code>.
  50. <p><br>
  51. <a name="member">
  52. <h3>5.2 Adding and removing a member</h3>
  53. <p>
  54. <code>ClassFile</code> provides <code>addField()</code> and
  55. <code>addMethod()</code> for adding a field or a method (note that
  56. a constructor is regarded as a method at the bytecode level).
  57. It also provides <code>addAttribute()</code> for adding an attribute
  58. to the class file.
  59. <p>
  60. Note that <code>FieldInfo</code>, <code>MethodInfo</code>, and
  61. <code>AttributeInfo</code> objects include a link to a
  62. <code>ConstPool</code> (constant pool table) object. The <code>ConstPool</code>
  63. object must be common to the <code>ClassFile</code> object and
  64. a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  65. that is added to that <code>ClassFile</code> object.
  66. In other words, a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  67. must not be shared among different <code>ClassFile</code> objects.
  68. <p>
  69. To remove a field or a method from a <code>ClassFile</code> object,
  70. you must first obtain a <code>java.util.List</code>
  71. object containing all the fields of the class. <code>getFields()</code>
  72. and <code>getMethods()</code> return the lists. A field or a method can
  73. be removed by calling <code>remove()</code> on the <code>List</code> object.
  74. An attribute can be removed in a similar way.
  75. Call <code>getAttributes()</code> in <code>FieldInfo</code> or
  76. <code>MethodInfo</code> to obtain the list of attributes,
  77. and remove one from the list.
  78. <p><br>
  79. <a name="traverse">
  80. <h3>5.3 Traversing a method body</h3>
  81. <p>
  82. To examine every bytecode instruction in a method body,
  83. <code>CodeIterator</code> is useful. To otbain this object,
  84. do as follows:
  85. <ul><pre>
  86. ClassFile cf = ... ;
  87. MethodInfo minfo = cf.getMethod("move"); // we assume move is not overloaded.
  88. CodeAttribute ca = minfo.getCodeAttribute();
  89. CodeIterator i = ca.iterator();
  90. </pre></ul>
  91. <p>
  92. A <code>CodeIterator</code> object allows you to visit every
  93. bytecode instruction one by one from the beginning to the end.
  94. The following methods are part of the methods declared in
  95. <code>CodeIterator</code>:
  96. <ul>
  97. <li><code>void begin()</code><br>
  98. Move to the first instruction.<br>
  99. <li><code>void move(int index)</code><br>
  100. Move to the instruction specified by the given index.<br>
  101. <li><code>boolean hasNext()</code><br>
  102. Returns true if there is more instructions.<br>
  103. <li><code>int next()</code><br>
  104. Returns the index of the next instruction.<br>
  105. <em>Note that it does not return the opcode of the next
  106. instruction.</em><br>
  107. <li><code>int byteAt(int index)</code><br>
  108. Returns the unsigned 8bit value at the index.<br>
  109. <li><code>int u16bitAt(int index)</code><br>
  110. Returns the unsigned 16bit value at the index.<br>
  111. <li><code>int write(byte[] code, int index)</code><br>
  112. Writes a byte array at the index.<br>
  113. <li><code>void insert(int index, byte[] code)</code><br>
  114. Inserts a byte array at the index.
  115. Branch offsets etc. are automatically adjusted.<br>
  116. </ul>
  117. <p>The following code snippet displays all the instructions included
  118. in a method body:
  119. <ul><pre>
  120. CodeIterator ci = ... ;
  121. while (ci.hasNext()) {
  122. int index = ci.next();
  123. int op = ci.byteAt(index);
  124. System.out.println(Mnemonic.OPCODE[op]);
  125. }
  126. </pre></ul>
  127. <p><br>
  128. <a name="bytecode">
  129. <h3>5.4 Producing a bytecode sequence</h3>
  130. <p>
  131. A <code>Bytecode</code> object represents a sequence of bytecode
  132. instructions. It is a growable array of bytecode.
  133. Here is a sample code snippet:
  134. <ul><pre>
  135. ConstPool cp = ...; // constant pool table
  136. Bytecode b = new Bytecode(cp, 1, 0);
  137. b.addIconst(3);
  138. b.addReturn(CtClass.intType);
  139. CodeAttribute ca = b.toCodeAttribute();
  140. </pre></ul>
  141. <p>
  142. This produces the code attribute representing the following sequence:
  143. <ul><pre>
  144. iconst_3
  145. ireturn
  146. </pre></ul>
  147. <p>
  148. You can also obtain a byte array containing this sequence by
  149. calling <code>get()</code> in <code>Bytecode</code>. The
  150. obtained array can be inserted in another code attribute.
  151. <p>
  152. While <code>Bytecode</code> provides a number of methods for adding a
  153. specific instruction to the sequence, it provides
  154. <code>addOpcode()</code> for adding an 8bit opcode and
  155. <code>addIndex()</code> for adding an index.
  156. The 8bit value of each opcode is defined in the <code>Opcode</code>
  157. interface.
  158. <p>
  159. <code>addOpcode()</code> and other methods for adding a specific
  160. instruction are automatically maintain the maximum stack depth
  161. unless the control flow does not include a branch.
  162. This value can be obtained by calling <code>getMaxStack()</code>
  163. on the <code>Bytecode</code> object.
  164. It is also reflected on the <code>CodeAttribute</code> object
  165. constructed from the <code>Bytecode</code> object.
  166. To recompute the maximum stack depth of a method body,
  167. call <code>computeMaxStack()</code> in <code>CodeAttribute</code>.
  168. <p><br>
  169. <a name="annotation">
  170. <h3>5.5 Annotations (Meta tags)</h3>
  171. <p>Annotations are stored in a class file
  172. as runtime invisible (or visible) annotations attribute.
  173. These attributes can be obtained from <code>ClassFile</code>,
  174. <code>MethodInfo</code>, or <code>FieldInfo</code> objects.
  175. Call <code>getAttribute(AnnotationsAttribute.invisibleTag)</code>
  176. on those objects. For more details, see the javadoc manual
  177. of <code>javassist.bytecode.AnnotationsAttribute</code> class
  178. and the <code>javassist.bytecode.annotation</code> package.
  179. <p>Javassist also let you access annotations by the higher-level
  180. API.
  181. If you want to access annotations through <code>CtClass</code>,
  182. call <code>getAnnotations()</code> in <code>CtClass</code> or
  183. <code>CtBehavior</code>.
  184. <p><br>
  185. <h2><a name="generics">6. Generics</a></h2>
  186. <p>The lower-level API of Javassist fully supports generics
  187. introduced by Java 5. On the other hand, the higher-level
  188. API such as <code>CtClass</code> does not directly support
  189. generics. However, this is not a serious problem for bytecode
  190. transformation.
  191. <p>The generics of Java is implemented by the erasure technique.
  192. After compilation, all type parameters are dropped off. For
  193. example, suppose that your source code declares a parameterized
  194. type <code>Vector&lt;String&gt;</code>:
  195. <ul><pre>
  196. Vector&lt;String&gt; v = new Vector&lt;String&gt();
  197. :
  198. String s = v.get(0);
  199. </pre></ul>
  200. <p>The compiled bytecode is equivalent to the following code:
  201. <ul><pre>
  202. Vector v = new Vector();
  203. :
  204. String s = (String)v.get(0);
  205. </pre></ul>
  206. <p>So when you write a bytecode transformer, you can just drop
  207. off all type parameters. For example, if you have a class:
  208. <ul><pre>
  209. public class Wrapper&lt;T&gt; {
  210. T value;
  211. public Wrapper(T t) { value = t; }
  212. }
  213. </pre></ul>
  214. <p>and want to add an interface <code>Getter&lt;T&gt;</code> to the
  215. class <code>Wrapper&lt;T&gt;</code>:
  216. <ul><pre>
  217. public interface Getter&lt;T&gt; {
  218. T get();
  219. }
  220. </pre></ul>
  221. <p>Then the interface you really have to add is <code>Getter</code>
  222. (the type parameters <code>&lt;T&gt;</code> drops off)
  223. and the method you also have to add to the <code>Wrapper</code>
  224. class is this simple one:
  225. <ul><pre>
  226. public Object get() { return value; }
  227. </pre></ul>
  228. <p>Note that no type parameters are necessary.
  229. <p><br>
  230. <a href="tutorial2.html">Previous page</a>
  231. <hr>
  232. Java(TM) is a trademark of Sun Microsystems, Inc.<br>
  233. Copyright (C) 2000-2007 by Shigeru Chiba, All rights reserved.
  234. </body>
  235. </html>