aboutsummaryrefslogtreecommitdiffstats
path: root/tutorial/tutorial3.html
blob: cb2a6b3ef0b0d468e8f1fa7f924403d41644aa1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <title>Javassist Tutorial</title>
   <link rel="stylesheet" type="text/css" href="brown.css">
</head>

<body>

<div align="right">Getting Started with Javassist</div>

<div align="left"><a href="tutorial2.html">Previous page</a></div>

<p>
<a href="#intro">5. Bytecode level API</a>
<ul>
<li><a href="#classfile">Obtaining a <code>ClassFile</code> object</a>
<br><li><a href="#member">Adding and removing a member</a>
<br><li><a href="#traverse">Traversing a method body</a>
<br><li><a href="#bytecode">Producing a bytecode sequence</a>
<br><li><a href="#annotation">Annotations (Meta tags)</a>

</ul>

<p><a href="#generics">6. Generics</a>

<p><a href="#varargs">7. Varargs</a>

<p><a href="#j2me">8. J2ME</a>

<p><a href="#boxing">9. Boxing/Unboxing

<p><a href="#debug">10. Debug</a>

<p><br>

<a name="intro">
<h2>5. Bytecode level API</h2>

<p>
Javassist also provides lower-level API for directly editing
a class file.  To use this level of API, you need detailed
knowledge of the Java bytecode and the class file format
while this level of API allows you any kind of modification
of class files.

<p>
If you want to just produce a simple class file,
<code>javassist.bytecode.ClassFileWriter</code> might provide
the best API for you.  It is much faster than
<code>javassist.bytecode.ClassFile</code> although its API
is minimum.

<a name="classfile">
<h3>5.1 Obtaining a <code>ClassFile</code> object</h3>

<p>A <code>javassist.bytecode.ClassFile</code> object represents
a class file.  To obtian this object, <code>getClassFile()</code>
in <code>CtClass</code> should be called.

<p>Otherwise, you can construct a
<code>javassist.bytecode.ClassFile</code> directly from a class file.
For example,

<ul><pre>
BufferedInputStream fin
    = new BufferedInputStream(new FileInputStream("Point.class"));
ClassFile cf = new ClassFile(new DataInputStream(fin));
</pre></ul>

<p>
This code snippet creats a <code>ClassFile</code> object from
<code>Point.class</code>.

<p>
A <code>ClassFile</code> object can be written back to a
class file.  <code>write()</code> in <code>ClassFile</code>
writes the contents of the class file to a given
<code>DataOutputStream</code>.

<p>You can create a new class file from scratch.  For example,
<blockquote><pre>
ClassFile cf = new ClassFile(false, "test.Foo", null);
cf.setInterfaces(new String[] { "java.lang.Cloneable" });
 
FieldInfo f = new FieldInfo(cf.getConstPool(), "width", "I");
f.setAccessFlags(AccessFlag.PUBLIC);
cf.addField(f);

cf.write(new DataOutputStream(new FileOutputStream("Foo.class")));
</pre></blockquote>

<p>this code generates a class file <code>Foo.class</code> that contains
the implementation of the following class:

<blockquote><pre>
package test;
class Foo implements Cloneable {
    public int width;
}
</pre></blockquote>

<p><br>

<a name="member">
<h3>5.2 Adding and removing a member</h3>

<p>
<code>ClassFile</code> provides <code>addField()</code> and
<code>addMethod()</code> for adding a field or a method (note that
a constructor is regarded as a method at the bytecode level).
It also provides <code>addAttribute()</code> for adding an attribute
to the class file.

<p>
Note that <code>FieldInfo</code>, <code>MethodInfo</code>, and
<code>AttributeInfo</code> objects include a link to a
<code>ConstPool</code> (constant pool table) object.  The <code>ConstPool</code>
object must be common to the <code>ClassFile</code> object and
a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
that is added to that <code>ClassFile</code> object.
In other words, a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
must not be shared among different <code>ClassFile</code> objects.

<p>
To remove a field or a method from a <code>ClassFile</code> object,
you must first obtain a <code>java.util.List</code>
object containing all the fields of the class.  <code>getFields()</code>
and <code>getMethods()</code> return the lists.  A field or a method can
be removed by calling <code>remove()</code> on the <code>List</code> object.
An attribute can be removed in a similar way.
Call <code>getAttributes()</code> in <code>FieldInfo</code> or
<code>MethodInfo</code> to obtain the list of attributes,
and remove one from the list.


<p><br>

<a name="traverse">
<h3>5.3 Traversing a method body</h3>

<p>
To examine every bytecode instruction in a method body,
<code>CodeIterator</code> is useful.  To otbain this object,
do as follows:

<ul><pre>
ClassFile cf = ... ;
MethodInfo minfo = cf.getMethod("move");    // we assume move is not overloaded.
CodeAttribute ca = minfo.getCodeAttribute();
CodeIterator i = ca.iterator();
</pre></ul>

<p>
A <code>CodeIterator</code> object allows you to visit every
bytecode instruction one by one from the beginning to the end.
The following methods are part of the methods declared in
<code>CodeIterator</code>:

