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.

TransformCall.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.bytecode.*;
  19. import javassist.CannotCompileException;
  20. public class TransformCall extends Transformer {
  21. protected String classname, methodname, methodDescriptor;
  22. protected String newClassname, newMethodname;
  23. /* cache */
  24. protected int newIndex;
  25. protected ConstPool constPool;
  26. public TransformCall(Transformer next, CtMethod origMethod,
  27. CtMethod substMethod)
  28. {
  29. super(next);
  30. this.classname = origMethod.getDeclaringClass().getName();
  31. this.methodname = origMethod.getName();
  32. this.methodDescriptor = origMethod.getMethodInfo2().getDescriptor();
  33. this.newClassname = substMethod.getDeclaringClass().getName();
  34. this.newMethodname = substMethod.getName();
  35. this.constPool = null;
  36. }
  37. public void initialize(ConstPool cp, CodeAttribute attr) {
  38. if (constPool != cp)
  39. newIndex = 0;
  40. }
  41. /**
  42. * Modify INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC and INVOKEVIRTUAL
  43. * so that a different method is invoked.
  44. */
  45. public int transform(CtClass clazz, int pos, CodeIterator iterator,
  46. ConstPool cp) throws BadBytecode
  47. {
  48. int c = iterator.byteAt(pos);
  49. if (c == INVOKEINTERFACE || c == INVOKESPECIAL
  50. || c == INVOKESTATIC || c == INVOKEVIRTUAL) {
  51. int index = iterator.u16bitAt(pos + 1);
  52. int typedesc = cp.isMember(classname, methodname, index);
  53. if (typedesc != 0)
  54. if (cp.getUtf8Info(typedesc).equals(methodDescriptor))
  55. pos = match(c, pos, iterator, typedesc, cp);
  56. }
  57. return pos;
  58. }
  59. protected int match(int c, int pos, CodeIterator iterator,
  60. int typedesc, ConstPool cp) throws BadBytecode
  61. {
  62. if (newIndex == 0) {
  63. int nt = cp.addNameAndTypeInfo(cp.addUtf8Info(newMethodname),
  64. typedesc);
  65. int ci = cp.addClassInfo(newClassname);
  66. if (c == INVOKEINTERFACE)
  67. newIndex = cp.addInterfaceMethodrefInfo(ci, nt);
  68. else
  69. newIndex = cp.addMethodrefInfo(ci, nt);
  70. constPool = cp;
  71. }
  72. iterator.write16bit(newIndex, pos + 1);
  73. return pos;
  74. }
  75. }