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

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