<ul>
<li><code>void begin()</code><br>
Move to the first instruction.<br>
<li><code>void move(int index)</code><br>
Move to the instruction specified by the given index.<br>
<li><code>boolean hasNext()</code><br>
Returns true if there is more instructions.<br>
<li><code>int next()</code><br>
Returns the index of the next instruction.<br>
<em>Note that it does not return the opcode of the next
instruction.</em><br>
<li><code>int byteAt(int index)</code><br>
Returns the unsigned 8bit value at the index.<br>
<li><code>int u16bitAt(int index)</code><br>
Returns the unsigned 16bit value at the index.<br>
<li><code>int write(byte[] code, int index)</code><br>
Writes a byte array at the index.<br>
<li><code>void insert(int index, byte[] code)</code><br>
Inserts a byte array at the index.
Branch offsets etc. are automatically adjusted.<br>
</ul>

<p>The following code snippet displays all the instructions included
in a method body:

<ul><pre>
CodeIterator ci = ... ;
while (ci.hasNext()) {
    int index = ci.next();
    int op = ci.byteAt(index);
    System.out.println(Mnemonic.OPCODE[op]);
}
</pre></ul>

<p><br>

<a name="bytecode">
<h3>5.4 Producing a bytecode sequence</h3>

<p>
A <code>Bytecode</code> object represents a sequence of bytecode
instructions.  It is a growable array of bytecode.
Here is a sample code snippet:

<ul><pre>
ConstPool cp = ...;    // constant pool table
Bytecode b = new Bytecode(cp, 1, 0);
b.addIconst(3);
b.addReturn(CtClass.intType);
CodeAttribute ca = b.toCodeAttribute();
</pre></ul>

<p>
This produces the code attribute representing the following sequence:

<ul><pre>
iconst_3
ireturn
</pre></ul>

<p>
You can also obtain a byte array containing this sequence by
calling <code>get()</code> in <code>Bytecode</code>.  The
obtained array can be inserted in another code attribute.

<p>
While <code>Bytecode</code> provides a number of methods for adding a
specific instruction to the sequence, it provides
<code>addOpcode()</code> for adding an 8bit opcode and
<code>addIndex()</code> for adding an index.
The 8bit value of each opcode is defined in the <code>Opcode</code>
interface.

<p>
<code>addOpcode()</code> and other methods for adding a specific
instruction are automatically maintain the maximum stack depth
unless the control flow does not include a branch.
This value can be obtained by calling <code>getMaxStack()</code>
on the <code>Bytecode</code> object.
It is also reflected on the <code>CodeAttribute</code> object
constructed from the <code>Bytecode</code> object.
To recompute the maximum stack depth of a method body,
call <code>computeMaxStack()</code> in <code>CodeAttribute</code>.

<p><code>Bytecode</code> can be used to construct a method.
For example,

<blockquote><pre>
ClassFile cf = ...
Bytecode code = new Bytecode(cf.getConstPool());
code.addAload(0);
code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
code.addReturn(null);
code.setMaxLocals(1);

MethodInfo minfo = new MethodInfo(cf.getConstPool(), MethodInfo.nameInit, "()V");
minfo.setCodeAttribute(code.toCodeAttribute());
cf.addMethod(minfo);
</pre></blockquote>

<p>this code makes the default constructor and adds it to the class specified
by <code>cf</code>.  The <code>Bytecode</code> object is first converted into
a <code>CodeAttribute</code> object and then added to the method specified
by <code>minfo</code>.  The method is finally added to a class file <code>cf</code>. 

<p><br>

<a name="annotation">
<h3>5.5 Annotations (Meta tags)</h3>

<p>Annotations are stored in a class file
as runtime invisible (or visible) annotations attribute.
These attributes can be obtained from <code>ClassFile</code>,
<code>MethodInfo</code>, or <code>FieldInfo</code> objects.
Call <code>getAttribute(AnnotationsAttribute.invisibleTag)</code>
on those objects.  For more details, see the javadoc manual
of <code>javassist.bytecode.AnnotationsAttribute</code> class
and the <code>javassist.bytecode.annotation</code> package.

<p>Javassist also let you access annotations by the higher-level
API.
If you want to access annotations through <code>CtClass</code>,
call <code>getAnnotations()</code> in <code>CtClass</code> or
<code>CtBehavior</code>.

<p><br>

<h2><a name="generics">6. Generics</a></h2>

<p>The lower-level API of Javassist fully supports generics
introduced by Java 5.  On the other hand, the higher-level
API such as <code>CtClass</code> does not directly support
generics.  However, this is not a serious problem for bytecode
transformation.

<p>The generics of Java is implemented by the erasure technique.
After compilation, all type parameters are dropped off.  For
example, suppose that your source code declares a parameterized
type <code>Vector&lt;String&gt;</code>:

<ul><pre>
Vector&lt;String&gt; v = new Vector&lt;String&gt();
  :
String s = v.get(0);
</pre></ul>

<p>The compiled bytecode is equivalent to the following code:

<ul><pre>
Vector v = new Vector();
  :
