選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tutorial2.html 38KB

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