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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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><a href="#varargs">7. Varargs</a>
  21. <p><a href="#j2me">8. J2ME</a>
  22. <p><a href="#boxing">9. Boxing/Unboxing
  23. <p><a href="#debug">10. Debug</a>
  24. <p><br>
  25. <a name="intro">
  26. <h2>5. Bytecode level API</h2>
  27. <p>
  28. Javassist also provides lower-level API for directly editing
  29. a class file. To use this level of API, you need detailed
  30. knowledge of the Java bytecode and the class file format
  31. while this level of API allows you any kind of modification
  32. of class files.
  33. <p>
  34. If you want to just produce a simple class file,
  35. <code>javassist.bytecode.ClassFileWriter</code> might provide
  36. the best API for you. It is much faster than
  37. <code>javassist.bytecode.ClassFile</code> although its API
  38. is minimum.
  39. <a name="classfile">
  40. <h3>5.1 Obtaining a <code>ClassFile</code> object</h3>
  41. <p>A <code>javassist.bytecode.ClassFile</code> object represents
  42. a class file. To obtian this object, <code>getClassFile()</code>
  43. in <code>CtClass</code> should be called.
  44. <p>Otherwise, you can construct a
  45. <code>javassist.bytecode.ClassFile</code> directly from a class file.
  46. For example,
  47. <ul><pre>
  48. BufferedInputStream fin
  49. = new BufferedInputStream(new FileInputStream("Point.class"));
  50. ClassFile cf = new ClassFile(new DataInputStream(fin));
  51. </pre></ul>
  52. <p>
  53. This code snippet creats a <code>ClassFile</code> object from
  54. <code>Point.class</code>.
  55. <p>
  56. A <code>ClassFile</code> object can be written back to a
  57. class file. <code>write()</code> in <code>ClassFile</code>
  58. writes the contents of the class file to a given
  59. <code>DataOutputStream</code>.
  60. <p><br>
  61. <a name="member">
  62. <h3>5.2 Adding and removing a member</h3>
  63. <p>
  64. <code>ClassFile</code> provides <code>addField()</code> and
  65. <code>addMethod()</code> for adding a field or a method (note that
  66. a constructor is regarded as a method at the bytecode level).
  67. It also provides <code>addAttribute()</code> for adding an attribute
  68. to the class file.
  69. <p>
  70. Note that <code>FieldInfo</code>, <code>MethodInfo</code>, and
  71. <code>AttributeInfo</code> objects include a link to a
  72. <code>ConstPool</code> (constant pool table) object. The <code>ConstPool</code>
  73. object must be common to the <code>ClassFile</code> object and
  74. a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  75. that is added to that <code>ClassFile</code> object.
  76. In other words, a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
  77. must not be shared among different <code>ClassFile</code> objects.
  78. <p>
  79. To remove a field or a method from a <code>ClassFile</code> object,
  80. you must first obtain a <code>java.util.List</code>
  81. object containing all the fields of the class. <code>getFields()</code>
  82. and <code>getMethods()</code> return the lists. A field or a method can
  83. be removed by calling <code>remove()</code> on the <code>List</code> object.
  84. An attribute can be removed in a similar way.
  85. Call <code>getAttributes()</code> in <code>FieldInfo</code> or
  86. <code>MethodInfo</code> to obtain the list of attributes,
  87. and remove one from the list.
  88. <p><br>
  89. <a name="traverse">
  90. <h3>5.3 Traversing a method body</h3>
  91. <p>
  92. To examine every bytecode instruction in a method body,
  93. <code>CodeIterator</code> is useful. To otbain this object,
  94. do as follows:
  95. <ul><pre>
  96. ClassFile cf = ... ;
  97. MethodInfo minfo = cf.getMethod("move"); // we assume move is not overloaded.
  98. CodeAttribute ca = minfo.getCodeAttribute();
  99. CodeIterator i = ca.iterator();
  100. </pre></ul>
  101. <p>
  102. A <code>CodeIterator</code> object allows you to visit every
  103. bytecode instruction one by one from the beginning to the end.
  104. The following methods are part of the methods declared in
  105. <code>CodeIterator</code>:
  106. <ul>
  107. <li><code>void begin()</code><br>
  108. Move to the first instruction.<br>
  109. <li><code>void move(int index)</code><br>
  110. Move to the instruction specified by the given index.<br>
  111. <li><code>boolean hasNext()</code><br>
  112. Returns true if there is more instructions.<br>
  113. <li><code>int next()</code><br>
  114. Returns the index of the next instruction.<br>
  115. <em>Note that it does not return the opcode of the next
  116. instruction.</em><br>
  117. <li><code>int byteAt(int index)</code><br>
  118. Returns the unsigned 8bit value at the index.<br>
  119. <li><code>int u16bitAt(int index)</code><br>
  120. Returns the unsigned 16bit value at the index.<br>
  121. <li><code>int write(byte[] code, int index)</code><br>
  122. Writes a byte array at the index.<br>
  123. <li><code>void insert(int index, byte[] code)</code><br>
  124. Inserts a byte array at the index.
  125. Branch offsets etc. are automatically adjusted.<br>
  126. </ul>
  127. <p>The following code snippet displays all the instructions included
  128. in a method body:
  129. <ul><pre>
  130. CodeIterator ci = ... ;
  131. while (ci.hasNext()) {
  132. int index = ci.next();
  133. int op = ci.byteAt(index);
  134. System.out.println(Mnemonic.OPCODE[op]);
  135. }
  136. </pre></ul>
  137. <p><br>
  138. <a name="bytecode">
  139. <h3>5.4 Producing a bytecode sequence</h3>
  140. <p>
  141. A <code>Bytecode</code> object represents a sequence of bytecode
  142. instructions. It is a growable array of bytecode.
  143. Here is a sample code snippet:
  144. <ul><pre>
  145. ConstPool cp = ...; // constant pool table
  146. Bytecode b = new Bytecode(cp, 1, 0);
  147. b.addIconst(3);
  148. b.addReturn(CtClass.intType);
  149. CodeAttribute ca = b.toCodeAttribute();
  150. </pre></ul>
  151. <p>
  152. This produces the code attribute representing the following sequence:
  153. <ul><pre>
  154. iconst_3
  155. ireturn
  156. </pre></ul>
  157. <p>
  158. You can also obtain a byte array containing this sequence by
  159. calling <code>get()</code> in <code>Bytecode</code>. The
  160. obtained array can be inserted in another code attribute.
  161. <p>
  162. While <code>Bytecode</code> provides a number of methods for adding a
  163. specific instruction to the sequence, it provides
  164. <code>addOpcode()</code> for adding an 8bit opcode and
  165. <code>addIndex()</code> for adding an index.
  166. The 8bit value of each opcode is defined in the <code>Opcode</code>
  167. interface.
  168. <p>
  169. <code>addOpcode()</code> and other methods for adding a specific
  170. instruction are automatically maintain the maximum stack depth
  171. unless the control flow does not include a branch.
  172. This value can be obtained by calling <code>getMaxStack()</code>
  173. on the <code>Bytecode</code> object.
  174. It is also reflected on the <code>CodeAttribute</code> object
  175. constructed from the <code>Bytecode</code> object.
  176. To recompute the maximum stack depth of a method body,
  177. call <code>computeMaxStack()</code> in <code>CodeAttribute</code>.
  178. <p><br>
  179. <a name="annotation">
  180. <h3>5.5 Annotations (Meta tags)</h3>
  181. <p>Annotations are stored in a class file
  182. as runtime invisible (or visible) annotations attribute.
  183. These attributes can be obtained from <code>ClassFile</code>,
  184. <code>MethodInfo</code>, or <code>FieldInfo</code> objects.
  185. Call <code>getAttribute(AnnotationsAttribute.invisibleTag)</code>
  186. on those objects. For more details, see the javadoc manual
  187. of <code>javassist.bytecode.AnnotationsAttribute</code> class
  188. and the <code>javassist.bytecode.annotation</code> package.
  189. <p>Javassist also let you access annotations by the higher-level
  190. API.
  191. If you want to access annotations through <code>CtClass</code>,
  192. call <code>getAnnotations()</code> in <code>CtClass</code> or
  193. <code>CtBehavior</code>.
  194. <p><br>
  195. <h2><a name="generics">6. Generics</a></h2>
  196. <p>The lower-level API of Javassist fully supports generics
  197. introduced by Java 5. On the other hand, the higher-level
  198. API such as <code>CtClass</code> does not directly support
  199. generics. However, this is not a serious problem for bytecode
  200. transformation.
  201. <p>The generics of Java is implemented by the erasure technique.
  202. After compilation, all type parameters are dropped off. For
  203. example, suppose that your source code declares a parameterized
  204. type <code>Vector&lt;String&gt;</code>:
  205. <ul><pre>
  206. Vector&lt;String&gt; v = new Vector&lt;String&gt();
  207. :
  208. String s = v.get(0);
  209. </pre></ul>
  210. <p>The compiled bytecode is equivalent to the following code:
  211. <ul><pre>
  212. Vector v = new Vector();
  213. :
  214. String s = (String)v.get(0);
  215. </pre></ul>
  216. <p>So when you write a bytecode transformer, you can just drop
  217. off all type parameters. For example, if you have a class:
  218. <ul><pre>
  219. public class Wrapper&lt;T&gt; {
  220. T value;
  221. public Wrapper(T t) { value = t; }
  222. }
  223. </pre></ul>
  224. <p>and want to add an interface <code>Getter&lt;T&gt;</code> to the
  225. class <code>Wrapper&lt;T&gt;</code>:
  226. <ul><pre>
  227. public interface Getter&lt;T&gt; {
  228. T get();
  229. }
  230. </pre></ul>
  231. <p>Then the interface you really have to add is <code>Getter</code>
  232. (the type parameters <code>&lt;T&gt;</code> drops off)
  233. and the method you also have to add to the <code>Wrapper</code>
  234. class is this simple one:
  235. <ul><pre>
  236. public Object get() { return value; }
  237. </pre></ul>
  238. <p>Note that no type parameters are necessary.
  239. <p>However, if you need to make type parameters accessible through reflection
  240. during runtime, you have to add generic signatures to the class file.
  241. For more details, see the API documentation (javadoc) of the
  242. <code>setGenericSignature</code> method in the <code>CtClass</code>.
  243. <p><br>
  244. <h2><a name="varargs">7. Varargs</a></h2>
  245. <p>Currently, Javassist does not directly support varargs. So to make a method with varargs,
  246. you must explicitly set a method modifier. But this is easy.
  247. Suppose that now you want to make the following method:
  248. <ul><pre>
  249. public int length(int... args) { return args.length; }
  250. </pre></ul>
  251. <p>The following code using Javassist will make the method shown above:
  252. <ul><pre>
  253. CtClass cc = /* target class */;
  254. CtMethod m = CtMethod.make("public int length(int[] args) { return args.length; }", cc);
  255. m.setModifiers(m.getModifiers() | Modifier.VARARGS);
  256. cc.addMethod(m);
  257. <pre></ul>
  258. <p>The parameter type <code>int...</code> is changed into <code>int[]</code>
  259. and <code>Modifier.VARARGS</code> is added to the method modifiers.
  260. <p>To call this method, you must write:
  261. <ul><pre>
  262. length(new int[] { 1, 2, 3 });
  263. </pre></ul>
  264. <p>instead of this method call using the varargs mechanism:
  265. <ul><pre>
  266. length(1, 2, 3);
  267. </pre></ul>
  268. <p><br>
  269. <h2><a name="j2me">8. J2ME</a></h2>
  270. <p>If you modify a class file for the J2ME execution environment,
  271. you must perform <it>preverification</it>. Preverifying is basically
  272. producing stack maps, which is similar to stack map tables introduced
  273. into J2SE at JDK 1.6. Javassist maintains the stack maps for J2ME only if
  274. <code>javassist.bytecode.MethodInfo.doPreverify</code> is true.
  275. <p>You can also manually
  276. produce a stack map for a modified method.
  277. For a given method represented by a <code>CtMethod</code> object <code>m</code>,
  278. you can produce a stack map by calling the following methods:
  279. <ul><pre>
  280. m.getMethodInfo().rebuildStackMapForME(cpool);
  281. </pre></ul>
  282. <p>Here, <code>cpool</code> is a <code>ClassPool</code> object, which is
  283. available by calling <code>getClassPool()</code> on a <code>CtClass</code>
  284. object. A <code>ClassPool</code> object is responsible for finding
  285. class files from given class pathes. To obtain all the <code>CtMethod</code>
  286. objects, call the <code>getDeclaredMethods</code> method on a <code>CtClass</code> object.
  287. <p><br>
  288. <h2><a name="boxing">9. Boxing/Unboxing</h2>
  289. <p>Boxing and unboxing in Java are syntactic sugar. There is no bytecode for
  290. boxing or unboxing. So the compiler of Javassist does not support them.
  291. For example, the following statement is valid in Java:
  292. <ul><pre>
  293. Integer i = 3;
  294. </pre></ul>
  295. <p>since boxing is implicitly performed. For Javassist, however, you must explicitly
  296. convert a value type from <code>int</code> to <code>Integer</code>:
  297. <ul><pre>
  298. Integer i = new Integer(3);
  299. </pre></ul>
  300. <p><br>
  301. <h2><a name="debug">10. Debug</h2>
  302. <p>Set <code>CtClass.debugDump</code> to a directory name.
  303. Then all class files modified and generated by Javassist are saved in that
  304. directory. To stop this, set <code>CtClass.debugDump</code> to null.
  305. The default value is null.
  306. <p>For example,
  307. <ul><pre>
  308. CtClass.debugDump = "./dump";
  309. </pre></ul>
  310. <p>All modified class files are saved in <code>./dump</code>.
  311. <p><br>
  312. <a href="tutorial2.html">Previous page</a>
  313. <hr>
  314. Java(TM) is a trademark of Sun Microsystems, Inc.<br>
  315. Copyright (C) 2000-2012 by Shigeru Chiba, All rights reserved.
  316. </body>
  317. </html>