String s = (String)v.get(0);
</pre></ul>

<p>So when you write a bytecode transformer, you can just drop
off all type parameters.  Because the compiler embedded in Javassist
does not support generics,
you must insert an explicit type cast at the
caller site if the source code is compiled by Javassist, for example,
through <code>CtMethod.make()</code>.  No type cast
is necessary if the source code is compiled by a normal Java compiler
such as <code>javac</code>.

<p>For example, if you have a class:

<ul><pre>
public class Wrapper&lt;T&gt; {
  T value;
  public Wrapper(T t) { value = t; }
}
</pre></ul>

<p>and want to add an interface <code>Getter&lt;T&gt;</code> to the
class <code>Wrapper&lt;T&gt;</code>:

<ul><pre>
public interface Getter&lt;T&gt; {
  T get();
}
</pre></ul>

<p>then the interface you really have to add is <code>Getter</code>
(the type parameters <code>&lt;T&gt;</code> drops off)
and the method you also have to add to the <code>Wrapper</code>
class is this simple one:

<ul><pre>
public Object get() { return value; }
</pre></ul>

<p>Note that no type parameters are necessary.
Since <code>get</code> returns an <code>Object</code>, an explicit type cast
is needed at the caller site if the source code is compiled by Javassist.
For example, if the type parameter <code>T</code>
is <code>String</code>, then <code>(String)</code> must be inserted as follows:

<ul><pre>
Wrapper w = ...
String s = (String)w.get();
</pre></ul>

<p>The type cast is not needed if the source code is compiled by a normal Java
compiler because it will automatically insert a type cast. 

<p>If you need to make type parameters accessible through reflection
during runtime, you have to add generic signatures to the class file.
For more details, see the API documentation (javadoc) of the
<code>setGenericSignature</code> method in the <code>CtClass</code>.

<p><br>

<h2><a name="varargs">7. Varargs</a></h2>

<p>Currently, Javassist does not directly support varargs.  So to make a method with varargs,
you must explicitly set a method modifier.  But this is easy.
Suppose that now you want to make the following method:

<ul><pre>
public int length(int... args) { return args.length; }
</pre></ul>

<p>The following code using Javassist will make the method shown above:

<ul><pre>
CtClass cc = /* target class */;
CtMethod m = CtMethod.make("public int length(int[] args) { return args.length; }", cc);
m.setModifiers(m.getModifiers() | Modifier.VARARGS);
cc.addMethod(m);
<pre></ul>

<p>The parameter type <code>int...</code> is changed into <code>int[]</code>
and <code>Modifier.VARARGS</code> is added to the method modifiers.

<p>To call this method in the source code compiled by the compiler embedded in Javassist,
you must write:

<ul><pre>
length(new int[] { 1, 2, 3 });
</pre></ul>

<p>instead of this method call using the varargs mechanism:

<ul><pre>
length(1, 2, 3);
</pre></ul>

<p><br>

<h2><a name="j2me">8. J2ME</a></h2>

<p>If you modify a class file for the J2ME execution environment,
you must perform <it>preverification</it>.  Preverifying is basically
producing stack maps, which is similar to stack map tables introduced
into J2SE at JDK 1.6.  Javassist maintains the stack maps for J2ME only if
<code>javassist.bytecode.MethodInfo.doPreverify</code> is true.

<p>You can also manually
produce a stack map for a modified method.
For a given method represented by a <code>CtMethod</code> object <code>m</code>,
you can produce a stack map by calling the following methods:

<ul><pre>
m.getMethodInfo().rebuildStackMapForME(cpool);
</pre></ul>

<p>Here, <code>cpool</code> is a <code>ClassPool</code> object, which is
available by calling <code>getClassPool()</code> on a <code>CtClass</code>
object.  A <code>ClassPool</code> object is responsible for finding
class files from given class pathes.  To obtain all the <code>CtMethod</code>
objects, call the <code>getDeclaredMethods</code> method on a <code>CtClass</code> object.

<p><br>

<h2><a name="boxing">9. Boxing/Unboxing</h2>

<p>Boxing and unboxing in Java are syntactic sugar.  There is no bytecode for
boxing or unboxing.  So the compiler of Javassist does not support them.
For example, the following statement is valid in Java:

<ul><pre>
Integer i = 3;
</pre></ul>

<p>since boxing is implicitly performed.  For Javassist, however, you must explicitly
convert a value type from <code>int</code> to <code>Integer</code>:

<ul><pre>
Integer i = new Integer(3);
</pre></ul>

<p><br>

<h2><a name="debug">10. Debug</h2>

<p>Set <code>CtClass.debugDump</code> to a directory name.
Then all class files modified and generated by Javassist are saved in that
directory.  To stop this, set <code>CtClass.debugDump</code> to null.
The default value is null.

<p>For example,

<ul><pre>
CtClass.debugDump = "./dump";
</pre></ul>

<p>All modified class files are saved in <code>./dump</code>.

<p><br>

<a href="tutorial2.html">Previous page</a>

<hr>
Java(TM) is a trademark of Sun Microsystems, Inc.<br>
Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved.
</body>
</html>