You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tutorial2.html 48KB

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