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