diff options
author | chibash <chiba@javassist.org> | 2015-02-04 12:26:50 +0900 |
---|---|---|
committer | chibash <chiba@javassist.org> | 2015-02-04 12:26:50 +0900 |
commit | e724bc8af9362198b2e7ba05843d7e9c76a28eff (patch) | |
tree | aed9732048dd7b6f4a01a398d44511279de5aba4 /tutorial | |
parent | c43f4a7cde41d04095feb027f33f3ab4da7248bd (diff) | |
download | javassist-e724bc8af9362198b2e7ba05843d7e9c76a28eff.tar.gz javassist-e724bc8af9362198b2e7ba05843d7e9c76a28eff.zip |
updated the tutorial and some javadoc comments
Diffstat (limited to 'tutorial')
-rw-r--r-- | tutorial/tutorial.html | 2 | ||||
-rw-r--r-- | tutorial/tutorial2.html | 2 | ||||
-rw-r--r-- | tutorial/tutorial3.html | 45 |
3 files changed, 46 insertions, 3 deletions
diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index 3a125f08..b8110390 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -1100,6 +1100,6 @@ For more information, see the API documentation of <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. </body> </html> diff --git a/tutorial/tutorial2.html b/tutorial/tutorial2.html index f2571c8b..04afbd9c 100644 --- a/tutorial/tutorial2.html +++ b/tutorial/tutorial2.html @@ -1628,6 +1628,6 @@ write: <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. </body> </html> diff --git a/tutorial/tutorial3.html b/tutorial/tutorial3.html index e6964553..cb2a6b3e 100644 --- a/tutorial/tutorial3.html +++ b/tutorial/tutorial3.html @@ -78,6 +78,28 @@ 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"> @@ -219,6 +241,27 @@ 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"> @@ -427,6 +470,6 @@ CtClass.debugDump = "./dump"; <hr> Java(TM) is a trademark of Sun Microsystems, Inc.<br> -Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. </body> </html> |