From e724bc8af9362198b2e7ba05843d7e9c76a28eff Mon Sep 17 00:00:00 2001 From: chibash Date: Wed, 4 Feb 2015 12:26:50 +0900 Subject: [PATCH] updated the tutorial and some javadoc comments --- src/main/javassist/CtClass.java | 5 ++++ tutorial/tutorial.html | 2 +- tutorial/tutorial2.html | 2 +- tutorial/tutorial3.html | 45 ++++++++++++++++++++++++++++++++- 4 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/main/javassist/CtClass.java b/src/main/javassist/CtClass.java index fc828847..0fca3ff7 100644 --- a/src/main/javassist/CtClass.java +++ b/src/main/javassist/CtClass.java @@ -463,6 +463,11 @@ public abstract class CtClass { * and the methods so that the type variable T can be * accessible through reflection. * + *

MethodSignature is a utility class. You can directly + * pass the signature string to the setGenericSignature method. + * For the specification of the signatures, see Section 4.7.9.1 Signatures + * of The Java Virtual Machine Specification (Java SE 8). + * * @param sig a generic signature. * @see javassist.bytecode.SignatureAttribute.ClassSignature#encode() * @see javassist.bytecode.SignatureAttribute.MethodSignature#encode() 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


Java(TM) is a trademark of Sun Microsystems, Inc.
-Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. 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:
Java(TM) is a trademark of Sun Microsystems, Inc.
-Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. 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. write() in ClassFile writes the contents of the class file to a given DataOutputStream. +

You can create a new class file from scratch. For example, +

+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")));
+
+ +

this code generates a class file Foo.class that contains +the implementation of the following class: + +

+package test;
+class Foo implements Cloneable {
+    public int width;
+}
+
+


@@ -219,6 +241,27 @@ constructed from the Bytecode object. To recompute the maximum stack depth of a method body, call computeMaxStack() in CodeAttribute. +

Bytecode can be used to construct a method. +For example, + +

+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);
+
+ +

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


@@ -427,6 +470,6 @@ CtClass.debugDump = "./dump";


Java(TM) is a trademark of Sun Microsystems, Inc.
-Copyright (C) 2000-2014 by Shigeru Chiba, All rights reserved. +Copyright (C) 2000-2015 by Shigeru Chiba, All rights reserved. -- 2.39.5