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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- 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. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.convert;
  17. import javassist.CtClass;
  18. import javassist.CtMethod;
  19. import javassist.NotFoundException;
  20. import javassist.bytecode.BadBytecode;
  21. import javassist.bytecode.Bytecode;
  22. import javassist.bytecode.CodeAttribute;
  23. import javassist.bytecode.CodeIterator;
  24. import javassist.bytecode.ConstPool;
  25. import javassist.bytecode.Descriptor;
  26. public class TransformBefore extends TransformCall {
  27. protected CtClass[] parameterTypes;
  28. protected int locals;
  29. protected int maxLocals;
  30. protected byte[] saveCode, loadCode;
  31. public TransformBefore(Transformer next,
  32. CtMethod origMethod, CtMethod beforeMethod)
  33. throws NotFoundException
  34. {
  35. super(next, origMethod, beforeMethod);
  36. // override
  37. methodDescriptor = origMethod.getMethodInfo2().getDescriptor();
  38. parameterTypes = origMethod.getParameterTypes();
  39. locals = 0;
  40. maxLocals = 0;
  41. saveCode = loadCode = null;
  42. }
  43. @Override
  44. public void initialize(ConstPool cp, CodeAttribute attr) {
  45. super.initialize(cp, attr);
  46. locals = 0;
  47. maxLocals = attr.getMaxLocals();
  48. saveCode = loadCode = null;
  49. }
  50. @Override
  51. protected int match(int c, int pos, CodeIterator iterator,
  52. int typedesc, ConstPool cp) throws BadBytecode
  53. {
  54. if (newIndex == 0) {
  55. String desc = Descriptor.ofParameters(parameterTypes) + 'V';
  56. desc = Descriptor.insertParameter(classname, desc);
  57. int nt = cp.addNameAndTypeInfo(newMethodname, desc);
  58. int ci = cp.addClassInfo(newClassname);
  59. newIndex = cp.addMethodrefInfo(ci, nt);
  60. constPool = cp;
  61. }
  62. if (saveCode == null)
  63. makeCode(parameterTypes, cp);
  64. return match2(pos, iterator);
  65. }
  66. protected int match2(int pos, CodeIterator iterator) throws BadBytecode {
  67. iterator.move(pos);
  68. iterator.insert(saveCode);
  69. iterator.insert(loadCode);
  70. int p = iterator.insertGap(3);
  71. iterator.writeByte(INVOKESTATIC, p);
  72. iterator.write16bit(newIndex, p + 1);
  73. iterator.insert(loadCode);
  74. return iterator.next();
  75. }
  76. @Override
  77. public int extraLocals() { return locals; }
  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. int var = maxLocals;
  82. int len = (paramTypes == null) ? 0 : paramTypes.length;
  83. load.addAload(var);
  84. makeCode2(save, load, 0, len, paramTypes, var + 1);
  85. save.addAstore(var);
  86. saveCode = save.get();
  87. loadCode = load.get();
  88. }
  89. private void makeCode2(Bytecode save, Bytecode load,
  90. int i, int n, CtClass[] paramTypes, int var)
  91. {
  92. if (i < n) {
  93. int size = load.addLoad(var, paramTypes[i]);
  94. makeCode2(save, load, i + 1, n, paramTypes, var + size);
  95. save.addStore(var, paramTypes[i]);
  96. }
  97. else
  98. locals = var - maxLocals;
  99. }
  100. }