You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TransformBefore.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2003 Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later.
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. */
  15. package javassist.convert;
  16. import javassist.CtClass;
  17. import javassist.CtMethod;
  18. import javassist.NotFoundException;
  19. import javassist.bytecode.*;
  20. import javassist.CannotCompileException;
  21. public class TransformBefore extends TransformCall {
  22. protected CtClass[] parameterTypes;
  23. protected int locals;
  24. protected int maxLocals;
  25. protected byte[] saveCode, loadCode;
  26. public TransformBefore(Transformer next,
  27. CtMethod origMethod, CtMethod beforeMethod)
  28. throws NotFoundException
  29. {
  30. super(next, origMethod, beforeMethod);
  31. parameterTypes = origMethod.getParameterTypes();
  32. locals = 0;
  33. maxLocals = 0;
  34. saveCode = loadCode = null;
  35. }
  36. public void initialize(ConstPool cp, CodeAttribute attr) {
  37. super.initialize(cp, attr);
  38. locals = 0;
  39. maxLocals = attr.getMaxLocals();
  40. saveCode = loadCode = null;
  41. }
  42. protected int match(int c, int pos, CodeIterator iterator,
  43. int typedesc, ConstPool cp) throws BadBytecode
  44. {
  45. if (newIndex == 0) {
  46. String desc = Descriptor.ofParameters(parameterTypes) + 'V';
  47. desc = Descriptor.insertParameter(classname, desc);
  48. int nt = cp.addNameAndTypeInfo(newMethodname, desc);
  49. int ci = cp.addClassInfo(newClassname);
  50. newIndex = cp.addMethodrefInfo(ci, nt);
  51. constPool = cp;
  52. }
  53. if (saveCode == null)
  54. makeCode(parameterTypes, cp);
  55. return match2(pos, iterator);
  56. }
  57. protected int match2(int pos, CodeIterator iterator) throws BadBytecode {
  58. iterator.move(pos);
  59. iterator.insert(saveCode);
  60. iterator.insert(loadCode);
  61. int p = iterator.insertGap(3);
  62. iterator.writeByte(INVOKESTATIC, p);
  63. iterator.write16bit(newIndex, p + 1);
  64. iterator.insert(loadCode);
  65. return iterator.next();
  66. }
  67. public int extraLocals() { return locals; }
  68. protected void makeCode(CtClass[] paramTypes, ConstPool cp) {
  69. Bytecode save = new Bytecode(cp, 0, 0);
  70. Bytecode load = new Bytecode(cp, 0, 0);
  71. int var = maxLocals;
  72. int len = (paramTypes == null) ? 0 : paramTypes.length;
  73. load.addAload(var);
  74. makeCode2(save, load, 0, len, paramTypes, var + 1);
  75. save.addAstore(var);
  76. saveCode = save.get();
  77. loadCode = load.get();
  78. }
  79. private void makeCode2(Bytecode save, Bytecode load,
  80. int i, int n, CtClass[] paramTypes, int var)
  81. {
  82. if (i < n) {
  83. int size = load.addLoad(var, paramTypes[i]);
  84. makeCode2(save, load, i + 1, n, paramTypes, var + size);
  85. save.addStore(var, paramTypes[i]);
  86. }
  87. else
  88. locals = var - maxLocals;
  89. }
  90. }