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

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