Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tutorial3.html 6.2KB

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