diff options
Diffstat (limited to 'src/main/javassist/bytecode')
-rw-r--r-- | src/main/javassist/bytecode/Bytecode.java | 24 | ||||
-rw-r--r-- | src/main/javassist/bytecode/Descriptor.java | 28 |
2 files changed, 45 insertions, 7 deletions
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; } |