diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-11-22 16:51:12 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-11-22 16:51:12 +0000 |
commit | ecb71c75155bdd2990141314db777348fdf313ff (patch) | |
tree | d45c70c481bf274e87dd9ecbbc41670e51e080ef | |
parent | a1419a5df6a881f26d3eedacb37d9ea616764cf0 (diff) | |
download | javassist-ecb71c75155bdd2990141314db777348fdf313ff.tar.gz javassist-ecb71c75155bdd2990141314db777348fdf313ff.zip |
added some methods to javassist.Bytecode and Descriptor.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@225 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
-rw-r--r-- | src/main/javassist/CtClassType.java | 5 | ||||
-rw-r--r-- | src/main/javassist/bytecode/Bytecode.java | 24 | ||||
-rw-r--r-- | src/main/javassist/bytecode/Descriptor.java | 28 | ||||
-rw-r--r-- | tutorial/tutorial.html | 6 |
4 files changed, 54 insertions, 9 deletions
diff --git a/src/main/javassist/CtClassType.java b/src/main/javassist/CtClassType.java index 46fb268a..92f73d3e 100644 --- a/src/main/javassist/CtClassType.java +++ b/src/main/javassist/CtClassType.java @@ -193,8 +193,9 @@ class CtClassType extends CtClass { fin = new BufferedInputStream(fin); classfile = new ClassFile(new DataInputStream(fin)); if (!classfile.getName().equals(qualifiedName)) - throw new RuntimeException(classfile.getName() + " in " - + qualifiedName.replace('.', '/') + ".java"); + throw new RuntimeException("cannot find " + qualifiedName + ": " + + classfile.getName() + " found in " + + qualifiedName.replace('.', '/') + ".class"); return classfile; } diff --git a/src/main/javassist/bytecode/Bytecode.java b/src/main/javassist/bytecode/Bytecode.java index 32f9d2b2..9fe0bb84 100644 --- a/src/main/javassist/bytecode/Bytecode.java +++ b/src/main/javassist/bytecode/Bytecode.java @@ -1297,9 +1297,29 @@ public class Bytecode extends ByteVector implements Cloneable, Opcode { * @param desc the descriptor of the field type. */ public void addPutstatic(CtClass c, String name, String desc) { + addPutstatic0(c, null, name, desc); + } + + /** + * Appends PUTSTATIC. + * + * @param classname the fully-qualified name of the target class. + * @param filedName the field name. + * @param desc the descriptor of the field type. + */ + public void addPutstatic(String classname, String fieldName, String desc) { + // if classname is null, the target class is THIS. + addPutstatic0(null, classname, fieldName, desc); + } + + private void addPutstatic0(CtClass target, String classname, + String fieldName, String desc) { add(PUTSTATIC); - int ci = constPool.addClassInfo(c); - addIndex(constPool.addFieldrefInfo(ci, name, desc)); + + // target is null if it represents THIS. + int ci = classname == null ? constPool.addClassInfo(target) + : constPool.addClassInfo(classname); + addIndex(constPool.addFieldrefInfo(ci, fieldName, desc)); growStack(-Descriptor.dataSize(desc)); } diff --git a/src/main/javassist/bytecode/Descriptor.java b/src/main/javassist/bytecode/Descriptor.java index b67e7a49..ef194b7f 100644 --- a/src/main/javassist/bytecode/Descriptor.java +++ b/src/main/javassist/bytecode/Descriptor.java @@ -621,11 +621,28 @@ public class Descriptor { * <p>If the descriptor represents a method type, this method returns * (the size of the returned value) - (the sum of the data sizes * of all the parameters). For example, if the descriptor is - * "(I)D", then this method returns 1 (= 2 - 1). + * <code>"(I)D"</code>, then this method returns 1 (= 2 - 1). * * @param desc descriptor */ public static int dataSize(String desc) { + return dataSize(desc, true); + } + + /** + * Computes the data size of parameters. + * If one of the parameters is double type, the size of that parameter + * is 2 words. For example, if the given descriptor is + * <code>"(IJ)D"</code>, then this method returns 3. The size of the + * return type is not computed. + * + * @param desc a method descriptor. + */ + public static int paramSize(String desc) { + return -dataSize(desc, false); + } + + private static int dataSize(String desc, boolean withRet) { int n = 0; char c = desc.charAt(0); if (c == '(') { @@ -658,10 +675,11 @@ public class Descriptor { } } - if (c == 'J' || c == 'D') - n += 2; - else if (c != 'V') - ++n; + if (withRet) + if (c == 'J' || c == 'D') + n += 2; + else if (c != 'V') + ++n; return n; } diff --git a/tutorial/tutorial.html b/tutorial/tutorial.html index f3766d43..b1541770 100644 --- a/tutorial/tutorial.html +++ b/tutorial/tutorial.html @@ -184,6 +184,7 @@ for that purpose. It stops pruning, write a class file, defrost it, and turn pruning on again (if it was initially on). </ul> + <h4>Class search path</h4> <p>The default <code>ClassPool</code> returned @@ -328,6 +329,11 @@ cc.detach(); <p>You must not call any method on that <code>CtClass</code> object after <code>detach()</code> is called. +However, you can call <code>get()</code> on <code>ClassPool</code> +to make a new instance of <code>CtClass</code> representing +the same class. If you call <code>get()</code>, the <code>ClassPool</code> +reads a class file again and newly creates a <code>CtClass</code> +object, which is returned by <code>get()</code>. <p> Another idea is to occasionally replace a <code>ClassPool</code> with |