From: chiba Date: Tue, 22 Nov 2005 16:51:12 +0000 (+0000) Subject: added some methods to javassist.Bytecode and Descriptor. X-Git-Tag: rel_3_17_1_ga~401 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ecb71c75155bdd2990141314db777348fdf313ff;p=javassist.git added some methods to javassist.Bytecode and Descriptor. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@225 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- 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 { *

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). + * "(I)D", 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 + * "(IJ)D", 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). +

Class search path

The default ClassPool returned @@ -328,6 +329,11 @@ cc.detach();

You must not call any method on that CtClass object after detach() is called. +However, you can call get() on ClassPool +to make a new instance of CtClass representing +the same class. If you call get(), the ClassPool +reads a class file again and newly creates a CtClass +object, which is returned by get().

Another idea is to occasionally replace a ClassPool with