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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. <ul><pre>$_ = ($r)foo();</pre></ul>
  291. <p>is a valid statement if the result type is <code>void</code>.
  292. Here, <code>foo()</code> is a <code>void</code> method.
  293. <p>The cast operator <code>($r)</code> is also useful in a
  294. <code>return</code> statement. Even if the result type is
  295. <code>void</code>, the following <code>return</code> statement is valid:
  296. <ul><pre>return ($r)result;</pre></ul>
  297. <p>Here, <code>result</code> is some local variable.
  298. Since <code>($r)</code> is specified, the resulting value is
  299. discarded.
  300. This <code>return</code> statement is regarded as the equivalent
  301. of the <code>return</code> statement without a resulting value:
  302. <ul><pre>return;</pre></ul>
  303. <h4>$w</h4>
  304. <p><code>$w</code> represents a wrapper type.
  305. It must be used as the cast type in a cast expression.
  306. <code>($w)</code> converts from a primitive type to the corresponding
  307. wrapper type.
  308. The following code is an example:
  309. <ul><pre>Integer i = ($w)5;</pre></ul>
  310. <p>The selected wrapper type depends on the type of the expression
  311. following <code>($w)</code>. If the type of the expression is
  312. <code>double</code>, then the wrapper type is <code>java.lang.Double</code>.
  313. <p>If the type of the expression following <code>($w)</code> is not
  314. a primitive type, then <code>($w)</code> does nothing.
  315. <h4>$_</h4>
  316. <p><code>insertAfter()</code> in <code>CtMethod</code> and
  317. <code>CtConstructor</code> inserts the
  318. compiled code at the end of the method. In the statement given to
  319. <code>insertAfter()</code>, not only the variables shown above such as
  320. <code>$0</code>, <code>$1</code>, ... but also <code>$_</code> is
  321. available.
  322. <p>The variable <code>$_</code> represents the resulting value of the
  323. method. The type of that variable is the type of the result type (the
  324. return type) of the method. If the result type is <code>void</code>,
  325. then the type of <code>$_</code> is <code>Object</code> and the value
  326. of <code>$_</code> is <code>null</code>.
  327. <p>Although the compiled code inserted by <code>insertAfter()</code>
  328. is executed just before the control normally returns from the method,
  329. it can be also executed when an exception is thrown from the method.
  330. To execute it when an exception is thrown, the second parameter
  331. <code>asFinally</code> to <code>insertAfter()</code> must be
  332. <code>true</code>.
  333. <p>If an exception is thrown, the compiled code inserted by
  334. <code>insertAfter()</code> is executed as a <code>finally</code>
  335. clause. The value of <code>$_</code> is <code>0</code> or
  336. <code>null</code> in the compiled code. After the execution of the
  337. compiled code terminates, the exception originally thrown is re-thrown
  338. to the caller. Note that the value of <code>$_</code> is never thrown
  339. to the caller; it is rather discarded.
  340. <h4>$sig</h4>
  341. <p>The value of <code>$sig</code> is an array of
  342. <code>java.lang.Class</code> objects that represent the formal
  343. parameter types in declaration order.
  344. <h4>$type</h4>
  345. <p>The value of <code>$type</code> is an <code>java.lang.Class</code>
  346. object representing the formal type of the result value. This
  347. variable is available only in <code>insertAfter()</code> in
  348. <code>CtMethod</code> and <code>CtConstructor</code>.
  349. <h4>$class</h4>
  350. <p>The value of <code>$class</code> is an <code>java.lang.Class</code>
  351. object representing the class in which the edited method is declared.
  352. <h4>addCatch()</h4>
  353. <p><code>addCatch()</code> inserts a code fragment into a method body
  354. so that the code fragment is executed when the method body throws
  355. an exception and the control returns to the caller. In the source
  356. text representing the inserted code fragment, the exception value
  357. is referred to with the special variable <code>$e</code>.
  358. <p>For example, this program:
  359. <ul><pre>
  360. CtMethod m = ...;
  361. CtClass etype = ClassPool.getDefault().get("java.io.IOException");
  362. m.addCatch("{ System.out.println($e); throw $e; }", etype);
  363. </pre></ul>
  364. <p>translates the method body represented by <code>m</code> into
  365. something like this:
  366. <ul><pre>
  367. try {
  368. <font face="serif"><em>the original method body</em></font>
  369. }
  370. catch (java.io.IOException e) {
  371. System.out.println(e);
  372. throw e;
  373. }
  374. </pre></ul>
  375. <p>Note that the inserted code fragment must end with a
  376. <code>throw</code> or <code>return</code> statement.
  377. <p><br>
  378. <h3>5.2 Modifying a method body</h3>
  379. <p><code>javassist.expr.ExprEditor</code> is a class
  380. for replacing an expression in a method body.
  381. The users can define a subclass of <code>ExprEditor</code>
  382. to specify how an expression is modified.
  383. <p>To run an <code>ExprEditor</code> object, the users must
  384. call <code>instrument()</code> in <code>CtMethod</code> or
  385. <code>CtClass</code>.
  386. For example,
  387. <ul><pre>
  388. CtMethod cm = ... ;
  389. cm.instrument(
  390. new ExprEditor() {
  391. public void edit(MethodCall m)
  392. throws CannotCompileException
  393. {
  394. if (m.getClassName().equals("Point")
  395. && m.getMethodName().equals("move"))
  396. m.replace("{ $1 = 0; $_ = $proceed($$); }");
  397. }
  398. });
  399. </pre></ul>
  400. <p>searches the method body represented by <code>cm</code> and
  401. replaces all calls to <code>move()</code> in class <code>Point</code>
  402. with a block:
  403. <ul><pre>{ $1 = 0; $_ = $proceed($$); }
  404. </pre></ul>
  405. <p>so that the first parameter to <code>move()</code> is always 0.
  406. Note that the substituted code is not an expression but
  407. a statement or a block.
  408. <p>The method <code>instrument()</code> searches a method body.
  409. If it finds an expression such as a method call, field access, and object
  410. creation, then it calls <code>edit()</code> on the given
  411. <code>ExprEditor</code> object. The parameter to <code>edit()</code>
  412. is an object representing the found expression. The <code>edit()</code>
  413. method can inspect and replace the expression through that object.
  414. <p>Calling <code>replace()</code> on the parameter to <code>edit()</code>
  415. substitutes the given statement or block for the expression. If the given
  416. block is an empty block, that is, if <code>replace("{}")</code>
  417. is executed, then the expression is removed from the method body.
  418. If you want to insert a statement (or a block) before/after the
  419. expression, a block like the following should be passed to
  420. <code>replace()</code>:
  421. <ul><pre>
  422. { <em>before-statements;</em>
  423. $_ = $proceed($$);
  424. <em>after-statements;</em> }
  425. </pre></ul>
  426. <p>whichever the expression is either a method call, field access,
  427. object creation, or others. The second statement could be:
  428. <ul><pre>$_ = $proceed();</pre></ul>
  429. <p>if the expression is read access, or
  430. <ul><pre>$proceed($$);</pre></ul>
  431. <p>if the expression is write access.
  432. <h4>javassist.expr.MethodCall</h4>
  433. <p>A <code>MethodCall</code> object represents a method call.
  434. The method <code>replace()</code> in
  435. <code>MethodCall</code> substitutes a statement or
  436. a block for the method call.
  437. It receives source text representing the substitued statement or
  438. block, in which the identifiers starting with <code>$</code>
  439. have special meaning as in the source text passed to
  440. <code>insertBefore()</code>.
  441. <ul><table border=0>
  442. <tr>
  443. <td><code>$0</code></td>
  444. <td rowspan=3>
  445. The target object of the method call.<br>
  446. This is not equivalent to <code>this</code>, which represents
  447. the caller-side <code>this</code> object.<br>
  448. <code>$0</code> is <code>null</code> if the method is static.
  449. </td>
  450. </tr>
  451. <tr><td>&nbsp</td></tr>
  452. <tr><td>&nbsp</td></tr>
  453. <tr>
  454. <td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  455. <td>
  456. The parameters of the method call.
  457. </td>
  458. </tr>
  459. <tr><td>
  460. <code>$_</code></td>
  461. <td>The resulting value of the method call.</td>
  462. </tr>
  463. <tr><td><code>$r</code></td>
  464. <td>The result type of the method call.</td>
  465. </tr>
  466. <tr><td><code>$class</code> &nbsp &nbsp</td>
  467. <td>A <code>java.lang.Class</code> object representing
  468. the class declaring the method.
  469. </td>
  470. </tr>
  471. <tr><td><code>$sig</code> &nbsp &nbsp</td>
  472. <td>An array of <code>java.lang.Class</code> objects representing
  473. the formal parameter types.</td>
  474. </tr>
  475. <tr><td><code>$type</code> &nbsp &nbsp</td>
  476. <td>A <code>java.lang.Class</code> object representing
  477. the formal result type.</td>
  478. </tr>
  479. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  480. <td>The name of the method originally called
  481. in the expression.</td>
  482. </tr>
  483. </table>
  484. </ul>
  485. <p>Here the method call means the one represented by the
  486. <code>MethodCall</code> object.
  487. <p>The other identifiers such as <code>$w</code>,
  488. <code>$args</code> and <code>$$</code>
  489. are also available.
  490. <p>Unless the result type of the method call is <code>void</code>,
  491. a value must be assigned to
  492. <code>$_</code> in the source text and the type of <code>$_</code>
  493. is the result type.
  494. If the result type is <code>void</code>, the type of <code>$_</code>
  495. is <code>Object</code> and the value assigned to <code>$_</code>
  496. is ignored.
  497. <p><code>$proceed</code> is not a <code>String</code> value but special
  498. syntax. It must be followed by an argument list surrounded by parentheses
  499. <code>( )</code>.
  500. <h4>javassist.expr.FieldAccess</h4>
  501. <p>A <code>FieldAccess</code> object represents field access.
  502. The method <code>edit()</code> in <code>ExprEditor</code>
  503. receives this object if field access is found.
  504. The method <code>replace()</code> in
  505. <code>FieldAccess</code> receives
  506. source text representing the substitued statement or
  507. block for the field access.
  508. In the source text, the identifiers starting with <code>$</code>
  509. have also special meaning:
  510. <ul><table border=0>
  511. <tr>
  512. <td><code>$0</code></td>
  513. <td rowspan=3>
  514. The object containing the field accessed by the expression.
  515. This is not equivalent to <code>this</code>.<br>
  516. <code>this</code> represents the object that the method including the
  517. expression is invoked on.<br>
  518. <code>$0</code> is <code>null</code> if the field is static.
  519. </td>
  520. </tr>
  521. <tr><td>&nbsp</td></tr>
  522. <tr><td>&nbsp</td></tr>
  523. <tr>
  524. <td><code>$1</code></td>
  525. <td rowspan=2>
  526. The value that would be stored in the field
  527. if the expression is write access.
  528. <br>Otherwise, <code>$1</code> is not available.
  529. </td>
  530. </tr>
  531. <tr><td>&nbsp</td></tr>
  532. <tr>
  533. <td><code>$_</code></td>
  534. <td rowspan=2>
  535. The resulting value of the field access
  536. if the expression is read access.
  537. <br>Otherwise, the value stored in <code>$_</code> is discarded.
  538. </td>
  539. </tr>
  540. <tr><td>&nbsp</td></tr>
  541. <tr>
  542. <td><code>$r</code></td>
  543. <td rowspan=2>
  544. The type of the field if the expression is read access.
  545. <br>Otherwise, <code>$r</code> is <code>void</code>.
  546. </td>
  547. </tr>
  548. <tr><td>&nbsp</td></tr>
  549. <tr><td><code>$class</code> &nbsp &nbsp</td>
  550. <td>A <code>java.lang.Class</code> object representing
  551. the class declaring the field.
  552. </td></tr>
  553. <tr><td><code>$type</code></td>
  554. <td>A <code>java.lang.Class</code> object representing
  555. the field type.</td>
  556. </tr>
  557. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  558. <td>The name of a virtual method executing the original
  559. field access.
  560. .</td>
  561. </tr>
  562. </table>
  563. </ul>
  564. <p>The other identifiers such as <code>$w</code>,
  565. <code>$args</code> and <code>$$</code>
  566. are also available.
  567. <p>If the expression is read access, a value must be assigned to
  568. <code>$_</code> in the source text. The type of <code>$_</code>
  569. is the type of the field.
  570. <h4>javassist.expr.NewExpr</h4>
  571. <p>A <code>NewExpr</code> object represents object creation
  572. with the <code>new</code> operator.
  573. The method <code>edit()</code> in <code>ExprEditor</code>
  574. receives this object if object creation is found.
  575. The method <code>replace()</code> in
  576. <code>NewExpr</code> receives
  577. source text representing the substitued statement or
  578. block for the object creation.
  579. In the source text, the identifiers starting with <code>$</code>
  580. have also special meaning:
  581. <ul><table border=0>
  582. <tr>
  583. <td><code>$0</code></td>
  584. <td>
  585. <code>null</code>.
  586. </td>
  587. </tr>
  588. <tr>
  589. <td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
  590. <td>
  591. The parameters to the constructor.
  592. </td>
  593. </tr>
  594. <tr>
  595. <td><code>$_</code></td>
  596. <td rowspan=2>
  597. The resulting value of the object creation.
  598. <br>A newly created object must be stored in this variable.
  599. </td>
  600. </tr>
  601. <tr><td>&nbsp</td></tr>
  602. <tr>
  603. <td><code>$r</code></td>
  604. <td>
  605. The type of the created object.
  606. </td>
  607. </tr>
  608. <tr><td><code>$class</code> &nbsp &nbsp</td>
  609. <td>A <code>java.lang.Class</code> object representing
  610. the class of the created object.
  611. </td></tr>
  612. <tr><td><code>$sig</code> &nbsp &nbsp</td>
  613. <td>An array of <code>java.lang.Class</code> objects representing
  614. the formal parameter types.</td>
  615. </tr>
  616. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  617. <td>The name of a virtual method executing the original
  618. object creation.
  619. .</td>
  620. </tr>
  621. </table>
  622. </ul>
  623. <p>The other identifiers such as <code>$w</code>,
  624. <code>$args</code> and <code>$$</code>
  625. are also available.
  626. <h4>javassist.expr.Instanceof</h4>
  627. <p>A <code>Instanceof</code> object represents an <code>instanceof</code>
  628. expression.
  629. The method <code>edit()</code> in <code>ExprEditor</code>
  630. receives this object if an instanceof expression is found.
  631. The method <code>replace()</code> in
  632. <code>Instanceof</code> receives
  633. source text representing the substitued statement or
  634. block for the expression.
  635. In the source text, the identifiers starting with <code>$</code>
  636. have also special meaning:
  637. <ul><table border=0>
  638. <tr>
  639. <td><code>$0</code></td>
  640. <td>
  641. <code>null</code>.
  642. </td>
  643. </tr>
  644. <tr>
  645. <td><code>$1</code></td>
  646. <td>
  647. The value on the left hand side of the original
  648. <code>instanceof</code> operator.
  649. </td>
  650. </tr>
  651. <tr>
  652. <td><code>$_</code></td>
  653. <td>
  654. The resulting value of the expression.
  655. The type of <code>$_</code> is <code>boolean</code>.
  656. </td>
  657. </tr>
  658. <tr>
  659. <td><code>$r</code></td>
  660. <td>
  661. The type on the right hand side of the <code>instanceof</code> operator.
  662. </td>
  663. </tr>
  664. <tr><td><code>$type</code></td>
  665. <td>A <code>java.lang.Class</code> object representing
  666. the type on the right hand side of the <code>instanceof</code> operator.
  667. </td>
  668. </tr>
  669. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  670. <td rowspan=4>The name of a virtual method executing the original
  671. <code>instanceof</code> expression.
  672. <br>It takes one parameter (the type is <code>java.lang.Object</code>)
  673. and returns true
  674. <br>if the parameter value is an instance of the type on the right
  675. hand side of
  676. <br>the original <code>instanceof</code> operator.
  677. Otherwise, it returns false.
  678. </td>
  679. </tr>
  680. <tr><td>&nbsp</td></tr>
  681. <tr><td>&nbsp</td></tr>
  682. <tr><td>&nbsp</td></tr>
  683. </table>
  684. </ul>
  685. <p>The other identifiers such as <code>$w</code>,
  686. <code>$args</code> and <code>$$</code>
  687. are also available.
  688. <h4>javassist.expr.Cast</h4>
  689. <p>A <code>Cast</code> object represents an expression for
  690. explicit type casting.
  691. The method <code>edit()</code> in <code>ExprEditor</code>
  692. receives this object if explicit type casting is found.
  693. The method <code>replace()</code> in
  694. <code>Cast</code> receives
  695. source text representing the substitued statement or
  696. block for the expression.
  697. In the source text, the identifiers starting with <code>$</code>
  698. have also special meaning:
  699. <ul><table border=0>
  700. <tr>
  701. <td><code>$0</code></td>
  702. <td>
  703. <code>null</code>.
  704. </td>
  705. </tr>
  706. <tr>
  707. <td><code>$1</code></td>
  708. <td>
  709. The value the type of which is explicitly cast.
  710. </td>
  711. </tr>
  712. <tr>
  713. <td><code>$_</code></td>
  714. <td rowspan=2>
  715. The resulting value of the expression.
  716. The type of <code>$_</code> is the same as the type
  717. <br>after the explicit casting, that is, the type surrounded
  718. by <code>( )</code>.
  719. </td>
  720. </tr>
  721. <tr><td>&nbsp</td></tr>
  722. <tr>
  723. <td><code>$r</code></td>
  724. <td>the type after the explicit casting, or the type surrounded
  725. by <code>( )</code>.
  726. </td>
  727. </tr>
  728. <tr><td><code>$type</code></td>
  729. <td>A <code>java.lang.Class</code> object representing
  730. the same type as <code>$r</code>.
  731. </td>
  732. </tr>
  733. <tr><td><code>$proceed</code> &nbsp &nbsp</td>
  734. <td rowspan=3>The name of a virtual method executing the original
  735. type casting.
  736. <br>It takes one parameter of the type <code>java.lang.Object</code>
  737. and returns it after
  738. <br>the explicit type casting specified by the original expression.
  739. </td>
  740. </tr>
  741. <tr><td>&nbsp</td></tr>
  742. <tr><td>&nbsp</td></tr>
  743. </table>
  744. </ul>
  745. <p>The other identifiers such as <code>$w</code>,
  746. <code>$args</code> and <code>$$</code>
  747. are also available.
  748. <h4>javassist.expr.Handler</h4>
  749. <p>A <code>Handler</code> object represents a <code>catch</code>
  750. clause of <code>try-catch</code> statement.
  751. The method <code>edit()</code> in <code>ExprEditor</code>
  752. receives this object if a <code>catch</code> is found.
  753. The method <code>insertBefore()</code> in
  754. <code>Handler</code> compiles the received
  755. source text and inserts it at the beginning of the <code>catch</code> clause.
  756. In the source text, the identifiers starting with <code>$</code>
  757. have special meaning:
  758. <ul><table border=0>
  759. <tr>
  760. <td><code>$1</code></td>
  761. <td>
  762. The exception object caught by the <code>catch</code> clause.
  763. </td>
  764. </tr>
  765. <tr>
  766. <td><code>$r</code></td>
  767. <td>the type of the exception caught by the <code>catch</code> clause.
  768. It is used in a cast expression.
  769. </td>
  770. </tr>
  771. <tr>
  772. <td><code>$w</code></td>
  773. <td>The wrapper type. It is used in a cast expression.
  774. </td>
  775. </tr>
  776. <tr><td><code>$type</code> &nbsp &nbsp</td>
  777. <td rowspan=2>
  778. A <code>java.lang.Class</code> object representing
  779. <br>the type of the exception caught by the <code>catch</code> clause.
  780. </td>
  781. </tr>
  782. <tr><td>&nbsp</td></tr>
  783. </table>
  784. </ul>
  785. <p>If a new exception object is assigned to <code>$1</code>,
  786. it is passed to the original <code>catch</code> clause as the caught
  787. exception.
  788. <p><br>
  789. <h3>5.3 Adding a new method or field</h3>
  790. <h4>Adding a method</h4>
  791. <p>Javassist allows the users to create a new method and constructor
  792. from scratch. <code>CtNewMethod</code>
  793. and <code>CtNewConstructor</code> provide several factory methods,
  794. which are static methods for creating <code>CtMethod</code> or
  795. <code>CtConstructor</code> objects.
  796. Especially, <code>make()</code> creates
  797. a <code>CtMethod</code> or <code>CtConstructor</code> object
  798. from the given source text.
  799. <p>For example, this program:
  800. <ul><pre>
  801. CtClass point = ClassPool.getDefault().get("Point");
  802. CtMethod m = CtNewMethod.make(
  803. "public int xmove(int dx) { x += dx; }",
  804. point);
  805. point.addMethod(m);
  806. </pre></ul>
  807. <p>adds a public method <code>xmove()</code> to class <code>Point</code>.
  808. In this example, <code>x</code> is a <code>int</code> field in
  809. the class <code>Point</code>.
  810. <p>The source text passed to <code>make()</code> can refer to
  811. <code>$proceed</code> if the target object and the target method name
  812. are also given to <code>make()</code>. For example,
  813. <ul><pre>
  814. CtClass point = ClassPool.getDefault().get("Point");
  815. CtMethod m = CtNewMethod.make(
  816. "public int ymove(int dy) { $proceed(0, dy); }",
  817. point, "this", "move");
  818. </pre></ul>
  819. <p>this program creates a method <code>ymove()</code> defined below:
  820. <ul><pre>
  821. public int ymove(int dy) { this.move(0, dy); }
  822. </pre></ul>
  823. <p>Note that <code>$proceed</code> has been replaced with
  824. <code>this.move</code>.
  825. <h4>Mutual recursive methods</h4>
  826. <p>Javassist cannot compile a method if it calls another method that
  827. has not been added to a class. (Javassist can compile a method that
  828. calls itself recursively.) To add mutual recursive methods to a class,
  829. you need a trick shown below. Suppose that you want to add methods
  830. <code>m()</code> and <code>n()</code> to a class represented
  831. by <code>cc</code>:
  832. <ul><pre>
  833. CtClass cc = ... ;
  834. CtMethod m = CtNewMethod.make("public abstract int m(int i);", cc);
  835. CtMethod n = CtNewMethod.make("public abstract int n(int i);", cc);
  836. cc.addMethod(m);
  837. cc.addMethod(n);
  838. m.setBody("{ return ($1 <= 0) ? 1 : (n($1 - 1) * $1); }");
  839. n.setBody("{ return m($1); }");
  840. cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
  841. </pre></ul>
  842. <p>You must first make two abstract methods and add them to the class.
  843. Then you can give the method bodies to these methods even if the method
  844. bodies include method calls to each other. Finally you must change the
  845. class to a not-abstract class since <code>addMethod()</code> automatically
  846. changes a class into an abstract one if an abstract method is added.
  847. <h4>Adding a field</h4>
  848. <p>Javassist also allows the users to create a new field.
  849. <ul><pre>
  850. CtClass point = ClassPool.getDefault().get("Point");
  851. CtField f = new CtField(CtClass.intType, "z", point);
  852. point.addField(f);
  853. </pre></ul>
  854. <p>This program adds a field named <code>z</code> to class
  855. <code>Point</code>.
  856. <p>If the initial value of the added field must be specified,
  857. the program shown above must be modified into:
  858. <ul><pre>
  859. CtClass point = ClassPool.getDefault().get("Point");
  860. CtField f = new CtField(CtClass.intType, "z", point);
  861. point.addField(f, "0"); <em>// initial value is 0.</em>
  862. </pre></ul>
  863. <p>Now, the method <code>addField()</code> receives the second parameter,
  864. which is the source text representing an expression computing the initial
  865. value. This source text can be any Java expression if the result type
  866. of the expression matches the type of the field. Note that an expression
  867. does not end with a semi colon (<code>;</code>).
  868. <p><br>
  869. <h3>5.4 Limitations</h3>
  870. <p>In the current implementation, the Java compiler included in Javassist
  871. has several limitations with respect to the language that the compiler can
  872. accept. Those limitations are:
  873. <p><li>The <code>.class</code> notation is not supported. Use the
  874. method <code>Class.forName()</code>.
  875. In regular
  876. Java, an expression <code>Point.class</code> means a <code>Class</code>
  877. object representing the <code>Point</code> class. This notation is
  878. not available.
  879. <p><li>Array initializers, a comma-separated list of expressions
  880. enclosed by braces <code>{</code> and <code>}</code>, are not
  881. supported.
  882. <p><li>Inner classes or anonymous classes are not supported.
  883. <p><li><code>switch</code> statements are not supported yet.
  884. <p><li>Labeled <code>continue</code> and <code>break</code> statements
  885. are not supported.
  886. <p><li>The <code>finally</code> clause following
  887. <code>try</code> and <code>catch</code> clauses is not supported.
  888. <p><li>The compiler does not correctly implement the Java method dispatch
  889. algorithm. The compiler may confuse if methods defined in a class
  890. have the same name but take different parameter lists.
  891. <p><li>The users are recommended to use <code>#</code> as the separator
  892. between a class name and a static method or field name.
  893. For example, in regular Java,
  894. <ul><pre>javassist.CtClass.intType.getName()</pre></ul>
  895. <p>calls a method <code>getName()</code> on
  896. the object indicated by the static field <code>intType</code>
  897. in <code>javassist.CtClass</code>. In Javassist, the users can
  898. write the expression shown above but they are recommended to
  899. write:
  900. <ul><pre>javassist.CtClass#intType.getName()</pre></ul>
  901. <p>so that the compiler can quickly parse the expression.
  902. </ul>
  903. <p><br>
  904. <a href="tutorial.html">Previous page</a>
  905. <hr>
  906. Java(TM) is a trademark of Sun Microsystems, Inc.<br>
  907. Copyright (C) 2000-2003 by Shigeru Chiba, All rights reserved.
  908. </body>
  909. </html>