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.

tutorial2.html 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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="tutorial.html">Previous page</a></div>
  10. <a name="intro">
  11. <h2>5. Introspection and customization</h2>
  12. <p><code>CtClass</code> provides methods for introspection. The
  13. introspective ability of Javassist is compatible with that of
  14. the Java reflection API. <code>CtClass</code> provides
  15. <code>getName()</code>, <code>getSuperclass()</code>,
  16. <code>getMethods()</code>, and so on.
  17. <code>CtClass</code> also provides methods for modifying a class
  18. definition. It allows to add a new field, constructor, and method.
  19. Instrumenting a method body is also possible.
  20. <p><hr width="40%">
  21. <ul>
  22. Javassist does not allow to remove a method or field, but it allows
  23. to change the name. So if a method is not necessary any more, it should be
  24. renamed and changed to be a private method by calling
  25. <code>setName()</code>
  26. and <code>setModifiers()</code> declared in <code>CtMethod</code>.
  27. <p>Javassist does not allow to add an extra parameter to an existing
  28. method, either. Instead of doing that, a new method receiving the
  29. extra parameter as well as the other parameters should be added to the
  30. same class. For example, if you want to add an extra <code>int</code>
  31. parameter <code>newZ</code> to a method:
  32. <ul><pre>void move(int newX, int newY) { x = newX; y = newY; }</pre></ul>
  33. <p>in a <code>Point</code> class, then you should add the following
  34. method to the <code>Point</code> class:
  35. <ul><pre>void move(int newX, int newY, int newZ) {
  36. // do what you want with newZ.
  37. move(newX, newY);
  38. }</pre></ul>
  39. </ul>
  40. <p><hr width="40%">
  41. <p>Javassist also provides low-level API for directly editing a raw
  42. class file. For example, <code>getClassFile()</code> in
  43. <code>CtClass</code> returns a <code>ClassFile</code> object
  44. representing a raw class file. <code>getMethodInfo()</code> in
  45. <code>CtMethod</code> returns a <code>MethodInfo</code> object
  46. representing a <code>method_info</code> structure included in a class
  47. file. The low-level API uses the vocabulary from the Java Virtual
  48. machine specification. The users must have the knowledge about class
  49. files and bytecode. For more details, the users should see the
  50. <code>javassist.bytecode</code> package.
  51. <p><br>
  52. <h3>5.1 Inserting source text at the beginning/end of a method body</h3>
  53. <p><code>CtMethod</code> and <code>CtConstructor</code> provide
  54. methods <code>insertBefore()</code>, <code>insertAfter()</code>, and
  55. <code>addCatch()</code>. They are used for inserting a code fragment
  56. into the body of an existing method. The users can specify those code
  57. fragments with <em>source text</em> written in Java.
  58. Javassist includes a simple Java compiler for processing source
  59. text. It receives source text
  60. written in Java and compiles it into Java bytecode, which will be
  61. <em>inlined</em> into a method body.
  62. <p>The methods <code>insertBefore()</code>, <code>insertAfter()</code>, and
  63. <code>addCatch()</code> receives a <code>String</code> object representing
  64. a statement or a block. A statement is a single control structure like
  65. <code>if</code> and <code>while</code> or an expression ending with
  66. a semi colon (<code>;</code>). A block is a set of
  67. statements surrounded with braces <code>{}</code>.
  68. Hence each of the following lines is an example of valid statement or block:
  69. <ul><pre>System.out.println("Hello");
  70. { System.out.println("Hello"); }
  71. if (i < 0) { i = -i; }
  72. </pre></ul>
  73. <p>The statement and the block can refer to fields and methods.
  74. However, they <em>cannot refer to local variables</em> declared in the
  75. method that they are inserted into.
  76. They can refer to the parameters
  77. to the method although they must use different names
  78. <code>$0</code>, <code>$1</code>, <code>$2</code>, ... described
  79. below. Declaring a local variable in the block is allowed.
  80. <!--
  81. <p><center><table border=8 cellspacing=0 bordercolor="#cfcfcf">
  82. <tr><td bgcolor="#cfcfcf">
  83. <b>Tip:</b>
  84. <br>&nbsp&nbsp&nbsp Local variables are not accessible.&nbsp&nbsp
  85. </td></tr>
  86. </table></center>
  87. -->
  88. <p>The <code>String</code> object passed to the methods
  89. <code>insertBefore()</code>, <code>insertAfter()</code>, and
  90. <code>addCatch()</code> are compiled by
  91. the compiler included in Javassist.
  92. Since the compiler supports language extensions,
  93. several identifiers starting with <code>$</code>
  94. have special meaning:
  95. <ul><table border=0>
  96. <tr>
  97. <td><code>$0</code>, <code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  98. <td>Actual parameters</td>
  99. </tr>
  100. <tr>
  101. <td><code>$args</code></td>
  102. <td>An array of parameters.
  103. The type of <code>$args</code> is <code>Object[]</code>.
  104. </td>
  105. </tr>
  106. <tr>
  107. <td><code>$$</code></td>
  108. <td rowspan=2>All actual parameters.<br>
  109. For example, <code>m($$)</code> is equivalent to
  110. <code>m($1,$2,</code>...<code>)</code></td>
  111. </tr>
  112. <tr><td>&nbsp</td></tr>
  113. <tr>
  114. <td><code>$cflow(</code>...<code>)</code></td>
  115. <td><code>cflow</code> variable</td>
  116. </tr>
  117. <tr>
  118. <td><code>$r</code></td>
  119. <td>The result type. It is used in a cast expression.</td>
  120. </tr>
  121. <tr>
  122. <td><code>$w</code></td>
  123. <td>The wrapper type. It is used in a cast expression.</td>
  124. </tr>
  125. <tr>
  126. <td><code>$_</code></td>
  127. <td>The resulting value</td>
  128. </tr>
  129. <tr>
  130. <td><code>$sig</code></td>
  131. <td>An array of <code>java.lang.Class</code> objects representing
  132. the formal parameter types.
  133. </td>
  134. </tr>
  135. <tr>
  136. <td><code>$type</code></td>
  137. <td>A <code>java.lang.Class</code> object representing
  138. the formal result type.</td>
  139. </tr>
  140. <tr>
  141. <td><code>$class</code></td>
  142. <td>A <code>java.lang.Class</code> object representing
  143. the class currently edited.</td>
  144. </tr>
  145. </table>
  146. </ul>
  147. <h4>$0, $1, $2, ...</h4>
  148. <p>The parameters passed to the methods <code>insertBefore()</code>,
  149. <code>insertAfter()</code>, and <code>addCatch()</code>
  150. are accessible with
  151. <code>$0</code>, <code>$1</code>, <code>$2</code>, ... instead of
  152. the original parameter names.
  153. <code>$1</code> represents the
  154. first parameter, <code>$2</code> represents the second parameter, and
  155. so on. The types of those variables are identical to the parameter
  156. types.
  157. <code>$0</code> is
  158. equivalent to <code>this</code>. If the method is static,
  159. <code>$0</code> is not available.
  160. <p>These variables are used as following. Suppose that a class
  161. <code>Point</code>:
  162. <pre><ul>class Point {
  163. int x, y;
  164. void move(int dx, int dy) { x += dx; y += dy; }
  165. }
  166. </ul></pre>
  167. <p>To print the values of <code>dx</code> and <code>dy</code>
  168. whenever the method <code>move()</code> is called, execute this
  169. program:
  170. <ul><pre>ClassPool pool = ClassPool.getDefault();
  171. CtClass cc = pool.get("Point");
  172. CtMethod m = cc.getDeclaredMethod("move");
  173. m.insertBefore("{ System.out.println($1); System.out.println($2); }");
  174. cc.writeFile();
  175. </pre></ul>
  176. <p>Note that the source text passed to <code>insertBefore()</code> is
  177. surrounded with braces <code>{}</code>.
  178. <code>insertBefore()</code> accepts only a single statement or a block
  179. surrounded with braces.
  180. <p>The definition of the class <code>Point</code> after the
  181. modification is like this:
  182. <pre><ul>class Point {
  183. int x, y;
  184. void move(int dx, int dy) {
  185. { System.out.println(dx); System.out.println(dy); }
  186. x += dx; y += dy;
  187. }
  188. }
  189. </ul></pre>
  190. <p><code>$1</code> and <code>$2</code> are replaced with
  191. <code>dx</code> and <code>dy</code>, respectively.
  192. <p><code>$1</code>, <code>$2</code>, <code>$3</code> ... are
  193. updatable. If a new value is assigend to one of those variables,
  194. then the value of the parameter represented by that variable is
  195. also updated.
  196. <h4>$args</h4>
  197. <p>The variable <code>$args</code> represents an array of all the
  198. parameters. The type of that variable is an array of class
  199. <code>Object</code>. If a parameter type is a primitive type such as
  200. <code>int</code>, then the parameter value is converted into a wrapper
  201. object such as <code>java.lang.Integer</code> to store in
  202. <code>$args</code>. Thus, <code>$args[0]</code> is equivalent to
  203. <code>$1</code> unless the type of the first parameter is a primitive
  204. type. Note that <code>$args[0]</code> is not equivalent to
  205. <code>$0</code>; <code>$0</code> represents <code>this</code>.
  206. <p>If an array of <code>Object</code> is assigned to
  207. <code>$args</code>, then each element of that array is
  208. assigned to each parameter. If a parameter type is a primitive
  209. type, the type of the corresponding element must be a wrapper type.
  210. The value is converted from the wrapper type to the primitive type
  211. before it is assigned to the parameter.
  212. <h4>$$</h4>
  213. <p>The variable <code>$$</code> is abbreviation of a list of
  214. all the parameters separated by commas.
  215. For example, if the number of the parameters
  216. to method <code>move()</code> is three, then
  217. <ul><pre>move($$)</pre></ul>
  218. <p>is equivalent to this:
  219. <ul><pre>move($1, $2, $3)</pre></ul>
  220. <p>If <code>move()</code> does not take any parameters,
  221. then <code>move($$)</code> is
  222. equivalent to <code>move()</code>.
  223. <p><code>$$</code> can be used with another method.
  224. If you write an expression:
  225. <ul><pre>exMove($$, context)</pre></ul>
  226. <p>then this expression is equivalent to:
  227. <ul><pre>exMove($1, $2, $3, context)</pre></ul>
  228. <p>Note that <code>$$</code> enables generic notation of method call
  229. with respect to the number of parameters.
  230. It is typically used with <code>$proceed</code> shown later.
  231. <h4>$cflow</h4>
  232. <p><code>$cflow</code> means "control flow".
  233. This read-only variable returns the depth of the recursive calls
  234. to a specific method.
  235. <p>Suppose that the method shown below is represented by a
  236. <code>CtMethod</code> object <code>cm</code>:
  237. <ul><pre>int fact(int n) {
  238. if (n <= 1)
  239. return n;
  240. else
  241. return n * fact(n - 1);
  242. }</pre></ul>
  243. <p>To use <code>$cflow</code>, first declare that <code>$cflow</code>
  244. is used for monitoring calls to the method <code>fact()</code>:
  245. <ul><pre>CtMethod cm = ...;
  246. cm.useCflow("fact");</pre></ul>
  247. <p>The parameter to <code>useCflow()</code> is the identifier of the
  248. declared <code>$cflow</code> variable. Any valid Java name can be
  249. used as the identifier. Since the identifier can also include
  250. <code>.</code> (dot), for example, <code>"my.Test.fact"</code>
  251. is a valid identifier.
  252. <p>Then, <code>$cflow(fact)</code> represents the depth of the
  253. recursive calls to the method specified by <code>cm</code>. The value
  254. of <code>$cflow(fact)</code> is 0 (zero) when the method is
  255. first called whereas it is 1 when the method is recursively called
  256. within the method. For example,
  257. <ul><pre>
  258. cm.insertBefore("if ($cflow(fact) == 0)"
  259. + " System.out.println(\"fact \" + $1);");
  260. </pre></ul>
  261. <p>translates the method <code>fact()</code> so that it shows the
  262. parameter. Since the value of <code>$cflow(fact)</code> is checked,
  263. the method <code>fact()</code> does not show the parameter if it is
  264. recursively called within <code>fact()</code>.
  265. <p>The value of <code>$cflow</code> is the number of stack frames
  266. associated with the specified method <code>cm</code>
  267. under the current topmost
  268. stack frame for the current thread. <code>$cflow</code> is also
  269. accessible within a method different from the specified method
  270. <code>cm</code>.
  271. <h4>$r</h4>
  272. <p><code>$r</code> represents the result type (return type) of the method.
  273. It must be used as the cast type in a cast expression.
  274. For example, this is a typical use:
  275. <ul><pre>Object result = ... ;
  276. $_ = ($r)result;</pre></ul>
  277. <p>If the result type is a primitive type, then <code>($r)</code>
  278. follows special semantics. First, if the operand type of the cast
  279. expression is a primitive type, <code>($r)</code> works as a normal
  280. cast operator to the result type.
  281. On the other hand, if the operand type is a wrapper type,
  282. <code>($r)</code> converts from the wrapper type to the result type.
  283. For example, if the result type is <code>int</code>, then
  284. <code>($r)</code> converts from <code>java.lang.Integer</code> to
  285. <code>int</code>.
  286. <p>If the result type is <code>void</code>, then
  287. <code>($r)</code> does not convert a type; it does nothing.
  288. However, if the operand is a call to a <code>void</code> method,
  289. then <code>($r)</code> results in <code>null</code>. For example,
  290. if the result type is <code>void</code> and
  291. <code>foo()</code> is a <code>void</code> method, then
  292. <ul><pre>$_ = ($r)foo();</pre></ul>
  293. <p>is a valid statement.
  294. <p>The cast operator <code>($r)</code> is also useful in a
  295. <code>return</code> statement. Even if the result type is
  296. <code>void</code>, the following <code>return</code> statement is valid:
  297. <ul><pre>return ($r)result;</pre></ul>
  298. <p>Here, <code>result</code> is some local variable.
  299. Since <code>($r)</code> is specified, the resulting value is
  300. discarded.
  301. This <code>return</code> statement is regarded as the equivalent
  302. of the <code>return</code> statement without a resulting value:
  303. <ul><pre>return;</pre></ul>
  304. <h4>$w</h4>
  305. <p><code>$w</code> represents a wrapper type.
  306. It must be used as the cast type in a cast expression.
  307. <code>($w)</code> converts from a primitive type to the corresponding
  308. wrapper type.
  309. The following code is an example:
  310. <ul><pre>Integer i = ($w)5;</pre></ul>
  311. <p>The selected wrapper type depends on the type of the expression
  312. following <code>($w)</code>. If the type of the expression is
  313. <code>double</code>, then the wrapper type is <code>java.lang.Double</code>.
  314. <p>If the type of the expression following <code>($w)</code> is not
  315. a primitive type, then <code>($w)</code> does nothing.
  316. <h4>$_</h4>
  317. <p><code>insertAfter()</code> in <code>CtMethod</code> and
  318. <code>CtConstructor</code> inserts the
  319. compiled code at the end of the method. In the statement given to
  320. <code>insertAfter()</code>, not only the variables shown above such as
  321. <code>$0</code>, <code>$1</code>, ... but also <code>$_</code> is
  322. available.
  323. <p>The variable <code>$_</code> represents the resulting value of the
  324. method. The type of that variable is the type of the result type (the
  325. return type) of the method. If the result type is <code>void</code>,
  326. then the type of <code>$_</code> is <code>Object</code> and the value
  327. of <code>$_</code> is <code>null</code>.
  328. <p>Although the compiled code inserted by <code>insertAfter()</code>
  329. is executed just before the control normally returns from the method,
  330. it can be also executed when an exception is thrown from the method.
  331. To execute it when an exception is thrown, the second parameter
  332. <code>asFinally</code> to <code>insertAfter()</code> must be
  333. <code>true</code>.
  334. <p>If an exception is thrown, the compiled code inserted by
  335. <code>insertAfter()</code> is executed as a <code>finally</code>
  336. clause. The value of <code>$_</code> is <code>0</code> or
  337. <code>null</code> in the compiled code. After the execution of the
  338. compiled code terminates, the exception originally thrown is re-thrown
  339. to the caller. Note that the value of <code>$_</code> is never thrown
  340. to the caller; it is rather discarded.
  341. <h4>$sig</h4>
  342. <p>The value of <code>$sig</code> is an array of
  343. <code>java.lang.Class</code> objects that represent the formal
  344. parameter types in declaration order.
  345. <h4>$type</h4>
  346. <p>The value of <code>$type</code> is an <code>java.lang.Class</code>
  347. object representing the formal type of the result value. This
  348. variable is available only in <code>insertAfter()</code> in
  349. <code>CtMethod</code> and <code>CtConstructor</code>.
  350. <h4>$class</h4>
  351. <p>The value of <code>$class</code> is an <code>java.lang.Class</code>
  352. object representing the class in which the edited method is declared.
  353. <h4>addCatch()</h4>
  354. <p><code>addCatch()</code> inserts a code fragment into a method body
  355. so that the code fragment is executed when the method body throws
  356. an exception and the control returns to the caller. In the source
  357. text representing the inserted code fragment, the exception value
  358. is referred to with the special variable <code>$e</code>.
  359. <p>For example, this program:
  360. <ul><pre>
  361. CtMethod m = ...;
  362. CtClass etype = ClassPool.getDefault().get("java.io.IOException");
  363. m.addCatch("{ System.out.println($e); throw $e; }", etype);
  364. </pre></ul>
  365. <p>translates the method body represented by <code>m</code> into
  366. something like this:
  367. <ul><pre>
  368. try {
  369. <font face="serif"><em>the original method body</em></font>
  370. }
  371. catch (java.io.IOException e) {
  372. System.out.println(e);
  373. throw e;
  374. }
  375. </pre></ul>
  376. <p>Note that the inserted code fragment must end with a
  377. <code>throw</code> or <code>return</code> statement.
  378. <p><br>
  379. <h3>5.2 Modifying a method body</h3>
  380. <p><code>CtMethod</code> and <code>CtConstructor</code> provide
  381. <code>setBody()</code> for substituting a whole
  382. method body. They compile the given source text into Java bytecode
  383. and substitutes it for the original method body. If the given source
  384. text is <code>null</code>, the substituted body includes only a
  385. <code>return</code> statement, which returns zero or null unless the
  386. result type is <code>void</code>.
  387. <p>In the source text given to <code>setBody()</code>, the identifiers
  388. starting with <code>$</code> have special meaning
  389. <ul><table border=0>
  390. <tr>
  391. <td><code>$0</code>, <code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  392. <td>Actual parameters</td>
  393. </tr>
  394. <tr>
  395. <td><code>$args</code></td>
  396. <td>An array of parameters.
  397. The type of <code>$args</code> is <code>Object[]</code>.
  398. </td>
  399. </tr>
  400. <tr>
  401. <td><code>$$</code></td>
  402. <td>All actual parameters.<br>
  403. </tr>
  404. <tr>
  405. <td><code>$cflow(</code>...<code>)</code></td>
  406. <td><code>cflow</code> variable</td>
  407. </tr>
  408. <tr>
  409. <td><code>$r</code></td>
  410. <td>The result type. It is used in a cast expression.</td>
  411. </tr>
  412. <tr>
  413. <td><code>$w</code></td>
  414. <td>The wrapper type. It is used in a cast expression.</td>
  415. </tr>
  416. <tr>
  417. <td><code>$sig</code></td>
  418. <td>An array of <code>java.lang.Class</code> objects representing
  419. the formal parameter types.
  420. </td>
  421. </tr>
  422. <tr>
  423. <td><code>$type</code></td>
  424. <td>A <code>java.lang.Class</code> object representing
  425. the formal result type.</td>
  426. </tr>
  427. <tr>
  428. <td><code>$class</code></td>
  429. <td>A <code>java.lang.Class</code> object representing
  430. the class currently edited.</td>
  431. </tr>
  432. </table>
  433. </ul>
  434. Note that <code>$_</code> is not available.
  435. <p>Javassist allows modifying only an expression included in a method body.
  436. <code>javassist.expr.ExprEditor</code> is a class
  437. for replacing an expression in a method body.
  438. The users can define a subclass of <code>ExprEditor</code>
  439. to specify how an expression is modified.
  440. <p>To run an <code>ExprEditor</code> object, the users must
  441. call <code>instrument()</code> in <code>CtMethod</code> or
  442. <code>CtClass</code>.
  443. For example,
  444. <ul><pre>
  445. CtMethod cm = ... ;
  446. cm.instrument(
  447. new ExprEditor() {
  448. public void edit(MethodCall m)
  449. throws CannotCompileException
  450. {
  451. if (m.getClassName().equals("Point")
  452. && m.getMethodName().equals("move"))
  453. m.replace("{ $1 = 0; $_ = $proceed($$); }");
  454. }
  455. });
  456. </pre></ul>
  457. <p>searches the method body represented by <code>cm</code> and
  458. replaces all calls to <code>move()</code> in class <code>Point</code>
  459. with a block:
  460. <ul><pre>{ $1 = 0; $_ = $proceed($$); }
  461. </pre></ul>
  462. <p>so that the first parameter to <code>move()</code> is always 0.
  463. Note that the substituted code is not an expression but
  464. a statement or a block.
  465. <p>The method <code>instrument()</code> searches a method body.
  466. If it finds an expression such as a method call, field access, and object
  467. creation, then it calls <code>edit()</code> on the given
  468. <code>ExprEditor</code> object. The parameter to <code>edit()</code>
  469. is an object representing the found expression. The <code>edit()</code>
  470. method can inspect and replace the expression through that object.
  471. <p>Calling <code>replace()</code> on the parameter to <code>edit()</code>
  472. substitutes the given statement or block for the expression. If the given
  473. block is an empty block, that is, if <code>replace("{}")</code>
  474. is executed, then the expression is removed from the method body.
  475. If you want to insert a statement (or a block) before/after the
  476. expression, a block like the following should be passed to
  477. <code>replace()</code>:
  478. <ul><pre>
  479. { <em>before-statements;</em>
  480. $_ = $proceed($$);
  481. <em>after-statements;</em> }
  482. </pre></ul>
  483. <p>whichever the expression is either a method call, field access,
  484. object creation, or others. The second statement could be:
  485. <ul><pre>$_ = $proceed();</pre></ul>
  486. <p>if the expression is read access, or
  487. <ul><pre>$proceed($$);</pre></ul>
  488. <p>if the expression is write access.
  489. <h4>javassist.expr.MethodCall</h4>
  490. <p>A <code>MethodCall</code> object represents a method call.
  491. The method <code>replace()</code> in
  492. <code>MethodCall</code> substitutes a statement or
  493. a block for the method call.
  494. It receives source text representing the substitued statement or
  495. block, in which the identifiers starting with <code>$</code>
  496. have special meaning as in the source text passed to
  497. <code>insertBefore()</code>.
  498. <ul><table border=0>
  499. <tr>
  500. <td><code>$0</code></td>
  501. <td rowspan=3>
  502. The target object of the method call.<br>
  503. This is not equivalent to <code>this</code>, which represents
  504. the caller-side <code>this</code> object.<br>
  505. <code>$0</code> is <code>null</code> if the method is static.
  506. </td>
  507. </tr>
  508. <tr><td>&nbsp</td></tr>
  509. <tr><td>&nbsp</td></tr>
  510. <tr>
  511. <td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  512. <td>
  513. The parameters of the method call.
  514. </td>
  515. </tr>
  516. <tr><td>
  517. <code>$_</code></td>
  518. <td>The resulting value of the method call.</td>
  519. </tr>
  520. <tr><td><code>$r</code></td>
  521. <td>The result type of the method call.</td>
  522. </tr>
  523. <tr><td><code>$class</code> &nbsp &nbsp</td>
  524. <td>A <code>java.lang.Class</code> object representing
  525. the class declaring the method.
  526. </td>
  527. </tr>
  528. <tr><td><code>$sig</code> &nbsp &nbsp</td>
  529. <td>An array of <code>java.lang.Class</code> objects representing
  530. the formal parameter types.</td>
  531. </tr>
  532. <tr><td><code>$type</code> &nbsp &nbsp</td>
  533. <td>A <code>java.lang.Class</code> object representing
  534. the formal result type.</td>
  535. </tr>
  536. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  537. <td>The name of the method originally called
  538. in the expression.</td>
  539. </tr>
  540. </table>
  541. </ul>
  542. <p>Here the method call means the one represented by the
  543. <code>MethodCall</code> object.
  544. <p>The other identifiers such as <code>$w</code>,
  545. <code>$args</code> and <code>$$</code>
  546. are also available.
  547. <p>Unless the result type of the method call is <code>void</code>,
  548. a value must be assigned to
  549. <code>$_</code> in the source text and the type of <code>$_</code>
  550. is the result type.
  551. If the result type is <code>void</code>, the type of <code>$_</code>
  552. is <code>Object</code> and the value assigned to <code>$_</code>
  553. is ignored.
  554. <p><code>$proceed</code> is not a <code>String</code> value but special
  555. syntax. It must be followed by an argument list surrounded by parentheses
  556. <code>( )</code>.
  557. <h4>javassist.expr.FieldAccess</h4>
  558. <p>A <code>FieldAccess</code> object represents field access.
  559. The method <code>edit()</code> in <code>ExprEditor</code>
  560. receives this object if field access is found.
  561. The method <code>replace()</code> in
  562. <code>FieldAccess</code> receives
  563. source text representing the substitued statement or
  564. block for the field access.
  565. In the source text, the identifiers starting with <code>$</code>
  566. have also special meaning:
  567. <ul><table border=0>
  568. <tr>
  569. <td><code>$0</code></td>
  570. <td rowspan=3>
  571. The object containing the field accessed by the expression.
  572. This is not equivalent to <code>this</code>.<br>
  573. <code>this</code> represents the object that the method including the
  574. expression is invoked on.<br>
  575. <code>$0</code> is <code>null</code> if the field is static.
  576. </td>
  577. </tr>
  578. <tr><td>&nbsp</td></tr>
  579. <tr><td>&nbsp</td></tr>
  580. <tr>
  581. <td><code>$1</code></td>
  582. <td rowspan=2>
  583. The value that would be stored in the field
  584. if the expression is write access.
  585. <br>Otherwise, <code>$1</code> is not available.
  586. </td>
  587. </tr>
  588. <tr><td>&nbsp</td></tr>
  589. <tr>
  590. <td><code>$_</code></td>
  591. <td rowspan=2>
  592. The resulting value of the field access
  593. if the expression is read access.
  594. <br>Otherwise, the value stored in <code>$_</code> is discarded.
  595. </td>
  596. </tr>
  597. <tr><td>&nbsp</td></tr>
  598. <tr>
  599. <td><code>$r</code></td>
  600. <td rowspan=2>
  601. The type of the field if the expression is read access.
  602. <br>Otherwise, <code>$r</code> is <code>void</code>.
  603. </td>
  604. </tr>
  605. <tr><td>&nbsp</td></tr>
  606. <tr><td><code>$class</code> &nbsp &nbsp</td>
  607. <td>A <code>java.lang.Class</code> object representing
  608. the class declaring the field.
  609. </td></tr>
  610. <tr><td><code>$type</code></td>
  611. <td>A <code>java.lang.Class</code> object representing
  612. the field type.</td>
  613. </tr>
  614. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  615. <td>The name of a virtual method executing the original
  616. field access.
  617. .</td>
  618. </tr>
  619. </table>
  620. </ul>
  621. <p>The other identifiers such as <code>$w</code>,
  622. <code>$args</code> and <code>$$</code>
  623. are also available.
  624. <p>If the expression is read access, a value must be assigned to
  625. <code>$_</code> in the source text. The type of <code>$_</code>
  626. is the type of the field.
  627. <h4>javassist.expr.NewExpr</h4>
  628. <p>A <code>NewExpr</code> object represents object creation
  629. with the <code>new</code> operator.
  630. The method <code>edit()</code> in <code>ExprEditor</code>
  631. receives this object if object creation is found.
  632. The method <code>replace()</code> in
  633. <code>NewExpr</code> receives
  634. source text representing the substitued statement or
  635. block for the object creation.
  636. In the source text, the identifiers starting with <code>$</code>
  637. have also special meaning:
  638. <ul><table border=0>
  639. <tr>
  640. <td><code>$0</code></td>
  641. <td>
  642. <code>null</code>.
  643. </td>
  644. </tr>
  645. <tr>
  646. <td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  647. <td>
  648. The parameters to the constructor.
  649. </td>
  650. </tr>
  651. <tr>
  652. <td><code>$_</code></td>
  653. <td rowspan=2>
  654. The resulting value of the object creation.
  655. <br>A newly created object must be stored in this variable.
  656. </td>
  657. </tr>
  658. <tr><td>&nbsp</td></tr>
  659. <tr>
  660. <td><code>$r</code></td>
  661. <td>
  662. The type of the created object.
  663. </td>
  664. </tr>
  665. <tr><td><code>$class</code> &nbsp &nbsp</td>
  666. <td>A <code>java.lang.Class</code> object representing
  667. the class of the created object.
  668. </td></tr>
  669. <tr><td><code>$sig</code> &nbsp &nbsp</td>
  670. <td>An array of <code>java.lang.Class</code> objects representing
  671. the formal parameter types.</td>
  672. </tr>
  673. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  674. <td>The name of a virtual method executing the original
  675. object creation.
  676. .</td>
  677. </tr>
  678. </table>
  679. </ul>
  680. <p>The other identifiers such as <code>$w</code>,
  681. <code>$args</code> and <code>$$</code>
  682. are also available.
  683. <h4>javassist.expr.Instanceof</h4>
  684. <p>A <code>Instanceof</code> object represents an <code>instanceof</code>
  685. expression.
  686. The method <code>edit()</code> in <code>ExprEditor</code>
  687. receives this object if an instanceof expression is found.
  688. The method <code>replace()</code> in
  689. <code>Instanceof</code> receives
  690. source text representing the substitued statement or
  691. block for the expression.
  692. In the source text, the identifiers starting with <code>$</code>
  693. have also special meaning:
  694. <ul><table border=0>
  695. <tr>
  696. <td><code>$0</code></td>
  697. <td>
  698. <code>null</code>.
  699. </td>
  700. </tr>
  701. <tr>
  702. <td><code>$1</code></td>
  703. <td>
  704. The value on the left hand side of the original
  705. <code>instanceof</code> operator.
  706. </td>
  707. </tr>
  708. <tr>
  709. <td><code>$_</code></td>
  710. <td>
  711. The resulting value of the expression.
  712. The type of <code>$_</code> is <code>boolean</code>.
  713. </td>
  714. </tr>
  715. <tr>
  716. <td><code>$r</code></td>
  717. <td>
  718. The type on the right hand side of the <code>instanceof</code> operator.
  719. </td>
  720. </tr>
  721. <tr><td><code>$type</code></td>
  722. <td>A <code>java.lang.Class</code> object representing
  723. the type on the right hand side of the <code>instanceof</code> operator.
  724. </td>
  725. </tr>
  726. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  727. <td rowspan=4>The name of a virtual method executing the original
  728. <code>instanceof</code> expression.
  729. <br>It takes one parameter (the type is <code>java.lang.Object</code>)
  730. and returns true
  731. <br>if the parameter value is an instance of the type on the right
  732. hand side of
  733. <br>the original <code>instanceof</code> operator.
  734. Otherwise, it returns false.
  735. </td>
  736. </tr>
  737. <tr><td>&nbsp</td></tr>
  738. <tr><td>&nbsp</td></tr>
  739. <tr><td>&nbsp</td></tr>
  740. </table>
  741. </ul>
  742. <p>The other identifiers such as <code>$w</code>,
  743. <code>$args</code> and <code>$$</code>
  744. are also available.
  745. <h4>javassist.expr.Cast</h4>
  746. <p>A <code>Cast</code> object represents an expression for
  747. explicit type casting.
  748. The method <code>edit()</code> in <code>ExprEditor</code>
  749. receives this object if explicit type casting is found.
  750. The method <code>replace()</code> in
  751. <code>Cast</code> receives
  752. source text representing the substitued statement or
  753. block for the expression.
  754. In the source text, the identifiers starting with <code>$</code>
  755. have also special meaning:
  756. <ul><table border=0>
  757. <tr>
  758. <td><code>$0</code></td>
  759. <td>
  760. <code>null</code>.
  761. </td>
  762. </tr>
  763. <tr>
  764. <td><code>$1</code></td>
  765. <td>
  766. The value the type of which is explicitly cast.
  767. </td>
  768. </tr>
  769. <tr>
  770. <td><code>$_</code></td>
  771. <td rowspan=2>
  772. The resulting value of the expression.
  773. The type of <code>$_</code> is the same as the type
  774. <br>after the explicit casting, that is, the type surrounded
  775. by <code>( )</code>.
  776. </td>
  777. </tr>
  778. <tr><td>&nbsp</td></tr>
  779. <tr>
  780. <td><code>$r</code></td>
  781. <td>the type after the explicit casting, or the type surrounded
  782. by <code>( )</code>.
  783. </td>
  784. </tr>
  785. <tr><td><code>$type</code></td>
  786. <td>A <code>java.lang.Class</code> object representing
  787. the same type as <code>$r</code>.
  788. </td>
  789. </tr>
  790. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  791. <td rowspan=3>The name of a virtual method executing the original
  792. type casting.
  793. <br>It takes one parameter of the type <code>java.lang.Object</code>
  794. and returns it after
  795. <br>the explicit type casting specified by the original expression.
  796. </td>
  797. </tr>
  798. <tr><td>&nbsp</td></tr>
  799. <tr><td>&nbsp</td></tr>
  800. </table>
  801. </ul>
  802. <p>The other identifiers such as <code>$w</code>,
  803. <code>$args</code> and <code>$$</code>
  804. are also available.
  805. <h4>javassist.expr.Handler</h4>
  806. <p>A <code>Handler</code> object represents a <code>catch</code>
  807. clause of <code>try-catch</code> statement.
  808. The method <code>edit()</code> in <code>ExprEditor</code>
  809. receives this object if a <code>catch</code> is found.
  810. The method <code>insertBefore()</code> in
  811. <code>Handler</code> compiles the received
  812. source text and inserts it at the beginning of the <code>catch</code> clause.
  813. In the source text, the identifiers starting with <code>$</code>
  814. have special meaning:
  815. <ul><table border=0>
  816. <tr>
  817. <td><code>$1</code></td>
  818. <td>
  819. The exception object caught by the <code>catch</code> clause.
  820. </td>
  821. </tr>
  822. <tr>
  823. <td><code>$r</code></td>
  824. <td>the type of the exception caught by the <code>catch</code> clause.
  825. It is used in a cast expression.
  826. </td>
  827. </tr>
  828. <tr>
  829. <td><code>$w</code></td>
  830. <td>The wrapper type. It is used in a cast expression.
  831. </td>
  832. </tr>
  833. <tr><td><code>$type</code> &nbsp &nbsp</td>
  834. <td rowspan=2>
  835. A <code>java.lang.Class</code> object representing
  836. <br>the type of the exception caught by the <code>catch</code> clause.
  837. </td>
  838. </tr>
  839. <tr><td>&nbsp</td></tr>
  840. </table>
  841. </ul>
  842. <p>If a new exception object is assigned to <code>$1</code>,
  843. it is passed to the original <code>catch</code> clause as the caught
  844. exception.
  845. <p><br>
  846. <h3>5.3 Adding a new method or field</h3>
  847. <h4>Adding a method</h4>
  848. <p>Javassist allows the users to create a new method and constructor
  849. from scratch. <code>CtNewMethod</code>
  850. and <code>CtNewConstructor</code> provide several factory methods,
  851. which are static methods for creating <code>CtMethod</code> or
  852. <code>CtConstructor</code> objects.
  853. Especially, <code>make()</code> creates
  854. a <code>CtMethod</code> or <code>CtConstructor</code> object
  855. from the given source text.
  856. <p>For example, this program:
  857. <ul><pre>
  858. CtClass point = ClassPool.getDefault().get("Point");
  859. CtMethod m = CtNewMethod.make(
  860. "public int xmove(int dx) { x += dx; }",
  861. point);
  862. point.addMethod(m);
  863. </pre></ul>
  864. <p>adds a public method <code>xmove()</code> to class <code>Point</code>.
  865. In this example, <code>x</code> is a <code>int</code> field in
  866. the class <code>Point</code>.
  867. <p>The source text passed to <code>make()</code> can include the
  868. identifiers starting with <code>$</code> except <code>$_</code>
  869. as in <code>setBody()</code>.
  870. It can also include
  871. <code>$proceed</code> if the target object and the target method name
  872. are also given to <code>make()</code>. For example,
  873. <ul><pre>
  874. CtClass point = ClassPool.getDefault().get("Point");
  875. CtMethod m = CtNewMethod.make(
  876. "public int ymove(int dy) { $proceed(0, dy); }",
  877. point, "this", "move");
  878. </pre></ul>
  879. <p>this program creates a method <code>ymove()</code> defined below:
  880. <ul><pre>
  881. public int ymove(int dy) { this.move(0, dy); }
  882. </pre></ul>
  883. <p>Note that <code>$proceed</code> has been replaced with
  884. <code>this.move</code>.
  885. <p>Javassist provides another way to add a new method.
  886. You can first create an abstract method and later give it a method body:
  887. <ul><pre>
  888. CtClass cc = ... ;
  889. CtMethod m = new CtMethod(CtClass.intType, "move",
  890. new CtClass[] { CtClass.intType }, cc);
  891. cc.addMethod(m);
  892. m.setBody("{ x += $1; }");
  893. cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
  894. </pre></ul>
  895. <p>Since Javassist makes a class abstract if an abstract method is
  896. added to the class, you have to explicitly change the class back to a
  897. non-abstract one after calling <code>setBody()</code>.
  898. <h4>Mutual recursive methods</h4>
  899. <p>Javassist cannot compile a method if it calls another method that
  900. has not been added to a class. (Javassist can compile a method that
  901. calls itself recursively.) To add mutual recursive methods to a class,
  902. you need a trick shown below. Suppose that you want to add methods
  903. <code>m()</code> and <code>n()</code> to a class represented
  904. by <code>cc</code>:
  905. <ul><pre>
  906. CtClass cc = ... ;
  907. CtMethod m = CtNewMethod.make("public abstract int m(int i);", cc);
  908. CtMethod n = CtNewMethod.make("public abstract int n(int i);", cc);
  909. cc.addMethod(m);
  910. cc.addMethod(n);
  911. m.setBody("{ return ($1 <= 0) ? 1 : (n($1 - 1) * $1); }");
  912. n.setBody("{ return m($1); }");
  913. cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
  914. </pre></ul>
  915. <p>You must first make two abstract methods and add them to the class.
  916. Then you can give the method bodies to these methods even if the method
  917. bodies include method calls to each other. Finally you must change the
  918. class to a not-abstract class since <code>addMethod()</code> automatically
  919. changes a class into an abstract one if an abstract method is added.
  920. <h4>Adding a field</h4>
  921. <p>Javassist also allows the users to create a new field.
  922. <ul><pre>
  923. CtClass point = ClassPool.getDefault().get("Point");
  924. CtField f = new CtField(CtClass.intType, "z", point);
  925. point.addField(f);
  926. </pre></ul>
  927. <p>This program adds a field named <code>z</code> to class
  928. <code>Point</code>.
  929. <p>If the initial value of the added field must be specified,
  930. the program shown above must be modified into:
  931. <ul><pre>
  932. CtClass point = ClassPool.getDefault().get("Point");
  933. CtField f = new CtField(CtClass.intType, "z", point);
  934. point.addField(f, "0"); <em>// initial value is 0.</em>
  935. </pre></ul>
  936. <p>Now, the method <code>addField()</code> receives the second parameter,
  937. which is the source text representing an expression computing the initial
  938. value. This source text can be any Java expression if the result type
  939. of the expression matches the type of the field. Note that an expression
  940. does not end with a semi colon (<code>;</code>).
  941. <p><br>
  942. <h3>5.4 Limitations</h3>
  943. <p>In the current implementation, the Java compiler included in Javassist
  944. has several limitations with respect to the language that the compiler can
  945. accept. Those limitations are:
  946. <p><li>The <code>.class</code> notation is not supported. Use the
  947. method <code>Class.forName()</code>.
  948. In regular
  949. Java, an expression <code>Point.class</code> means a <code>Class</code>
  950. object representing the <code>Point</code> class. This notation is
  951. not available.
  952. <p><li>Array initializers, a comma-separated list of expressions
  953. enclosed by braces <code>{</code> and <code>}</code>, are not
  954. supported.
  955. <p><li>Inner classes or anonymous classes are not supported.
  956. <p><li><code>switch</code> or <code>synchronized</code>
  957. statements are not supported yet.
  958. <p><li>Labeled <code>continue</code> and <code>break</code> statements
  959. are not supported.
  960. <p><li>The <code>finally</code> clause following
  961. <code>try</code> and <code>catch</code> clauses is not supported.
  962. <p><li>The compiler does not correctly implement the Java method dispatch
  963. algorithm. The compiler may confuse if methods defined in a class
  964. have the same name but take different parameter lists.
  965. <p><li>The users are recommended to use <code>#</code> as the separator
  966. between a class name and a static method or field name.
  967. For example, in regular Java,
  968. <ul><pre>javassist.CtClass.intType.getName()</pre></ul>
  969. <p>calls a method <code>getName()</code> on
  970. the object indicated by the static field <code>intType</code>
  971. in <code>javassist.CtClass</code>. In Javassist, the users can
  972. write the expression shown above but they are recommended to
  973. write:
  974. <ul><pre>javassist.CtClass#intType.getName()</pre></ul>
  975. <p>so that the compiler can quickly parse the expression.
  976. </ul>
  977. <p><br>
  978. <a href="tutorial.html">Previous page</a>
  979. <hr>
  980. Java(TM) is a trademark of Sun Microsystems, Inc.<br>
  981. Copyright (C) 2000-2003 by Shigeru Chiba, All rights reserved.
  982. </body>
  983. </html>