Lines Matching full:code

17 <li><a href="#classfile">Obtaining a <code>ClassFile</code> object</a>
49 <code>javassist.bytecode.ClassFileWriter</code> might provide
51 <code>javassist.bytecode.ClassFile</code> although its API
55 <h3>5.1 Obtaining a <code>ClassFile</code> object</h3>
57 <p>A <code>javassist.bytecode.ClassFile</code> object represents
58 a class file. To obtian this object, <code>getClassFile()</code>
59 in <code>CtClass</code> should be called.
62 <code>javassist.bytecode.ClassFile</code> directly from a class file.
72 This code snippet creats a <code>ClassFile</code> object from
73 <code>Point.class</code>.
76 A <code>ClassFile</code> object can be written back to a
77 class file. <code>write()</code> in <code>ClassFile</code>
79 <code>DataOutputStream</code>.
93 <p>this code generates a class file <code>Foo.class</code> that contains
109 <code>ClassFile</code> provides <code>addField()</code> and
110 <code>addMethod()</code> for adding a field or a method (note that
112 It also provides <code>addAttribute()</code> for adding an attribute
116 Note that <code>FieldInfo</code>, <code>MethodInfo</code>, and
117 <code>AttributeInfo</code> objects include a link to a
118 <code>ConstPool</code> (constant pool table) object. The <code>ConstPool</code>
119 object must be common to the <code>ClassFile</code> object and
120 a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
121 that is added to that <code>ClassFile</code> object.
122 In other words, a <code>FieldInfo</code> (or <code>MethodInfo</code> etc.) object
123 must not be shared among different <code>ClassFile</code> objects.
126 To remove a field or a method from a <code>ClassFile</code> object,
127 you must first obtain a <code>java.util.List</code>
128 object containing all the fields of the class. <code>getFields()</code>
129 and <code>getMethods()</code> return the lists. A field or a method can
130 be removed by calling <code>remove()</code> on the <code>List</code> object.
132 Call <code>getAttributes()</code> in <code>FieldInfo</code> or
133 <code>MethodInfo</code> to obtain the list of attributes,
144 <code>CodeIterator</code> is useful. To otbain this object,
155 A <code>CodeIterator</code> object allows you to visit every
158 <code>CodeIterator</code>:
161 <li><code>void begin()</code><br>
163 <li><code>void move(int index)</code><br>
165 <li><code>boolean hasNext()</code><br>
167 <li><code>int next()</code><br>
171 <li><code>int byteAt(int index)</code><br>
173 <li><code>int u16bitAt(int index)</code><br>
175 <li><code>int write(byte[] code, int index)</code><br>
177 <li><code>void insert(int index, byte[] code)</code><br>
182 <p>The following code snippet displays all the instructions included
200 A <code>Bytecode</code> object represents a sequence of bytecode
202 Here is a sample code snippet:
213 This produces the code attribute representing the following sequence:
222 calling <code>get()</code> in <code>Bytecode</code>. The
223 obtained array can be inserted in another code attribute.
226 While <code>Bytecode</code> provides a number of methods for adding a
228 <code>addOpcode()</code> for adding an 8bit opcode and
229 <code>addIndex()</code> for adding an index.
230 The 8bit value of each opcode is defined in the <code>Opcode</code>
234 <code>addOpcode()</code> and other methods for adding a specific
237 This value can be obtained by calling <code>getMaxStack()</code>
238 on the <code>Bytecode</code> object.
239 It is also reflected on the <code>CodeAttribute</code> object
240 constructed from the <code>Bytecode</code> object.
242 call <code>computeMaxStack()</code> in <code>CodeAttribute</code>.
244 <p><code>Bytecode</code> can be used to construct a method.
249 Bytecode code = new Bytecode(cf.getConstPool());
250 code.addAload(0);
251 code.addInvokespecial("java/lang/Object", MethodInfo.nameInit, "()V");
252 code.addReturn(null);
253 code.setMaxLocals(1);
256 minfo.setCodeAttribute(code.toCodeAttribute());
260 <p>this code makes the default constructor and adds it to the class specified
261 by <code>cf</code>. The <code>Bytecode</code> object is first converted into
262 a <code>CodeAttribute</code> object and then added to the method specified
263 by <code>minfo</code>. The method is finally added to a class file <code>cf</code>.
272 These attributes can be obtained from <code>ClassFile</code>,
273 <code>MethodInfo</code>, or <code>FieldInfo</code> objects.
274 Call <code>getAttribute(AnnotationsAttribute.invisibleTag)</code>
276 of <code>javassist.bytecode.AnnotationsAttribute</code> class
277 and the <code>javassist.bytecode.annotation</code> package.
281 If you want to access annotations through <code>CtClass</code>,
282 call <code>getAnnotations()</code> in <code>CtClass</code> or
283 <code>CtBehavior</code>.
291 API such as <code>CtClass</code> does not directly support
297 example, suppose that your source code declares a parameterized
298 type <code>Vector&lt;String&gt;</code>:
306 <p>The compiled bytecode is equivalent to the following code:
318 caller site if the source code is compiled by Javassist, for example,
319 through <code>CtMethod.make()</code>. No type cast
320 is necessary if the source code is compiled by a normal Java compiler
321 such as <code>javac</code>.
332 <p>and want to add an interface <code>Getter&lt;T&gt;</code> to the
333 class <code>Wrapper&lt;T&gt;</code>:
341 <p>then the interface you really have to add is <code>Getter</code>
342 (the type parameters <code>&lt;T&gt;</code> drops off)
343 and the method you also have to add to the <code>Wrapper</code>
351 Since <code>get</code> returns an <code>Object</code>, an explicit type cast
352 is needed at the caller site if the source code is compiled by Javassist.
353 For example, if the type parameter <code>T</code>
354 is <code>String</code>, then <code>(String)</code> must be inserted as follows:
361 <p>The type cast is not needed if the source code is compiled by a normal Java
367 <code>setGenericSignature</code> method in the <code>CtClass</code>.
381 <p>The following code using Javassist will make the method shown above:
390 <p>The parameter type <code>int...</code> is changed into <code>int[]</code>
391 and <code>Modifier.VARARGS</code> is added to the method modifiers.
393 <p>To call this method in the source code compiled by the compiler embedded in Javassist,
414 <code>javassist.bytecode.MethodInfo.doPreverify</code> is true.
418 For a given method represented by a <code>CtMethod</code> object <code>m</code>,
425 <p>Here, <code>cpool</code> is a <code>ClassPool</code> object, which is
426 available by calling <code>getClassPool()</code> on a <code>CtClass</code>
427 object. A <code>ClassPool</code> object is responsible for finding
428 class files from given class pathes. To obtain all the <code>CtMethod</code>
429 objects, call the <code>getDeclaredMethods</code> method on a <code>CtClass</code> object.
444 convert a value type from <code>int</code> to <code>Integer</code>:
454 <p>Set <code>CtClass.debugDump</code> to a directory name.
456 directory. To stop this, set <code>CtClass.debugDump</code> to null.
465 <p>All modified class files are saved in <code>./dump</code>.