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

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