• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>&nbsp&nbsp&nbsp Local variables are not accessible.&nbsp&nbsp
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>, ... &nbsp &nbsp</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>&nbsp</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.  The type of that variable is the type of the result type (the
485return type) of the method.  If the result type is <code>void</code>,
486then the type of <code>$_</code> is <code>Object</code> and the value
487of <code>$_</code> is <code>null</code>.
488
489<p>Although the compiled code inserted by <code>insertAfter()</code>
490is executed just before the control normally returns from the method,
491it can be also executed when an exception is thrown from the method.
492To execute it when an exception is thrown, the second parameter
493<code>asFinally</code> to <code>insertAfter()</code> must be
494<code>true</code>.
495
496<p>If an exception is thrown, the compiled code inserted by
497<code>insertAfter()</code> is executed as a <code>finally</code>
498clause.  The value of <code>$_</code> is <code>0</code> or
499<code>null</code> in the compiled code.  After the execution of the
500compiled code terminates, the exception originally thrown is re-thrown
501to the caller.  Note that the value of <code>$_</code> is never thrown
502to the caller; it is rather discarded.
503
504<h4>$sig</h4>
505
506<p>The value of <code>$sig</code> is an array of
507<code>java.lang.Class</code> objects that represent the formal
508parameter types in declaration order.
509
510<h4>$type</h4>
511
512<p>The value of <code>$type</code> is an <code>java.lang.Class</code>
513object representing the formal type of the result value.  This
514variable refers to <code>Void.class</code> if this is a constructor.
515
516<h4>$class</h4>
517
518<p>The value of <code>$class</code> is an <code>java.lang.Class</code>
519object representing the class in which the edited method is declared.
520This represents the type of <code>$0</code>.
521
522<h4>addCatch()</h4>
523
524<p><code>addCatch()</code> inserts a code fragment into a method body
525so that the code fragment is executed when the method body throws
526an exception and the control returns to the caller.  In the source
527text representing the inserted code fragment, the exception value
528is referred to with the special variable <code>$e</code>.
529
530<p>For example, this program:
531
532<ul><pre>
533CtMethod m = ...;
534CtClass etype = ClassPool.getDefault().get("java.io.IOException");
535m.addCatch("{ System.out.println($e); throw $e; }", etype);
536</pre></ul>
537
538<p>translates the method body represented by <code>m</code> into
539something like this:
540
541<ul><pre>
542try {
543    <font face="serif"><em>the original method body</em></font>
544}
545catch (java.io.IOException e) {
546    System.out.println(e);
547    throw e;
548}
549</pre></ul>
550
551<p>Note that the inserted code fragment must end with a
552<code>throw</code> or <code>return</code> statement.
553
554<p><br>
555
556<a name="alter">
557<h3>4.2 Altering a method body</h3>
558
559<p><code>CtMethod</code> and <code>CtConstructor</code> provide
560<code>setBody()</code> for substituting a whole
561method body.  They compile the given source text into Java bytecode
562and substitutes it for the original method body.  If the given source
563text is <code>null</code>, the substituted body includes only a
564<code>return</code> statement, which returns zero or null unless the
565result type is <code>void</code>.
566
567<p>In the source text given to <code>setBody()</code>, the identifiers
568starting with <code>$</code> have special meaning
569
570<ul><table border=0>
571<tr>
572<td><code>$0</code>, <code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
573<td><code>this</code> and actual parameters</td>
574</tr>
575
576<tr>
577<td><code>$args</code></td>
578<td>An array of parameters.
579The type of <code>$args</code> is <code>Object[]</code>.
580</td>
581</tr>
582
583<tr>
584<td><code>$$</code></td>
585<td>All actual parameters.<br>
586</tr>
587
588<tr>
589<td><code>$cflow(</code>...<code>)</code></td>
590<td><code>cflow</code> variable</td>
591</tr>
592
593<tr>
594<td><code>$r</code></td>
595<td>The result type.  It is used in a cast expression.</td>
596</tr>
597
598<tr>
599<td><code>$w</code></td>
600<td>The wrapper type.  It is used in a cast expression.</td>
601</tr>
602
603<tr>
604<td><code>$sig</code></td>
605<td>An array of <code>java.lang.Class</code> objects representing
606the formal parameter types.
607</td>
608</tr>
609
610<tr>
611<td><code>$type</code></td>
612<td>A <code>java.lang.Class</code> object representing
613the formal result type.</td>
614</tr>
615
616<tr>
617<td><code>$class</code></td>
618<td rowspan=2>A <code>java.lang.Class</code> object representing
619the class that declares the method<br>
620currently edited (the type of $0).</td>
621</tr>
622
623<tr><td>&nbsp</td></tr>
624
625</table>
626</ul>
627
628Note that <code>$_</code> is not available.
629
630<h4>Substituting source text for an existing expression</h4>
631
632<p>Javassist allows modifying only an expression included in a method body.
633<code>javassist.expr.ExprEditor</code> is a class
634for replacing an expression in a method body.
635The users can define a subclass of <code>ExprEditor</code>
636to specify how an expression is modified.
637
638<p>To run an <code>ExprEditor</code> object, the users must
639call <code>instrument()</code> in <code>CtMethod</code> or
640<code>CtClass</code>.
641
642For example,
643
644<ul><pre>
645CtMethod cm = ... ;
646cm.instrument(
647    new ExprEditor() {
648        public void edit(MethodCall m)
649                      throws CannotCompileException
650        {
651            if (m.getClassName().equals("Point")
652                          && m.getMethodName().equals("move"))
653                m.replace("{ $1 = 0; $_ = $proceed($$); }");
654        }
655    });
656</pre></ul>
657
658<p>searches the method body represented by <code>cm</code> and
659replaces all calls to <code>move()</code> in class <code>Point</code>
660with a block:
661
662<ul><pre>{ $1 = 0; $_ = $proceed($$); }
663</pre></ul>
664
665<p>so that the first parameter to <code>move()</code> is always 0.
666Note that the substituted code is not an expression but
667a statement or a block.  It cannot be or contain a try-catch statement.
668
669<p>The method <code>instrument()</code> searches a method body.
670If it finds an expression such as a method call, field access, and object
671creation, then it calls <code>edit()</code> on the given
672<code>ExprEditor</code> object.  The parameter to <code>edit()</code>
673is an object representing the found expression.  The <code>edit()</code>
674method can inspect and replace the expression through that object.
675
676<p>Calling <code>replace()</code> on the parameter to <code>edit()</code>
677substitutes the given statement or block for the expression.  If the given
678block is an empty block, that is, if <code>replace("{}")</code>
679is executed, then the expression is removed from the method body.
680
681If you want to insert a statement (or a block) before/after the
682expression, a block like the following should be passed to
683<code>replace()</code>:
684
685<ul><pre>
686{ <em>before-statements;</em>
687  $_ = $proceed($$);
688  <em>after-statements;</em> }
689</pre></ul>
690
691<p>whichever the expression is either a method call, field access,
692object creation, or others.  The second statement could be:
693
694<ul><pre>$_ = $proceed();</pre></ul>
695
696<p>if the expression is read access, or
697
698<ul><pre>$proceed($$);</pre></ul>
699
700<p>if the expression is write access.
701
702<p>Local variables available in the target expression is
703also available in the source text passed to <code>replace()</code>
704if the method searched by <code>instrument()</code> was compiled
705with the -g option (the class file includes a local variable
706attribute).
707
708<h4>javassist.expr.MethodCall</h4>
709
710<p>A <code>MethodCall</code> object represents a method call.
711The method <code>replace()</code> in
712<code>MethodCall</code> substitutes a statement or
713a block for the method call.
714It receives source text representing the substitued statement or
715block, in which the identifiers starting with <code>$</code>
716have special meaning as in the source text passed to
717<code>insertBefore()</code>.
718
719<ul><table border=0>
720<tr>
721<td><code>$0</code></td>
722<td rowspan=3>
723The target object of the method call.<br>
724This is not equivalent to <code>this</code>, which represents
725the caller-side <code>this</code> object.<br>
726<code>$0</code> is <code>null</code> if the method is static.
727</td>
728</tr>
729
730<tr><td>&nbsp</td></tr>
731
732<tr><td>&nbsp</td></tr>
733
734<tr>
735<td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
736<td>
737The parameters of the method call.
738</td>
739</tr>
740
741<tr><td>
742<code>$_</code></td>
743<td>The resulting value of the method call.</td>
744</tr>
745
746<tr><td><code>$r</code></td>
747<td>The result type of the method call.</td>
748</tr>
749
750<tr><td><code>$class</code> &nbsp &nbsp</td>
751<td>A <code>java.lang.Class</code> object representing
752the class declaring the method.
753</td>
754</tr>
755
756<tr><td><code>$sig</code> &nbsp &nbsp</td>
757<td>An array of <code>java.lang.Class</code> objects representing
758the formal parameter types.</td>
759</tr>
760
761<tr><td><code>$type</code> &nbsp &nbsp</td>
762<td>A <code>java.lang.Class</code> object representing
763the formal result type.</td>
764</tr>
765
766<tr><td><code>$proceed</code> &nbsp &nbsp</td>
767<td>The name of the method originally called
768in the expression.</td>
769</tr>
770
771</table>
772</ul>
773
774<p>Here the method call means the one represented by the
775<code>MethodCall</code> object.
776
777<p>The other identifiers such as <code>$w</code>,
778<code>$args</code> and <code>$$</code>
779are also available.
780
781<p>Unless the result type of the method call is <code>void</code>,
782a value must be assigned to
783<code>$_</code> in the source text and the type of <code>$_</code>
784is the result type.
785If the result type is <code>void</code>, the type of <code>$_</code>
786is <code>Object</code> and the value assigned to <code>$_</code>
787is ignored.
788
789<p><code>$proceed</code> is not a <code>String</code> value but special
790syntax.  It must be followed by an argument list surrounded by parentheses
791<code>( )</code>.
792
793<h4>javassist.expr.ConstructorCall</h4>
794
795<p>A <code>ConstructorCall</code> object represents a constructor call
796such as <code>this()</code> and <code>super</code> included in a constructor
797body.
798The method <code>replace()</code> in
799<code>ConstructorCall</code> substitutes a statement or
800a block for the constructor call.
801It receives source text representing the substituted statement or
802block, in which the identifiers starting with <code>$</code>
803have special meaning as in the source text passed to
804<code>insertBefore()</code>.
805
806<ul><table border=0>
807<tr>
808<td><code>$0</code></td>
809<td>
810The target object of the constructor call.
811This is equivalent to <code>this</code>.
812</td>
813</tr>
814
815<tr>
816<td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
817<td>
818The parameters of the constructor call.
819</td>
820</tr>
821
822<tr><td><code>$class</code> &nbsp &nbsp</td>
823<td>A <code>java.lang.Class</code> object representing
824the class declaring the constructor.
825</td>
826</tr>
827
828<tr><td><code>$sig</code> &nbsp &nbsp</td>
829<td>An array of <code>java.lang.Class</code> objects representing
830the formal parameter types.</td>
831</tr>
832
833<tr><td><code>$proceed</code> &nbsp &nbsp</td>
834<td>The name of the constructor originally called
835in the expression.</td>
836</tr>
837
838</table>
839</ul>
840
841<p>Here the constructor call means the one represented by the
842<code>ConstructorCall</code> object.
843
844<p>The other identifiers such as <code>$w</code>,
845<code>$args</code> and <code>$$</code>
846are also available.
847
848<p>Since any constructor must call either a constructor of the super
849class or another constructor of the same class,
850the substituted statement must include a constructor call,
851normally a call to <code>$proceed()</code>.
852
853<p><code>$proceed</code> is not a <code>String</code> value but special
854syntax.  It must be followed by an argument list surrounded by parentheses
855<code>( )</code>.
856
857<h4>javassist.expr.FieldAccess</h4>
858
859<p>A <code>FieldAccess</code> object represents field access.
860The method <code>edit()</code> in <code>ExprEditor</code>
861receives this object if field access is found.
862The method <code>replace()</code> in
863<code>FieldAccess</code> receives
864source text representing the substitued statement or
865block for the field access.
866
867<p>
868In the source text, the identifiers starting with <code>$</code>
869have special meaning:
870
871<ul><table border=0>
872<tr>
873<td><code>$0</code></td>
874<td rowspan=3>
875The object containing the field accessed by the expression.
876This is not equivalent to <code>this</code>.<br>
877<code>this</code> represents the object that the method including the
878expression is invoked on.<br>
879<code>$0</code> is <code>null</code> if the field is static.
880</td>
881</tr>
882
883<tr><td>&nbsp</td></tr>
884
885<tr><td>&nbsp</td></tr>
886
887<tr>
888<td><code>$1</code></td>
889<td rowspan=2>
890The value that would be stored in the field
891if the expression is write access.
892<br>Otherwise, <code>$1</code> is not available.
893</td>
894</tr>
895
896<tr><td>&nbsp</td></tr>
897
898<tr>
899<td><code>$_</code></td>
900<td rowspan=2>
901The resulting value of the field access
902if the expression is read access.
903<br>Otherwise, the value stored in <code>$_</code> is discarded.
904</td>
905</tr>
906
907<tr><td>&nbsp</td></tr>
908<tr>
909<td><code>$r</code></td>
910<td rowspan=2>
911The type of the field if the expression is read access.
912<br>Otherwise, <code>$r</code> is <code>void</code>.
913</td>
914</tr>
915
916<tr><td>&nbsp</td></tr>
917
918<tr><td><code>$class</code> &nbsp &nbsp</td>
919<td>A <code>java.lang.Class</code> object representing
920the class declaring the field.
921</td></tr>
922
923<tr><td><code>$type</code></td>
924<td>A <code>java.lang.Class</code> object representing
925the field type.</td>
926</tr>
927
928<tr><td><code>$proceed</code> &nbsp &nbsp</td>
929<td>The name of a virtual method executing the original
930field access.
931.</td>
932</tr>
933
934</table>
935</ul>
936
937<p>The other identifiers such as <code>$w</code>,
938<code>$args</code> and <code>$$</code>
939are also available.
940
941<p>If the expression is read access, a value must be assigned to
942<code>$_</code> in the source text.  The type of <code>$_</code>
943is the type of the field.
944
945<h4>javassist.expr.NewExpr</h4>
946
947<p>A <code>NewExpr</code> object represents object creation
948with the <code>new</code> operator (not including array creation).
949The method <code>edit()</code> in <code>ExprEditor</code>
950receives this object if object creation is found.
951The method <code>replace()</code> in
952<code>NewExpr</code> receives
953source text representing the substitued statement or
954block for the object creation.
955
956<p>
957In the source text, the identifiers starting with <code>$</code>
958have special meaning:
959
960<ul><table border=0>
961
962<tr>
963<td><code>$0</code></td>
964<td>
965<code>null</code>.
966</td>
967</tr>
968
969<tr>
970<td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
971<td>
972The parameters to the constructor.
973</td>
974</tr>
975
976<tr>
977<td><code>$_</code></td>
978<td rowspan=2>
979The resulting value of the object creation.
980<br>A newly created object must be stored in this variable.
981</td>
982</tr>
983
984<tr><td>&nbsp</td></tr>
985
986<tr>
987<td><code>$r</code></td>
988<td>
989The type of the created object.
990</td>
991</tr>
992
993<tr><td><code>$sig</code> &nbsp &nbsp</td>
994<td>An array of <code>java.lang.Class</code> objects representing
995the formal parameter types.</td>
996</tr>
997
998<tr><td><code>$type</code> &nbsp &nbsp</td>
999<td>A <code>java.lang.Class</code> object representing
1000the class of the created object.
1001</td></tr>
1002
1003<tr><td><code>$proceed</code> &nbsp &nbsp</td>
1004<td>The name of a virtual method executing the original
1005object creation.
1006.</td>
1007</tr>
1008
1009</table>
1010</ul>
1011
1012<p>The other identifiers such as <code>$w</code>,
1013<code>$args</code> and <code>$$</code>
1014are also available.
1015
1016<h4>javassist.expr.NewArray</h4>
1017
1018<p>A <code>NewArray</code> object represents array creation
1019with the <code>new</code> operator.
1020The method <code>edit()</code> in <code>ExprEditor</code>
1021receives this object if array creation is found.
1022The method <code>replace()</code> in
1023<code>NewArray</code> receives
1024source text representing the substitued statement or
1025block for the array creation.
1026
1027<p>
1028In the source text, the identifiers starting with <code>$</code>
1029have special meaning:
1030
1031<ul><table border=0>
1032
1033<tr>
1034<td><code>$0</code></td>
1035<td>
1036<code>null</code>.
1037</td>
1038</tr>
1039
1040<tr>
1041<td><code>$1</code>, <code>$2</code>, ... &nbsp &nbsp</td>
1042<td>
1043The size of each dimension.
1044</td>
1045</tr>
1046
1047<tr>
1048<td><code>$_</code></td>
1049<td rowspan=2>
1050The resulting value of the array creation.
1051<br>A newly created array must be stored in this variable.
1052</td>
1053</tr>
1054
1055<tr><td>&nbsp</td></tr>
1056
1057<tr>
1058<td><code>$r</code></td>
1059<td>
1060The type of the created array.
1061</td>
1062</tr>
1063
1064<tr><td><code>$type</code> &nbsp &nbsp</td>
1065<td>A <code>java.lang.Class</code> object representing
1066the class of the created array.
1067</td></tr>
1068
1069<tr><td><code>$proceed</code> &nbsp &nbsp</td>
1070<td>The name of a virtual method executing the original
1071array creation.
1072.</td>
1073</tr>
1074
1075</table>
1076</ul>
1077
1078<p>The other identifiers such as <code>$w</code>,
1079<code>$args</code> and <code>$$</code>
1080are also available.
1081
1082<p>For example, if the array creation is the following expression,
1083
1084<ul><pre>
1085String[][] s = new String[3][4];
1086</pre></ul>
1087
1088then the value of $1 and $2 are 3 and 4, respectively.  $3 is not available.
1089
1090<p>If the array creation is the following expression,
1091
1092<ul><pre>
1093String[][] s = new String[3][];
1094</pre></ul>
1095
1096then the value of $1 is 3 but $2 is not available.
1097
1098<h4>javassist.expr.Instanceof</h4>
1099
1100<p>A <code>Instanceof</code> object represents an <code>instanceof</code>
1101expression.
1102The method <code>edit()</code> in <code>ExprEditor</code>
1103receives this object if an instanceof expression is found.
1104The method <code>replace()</code> in
1105<code>Instanceof</code> receives
1106source text representing the substitued statement or
1107block for the expression.
1108
1109<p>
1110In the source text, the identifiers starting with <code>$</code>
1111have special meaning:
1112
1113<ul><table border=0>
1114
1115<tr>
1116<td><code>$0</code></td>
1117<td>
1118<code>null</code>.
1119</td>
1120</tr>
1121
1122<tr>
1123<td><code>$1</code></td>
1124<td>
1125The value on the left hand side of the original
1126<code>instanceof</code> operator.
1127</td>
1128</tr>
1129
1130<tr>
1131<td><code>$_</code></td>
1132<td>
1133The resulting value of the expression.
1134The type of <code>$_</code> is <code>boolean</code>.
1135</td>
1136</tr>
1137
1138<tr>
1139<td><code>$r</code></td>
1140<td>
1141The type on the right hand side of the <code>instanceof</code> operator.
1142</td>
1143</tr>
1144
1145<tr><td><code>$type</code></td>
1146<td>A <code>java.lang.Class</code> object representing
1147the type on the right hand side of the <code>instanceof</code> operator.
1148</td>
1149</tr>
1150
1151<tr><td><code>$proceed</code> &nbsp &nbsp</td>
1152<td rowspan=4>The name of a virtual method executing the original
1153<code>instanceof</code> expression.
1154<br>It takes one parameter (the type is <code>java.lang.Object</code>)
1155and returns true
1156<br>if the parameter value is an instance of the type on the right
1157hand side of
1158<br>the original <code>instanceof</code> operator.
1159Otherwise, it returns false.
1160</td>
1161</tr>
1162
1163<tr><td>&nbsp</td></tr>
1164<tr><td>&nbsp</td></tr>
1165<tr><td>&nbsp</td></tr>
1166
1167</table>
1168</ul>
1169
1170<p>The other identifiers such as <code>$w</code>,
1171<code>$args</code> and <code>$$</code>
1172are also available.
1173
1174<h4>javassist.expr.Cast</h4>
1175
1176<p>A <code>Cast</code> object represents an expression for
1177explicit type casting.
1178The method <code>edit()</code> in <code>ExprEditor</code>
1179receives this object if explicit type casting is found.
1180The method <code>replace()</code> in
1181<code>Cast</code> receives
1182source text representing the substitued statement or
1183block for the expression.
1184
1185<p>
1186In the source text, the identifiers starting with <code>$</code>
1187have special meaning:
1188
1189<ul><table border=0>
1190
1191<tr>
1192<td><code>$0</code></td>
1193<td>
1194<code>null</code>.
1195</td>
1196</tr>
1197
1198<tr>
1199<td><code>$1</code></td>
1200<td>
1201The value the type of which is explicitly cast.
1202</td>
1203</tr>
1204
1205<tr>
1206<td><code>$_</code></td>
1207<td rowspan=2>
1208The resulting value of the expression.
1209The type of <code>$_</code> is the same as the type
1210<br>after the explicit casting, that is, the type surrounded
1211by <code>( )</code>.
1212</td>
1213</tr>
1214
1215<tr><td>&nbsp</td></tr>
1216
1217<tr>
1218<td><code>$r</code></td>
1219<td>the type after the explicit casting, or the type surrounded
1220by <code>( )</code>.
1221</td>
1222</tr>
1223
1224<tr><td><code>$type</code></td>
1225<td>A <code>java.lang.Class</code> object representing
1226the same type as <code>$r</code>.
1227</td>
1228</tr>
1229
1230<tr><td><code>$proceed</code> &nbsp &nbsp</td>
1231<td rowspan=3>The name of a virtual method executing the original
1232type casting.
1233<br>It takes one parameter of the type <code>java.lang.Object</code>
1234and returns it after
1235<br>the explicit type casting specified by the original expression.
1236
1237</td>
1238</tr>
1239
1240<tr><td>&nbsp</td></tr>
1241
1242<tr><td>&nbsp</td></tr>
1243
1244</table>
1245</ul>
1246
1247<p>The other identifiers such as <code>$w</code>,
1248<code>$args</code> and <code>$$</code>
1249are also available.
1250
1251<h4>javassist.expr.Handler</h4>
1252
1253<p>A <code>Handler</code> object represents a <code>catch</code>
1254clause of <code>try-catch</code> statement.
1255The method <code>edit()</code> in <code>ExprEditor</code>
1256receives this object if a <code>catch</code> is found.
1257The method <code>insertBefore()</code> in
1258<code>Handler</code> compiles the received
1259source text and inserts it at the beginning of the <code>catch</code> clause.
1260
1261<p>
1262In the source text, the identifiers starting with <code>$</code>
1263have meaning:
1264
1265<ul><table border=0>
1266
1267<tr>
1268<td><code>$1</code></td>
1269<td>
1270The exception object caught by the <code>catch</code> clause.
1271</td>
1272</tr>
1273
1274<tr>
1275<td><code>$r</code></td>
1276<td>the type of the exception caught by the <code>catch</code> clause.
1277It is used in a cast expression.
1278</td>
1279</tr>
1280
1281<tr>
1282<td><code>$w</code></td>
1283<td>The wrapper type.  It is used in a cast expression.
1284</td>
1285</tr>
1286
1287<tr><td><code>$type</code> &nbsp &nbsp</td>
1288<td rowspan=2>
1289A <code>java.lang.Class</code> object representing
1290<br>the type of the exception caught by the <code>catch</code> clause.
1291</td>
1292</tr>
1293
1294<tr><td>&nbsp</td></tr>
1295
1296</table>
1297</ul>
1298
1299<p>If a new exception object is assigned to <code>$1</code>,
1300it is passed to the original <code>catch</code> clause as the caught
1301exception.
1302
1303<p><br>
1304
1305<a name="add">
1306<h3>4.3 Adding a new method or field</h3>
1307
1308<h4>Adding a method</h4>
1309
1310<p>Javassist allows the users to create a new method and constructor
1311from scratch.  <code>CtNewMethod</code>
1312and <code>CtNewConstructor</code> provide several factory methods,
1313which are static methods for creating <code>CtMethod</code> or
1314<code>CtConstructor</code> objects.
1315Especially, <code>make()</code> creates
1316a <code>CtMethod</code> or <code>CtConstructor</code> object
1317from the given source text.
1318
1319<p>For example, this program:
1320
1321<ul><pre>
1322CtClass point = ClassPool.getDefault().get("Point");
1323CtMethod m = CtNewMethod.make(
1324                 "public int xmove(int dx) { x += dx; }",
1325                 point);
1326point.addMethod(m);
1327</pre></ul>
1328
1329<p>adds a public method <code>xmove()</code> to class <code>Point</code>.
1330In this example, <code>x</code> is a <code>int</code> field in
1331the class <code>Point</code>.
1332
1333<p>The source text passed to <code>make()</code> can include the
1334identifiers starting with <code>$</code> except <code>$_</code>
1335as in <code>setBody()</code>.
1336It can also include
1337<code>$proceed</code> if the target object and the target method name
1338are also given to <code>make()</code>.  For example,
1339
1340<ul><pre>
1341CtClass point = ClassPool.getDefault().get("Point");
1342CtMethod m = CtNewMethod.make(
1343                 "public int ymove(int dy) { $proceed(0, dy); }",
1344                 point, "this", "move");
1345</pre></ul>
1346
1347<p>this program creates a method <code>ymove()</code> defined below:
1348
1349<ul><pre>
1350public int ymove(int dy) { this.move(0, dy); }
1351</pre></ul>
1352
1353<p>Note that <code>$proceed</code> has been replaced with
1354<code>this.move</code>.
1355
1356<p>Javassist provides another way to add a new method.
1357You can first create an abstract method and later give it a method body:
1358
1359<ul><pre>
1360CtClass cc = ... ;
1361CtMethod m = new CtMethod(CtClass.intType, "move",
1362                          new CtClass[] { CtClass.intType }, cc);
1363cc.addMethod(m);
1364m.setBody("{ x += $1; }");
1365cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
1366</pre></ul>
1367
1368<p>Since Javassist makes a class abstract if an abstract method is
1369added to the class, you have to explicitly change the class back to a
1370non-abstract one after calling <code>setBody()</code>.
1371
1372
1373<h4>Mutual recursive methods</h4>
1374
1375<p>Javassist cannot compile a method if it calls another method that
1376has not been added to a class.  (Javassist can compile a method that
1377calls itself recursively.)  To add mutual recursive methods to a class,
1378you need a trick shown below.  Suppose that you want to add methods
1379<code>m()</code> and <code>n()</code> to a class represented
1380by <code>cc</code>:
1381
1382<ul><pre>
1383CtClass cc = ... ;
1384CtMethod m = CtNewMethod.make("public abstract int m(int i);", cc);
1385CtMethod n = CtNewMethod.make("public abstract int n(int i);", cc);
1386cc.addMethod(m);
1387cc.addMethod(n);
1388m.setBody("{ return ($1 <= 0) ? 1 : (n($1 - 1) * $1); }");
1389n.setBody("{ return m($1); }");
1390cc.setModifiers(cc.getModifiers() & ~Modifier.ABSTRACT);
1391</pre></ul>
1392
1393<p>You must first make two abstract methods and add them to the class.
1394Then you can give the method bodies to these methods even if the method
1395bodies include method calls to each other.  Finally you must change the
1396class to a not-abstract class since <code>addMethod()</code> automatically
1397changes a class into an abstract one if an abstract method is added.
1398
1399<h4>Adding a field</h4>
1400
1401<p>Javassist also allows the users to create a new field.
1402
1403<ul><pre>
1404CtClass point = ClassPool.getDefault().get("Point");
1405CtField f = new CtField(CtClass.intType, "z", point);
1406point.addField(f);
1407</pre></ul>
1408
1409<p>This program adds a field named <code>z</code> to class
1410<code>Point</code>.
1411
1412<p>If the initial value of the added field must be specified,
1413the program shown above must be modified into:
1414
1415<ul><pre>
1416CtClass point = ClassPool.getDefault().get("Point");
1417CtField f = new CtField(CtClass.intType, "z", point);
1418point.addField(f, "0");    <em>// initial value is 0.</em>
1419</pre></ul>
1420
1421<p>Now, the method <code>addField()</code> receives the second parameter,
1422which is the source text representing an expression computing the initial
1423value.  This source text can be any Java expression if the result type
1424of the expression matches the type of the field.  Note that an expression
1425does not end with a semi colon (<code>;</code>).
1426
1427<p>Furthermore, the above code can be rewritten into the following
1428simple code:
1429
1430<ul><pre>
1431CtClass point = ClassPool.getDefault().get("Point");
1432CtField f = CtField.make("public int z = 0;", point);
1433point.addField(f);
1434</pre></ul>
1435
1436<h4>Removing a member</h4>
1437
1438<p>To remove a field or a method, call <code>removeField()</code>
1439or <code>removeMethod()</code> in <code>CtClass</code>.  A
1440<code>CtConstructor</code> can be removed by <code>removeConstructor()</code>
1441in <code>CtClass</code>.
1442
1443<p><br>
1444
1445<a name="annotation">
1446<h3>4.4 Annotations</h3>
1447
1448<p><code>CtClass</code>, <code>CtMethod</code>, <code>CtField</code>
1449and <code>CtConstructor</code> provides a convenient method
1450<code>getAnnotations()</code> for reading annotations.
1451It returns an annotation-type object.
1452
1453<p>For example, suppose the following annotation:
1454
1455<ul><pre>
1456public @interface Author {
1457    String name();
1458    int year();
1459}
1460</pre></ul>
1461
1462<p>This annotation is used as the following:
1463
1464<ul><pre>
1465@Author(name="Chiba", year=2005)
1466public class Point {
1467    int x, y;
1468}
1469</pre></ul>
1470
1471<p>Then, the value of the annotation can be obtained by
1472<code>getAnnotations()</code>.
1473It returns an array containing
1474annotation-type objects.
1475
1476<ul><pre>
1477CtClass cc = ClassPool.getDefault().get("Point");
1478Object[] all = cc.getAnnotations();
1479Author a = (Author)all[0];
1480String name = a.name();
1481int year = a.year();
1482System.out.println("name: " + name + ", year: " + year);
1483</pre></ul>
1484
1485<p>This code snippet should print:
1486
1487<ul><pre>
1488name: Chiba, year: 2005
1489</pre></ul>
1490
1491<p>
1492Since the annoation of <code>Point</code> is only <code>@Author</code>,
1493the length of the array <code>all</code> is one
1494and <code>all[0]</code> is an <code>Author</code> object.
1495The member values of the annotation can be obtained
1496by calling <code>name()</code> and <code>year()</code>
1497on the <code>Author</code> object.
1498
1499<p>To use <code>getAnnotations()</code>, annotation types
1500such as <code>Author</code> must be included in the current
1501class path.  <em>They must be also accessible from a
1502<code>ClassPool</code> object.</em>  If the class file of an annotation
1503type is not found, Javassist cannot obtain the default values
1504of the members of that annotation type.
1505
1506<p><br>
1507
1508<a name="runtime">
1509<h3>4.5 Runtime support classes</h3>
1510
1511<p>In most cases, a class modified by Javassist does not require
1512Javassist to run.  However, some kinds of bytecode generated by the
1513Javassist compiler need runtime support classes, which are in the
1514<code>javassist.runtime</code> package (for details, please read
1515the API reference of that package).  Note that the
1516<code>javassist.runtime</code> package is the only package that
1517classes modified by Javassist may need for running.  The other
1518Javassist classes are never used at runtime of the modified classes.
1519
1520<p><br>
1521
1522<a name="import">
1523<h3>4.6 Import</h3>
1524
1525<p>All the class names in source code must be fully qualified
1526(they must include package names).
1527However, the <code>java.lang</code> package is an
1528exception; for example, the Javassist compiler can
1529resolve <code>Object</code> as
1530well as <code>java.lang.Object</code>.
1531
1532<p>To tell the compiler to search other packages when resolving a
1533class name, call <code>importPackage()</code> in <code>ClassPool</code>.
1534For example,
1535
1536<ul><pre>
1537ClassPool pool = ClassPool.getDefault();
1538pool.importPackage("java.awt");
1539CtClass cc = pool.makeClass("Test");
1540CtField f = CtField.make("public Point p;", cc);
1541cc.addField(f);
1542</pre></ul>
1543
1544<p>The seconde line instructs the compiler
1545to import the <code>java.awt</code> package.
1546Thus, the third line will not throw an exception.
1547The compiler can recognize <code>Point</code>
1548as <code>java.awt.Point</code>.
1549
1550<p>Note that <code>importPackage()</code> <em>does not</em> affect
1551the <code>get()</code> method in <code>ClassPool</code>.
1552Only the compiler considers the imported packages.
1553The parameter to <code>get()</code>
1554must be always a fully qualified name.
1555
1556<p><br>
1557
1558<a name="limit">
1559<h3>4.7 Limitations</h3>
1560
1561<p>In the current implementation, the Java compiler included in Javassist
1562has several limitations with respect to the language that the compiler can
1563accept.  Those limitations are:
1564
1565<p><li>The new syntax introduced by J2SE 5.0 (including enums and generics)
1566has not been supported.  Annotations are supported only by the low level
1567API of Javassist.
1568See the <code>javassist.bytecode.annotation</code> package.
1569
1570<p><li>Array initializers, a comma-separated list of expressions
1571enclosed by braces <code>{</code> and <code>}</code>, are not
1572available unless the array dimension is one.
1573
1574<p><li>Inner classes or anonymous classes are not supported.
1575
1576<p><li>Labeled <code>continue</code> and <code>break</code> statements
1577are not supported.
1578
1579<p><li>The compiler does not correctly implement the Java method dispatch
1580algorithm.  The compiler may confuse if methods defined in a class
1581have the same name but take different parameter lists.
1582
1583<p>For example,
1584
1585<ul><pre>
1586class A {}
1587class B extends A {}
1588class C extends B {}
1589
1590class X {
1591    void foo(A a) { .. }
1592    void foo(B b) { .. }
1593}
1594</pre></ul>
1595
1596<p>If the compiled expression is <code>x.foo(new C())</code>, where
1597<code>x</code> is an instance of X, the compiler may produce a call
1598to <code>foo(A)</code> although the compiler can correctly compile
1599<code>foo((B)new C())</code>.
1600
1601<p><li>The users are recommended to use <code>#</code> as the separator
1602between a class name and a static method or field name.
1603For example, in regular Java,
1604
1605<ul><pre>javassist.CtClass.intType.getName()</pre></ul>
1606
1607<p>calls a method <code>getName()</code> on
1608the object indicated by the static field <code>intType</code>
1609in <code>javassist.CtClass</code>.  In Javassist, the users can
1610write the expression shown above but they are recommended to
1611write:
1612
1613<ul><pre>javassist.CtClass#intType.getName()</pre></ul>
1614
1615<p>so that the compiler can quickly parse the expression.
1616</ul>
1617
1618<p><br>
1619
1620<a href="tutorial.html">Previous page</a>
1621&nbsp;&nbsp;&nbsp;<a href="tutorial3.html">Next page</a>
1622
1623<hr>
1624Java(TM) is a trademark of Sun Microsystems, Inc.<br>
1625Copyright (C) 2000-2010 by Shigeru Chiba, All rights reserved.
1626</body>
1627</html>
1628