1 15 16 package javassist.convert; 17 18 import javassist.CtClass; 19 import javassist.CtMethod; 20 import javassist.NotFoundException; 21 import javassist.bytecode.*; 22 23 public class TransformBefore extends TransformCall { 24 protected CtClass[] parameterTypes; 25 protected int locals; 26 protected int maxLocals; 27 protected byte[] saveCode, loadCode; 28 29 public TransformBefore(Transformer next, 30 CtMethod origMethod, CtMethod beforeMethod) 31 throws NotFoundException 32 { 33 super(next, origMethod, beforeMethod); 34 parameterTypes = origMethod.getParameterTypes(); 35 locals = 0; 36 maxLocals = 0; 37 saveCode = loadCode = null; 38 } 39 40 public void initialize(ConstPool cp, CodeAttribute attr) { 41 super.initialize(cp, attr); 42 locals = 0; 43 maxLocals = attr.getMaxLocals(); 44 saveCode = loadCode = null; 45 } 46 47 protected int match(int c, int pos, CodeIterator iterator, 48 int typedesc, ConstPool cp) throws BadBytecode 49 { 50 if (newIndex == 0) { 51 String desc = Descriptor.ofParameters(parameterTypes) + 'V'; 52 desc = Descriptor.insertParameter(classname, desc); 53 int nt = cp.addNameAndTypeInfo(newMethodname, desc); 54 int ci = cp.addClassInfo(newClassname); 55 newIndex = cp.addMethodrefInfo(ci, nt); 56 constPool = cp; 57 } 58 59 if (saveCode == null) 60 makeCode(parameterTypes, cp); 61 62 return match2(pos, iterator); 63 } 64 65 protected int match2(int pos, CodeIterator iterator) throws BadBytecode { 66 iterator.move(pos); 67 iterator.insert(saveCode); 68 iterator.insert(loadCode); 69 int p = iterator.insertGap(3); 70 iterator.writeByte(INVOKESTATIC, p); 71 iterator.write16bit(newIndex, p + 1); 72 iterator.insert(loadCode); 73 return iterator.next(); 74 } 75 76 public int extraLocals() { return locals; } 77 78 protected void makeCode(CtClass[] paramTypes, ConstPool cp) { 79 Bytecode save = new Bytecode(cp, 0, 0); 80 Bytecode load = new Bytecode(cp, 0, 0); 81 82 int var = maxLocals; 83 int len = (paramTypes == null) ? 0 : paramTypes.length; 84 load.addAload(var); 85 makeCode2(save, load, 0, len, paramTypes, var + 1); 86 save.addAstore(var); 87 88 saveCode = save.get(); 89 loadCode = load.get(); 90 } 91 92 private void makeCode2(Bytecode save, Bytecode load, 93 int i, int n, CtClass[] paramTypes, int var) 94 { 95 if (i < n) { 96 int size = load.addLoad(var, paramTypes[i]); 97 makeCode2(save, load, i + 1, n, paramTypes, var + size); 98 save.addStore(var, paramTypes[i]); 99 } 100 else 101 locals = var - maxLocals; 102 } 103 } 104 | Popular Tags |