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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.ClassPool;
  18. import javassist.CtClass;
  19. import javassist.CtMethod;
  20. import javassist.Modifier;
  21. import javassist.NotFoundException;
  22. import javassist.bytecode.BadBytecode;
  23. import javassist.bytecode.CodeAttribute;
  24. import javassist.bytecode.CodeIterator;
  25. import javassist.bytecode.ConstPool;
  26. public class TransformCall extends Transformer {
  27. protected String classname, methodname, methodDescriptor;
  28. protected String newClassname, newMethodname;
  29. protected boolean newMethodIsPrivate;
  30. /* cache */
  31. protected int newIndex;
  32. protected ConstPool constPool;
  33. public TransformCall(Transformer next, CtMethod origMethod,
  34. CtMethod substMethod)
  35. {
  36. this(next, origMethod.getName(), substMethod);
  37. classname = origMethod.getDeclaringClass().getName();
  38. }
  39. public TransformCall(Transformer next, String oldMethodName,
  40. CtMethod substMethod)
  41. {
  42. super(next);
  43. methodname = oldMethodName;
  44. methodDescriptor = substMethod.getMethodInfo2().getDescriptor();
  45. classname = newClassname = substMethod.getDeclaringClass().getName();
  46. newMethodname = substMethod.getName();
  47. constPool = null;
  48. newMethodIsPrivate = Modifier.isPrivate(substMethod.getModifiers());
  49. }
  50. @Override
  51. public void initialize(ConstPool cp, CodeAttribute attr) {
  52. if (constPool != cp)
  53. newIndex = 0;
  54. }
  55. /**
  56. * Modify INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC and INVOKEVIRTUAL
  57. * so that a different method is invoked. The class name in the operand
  58. * of these instructions might be a subclass of the target class specified
  59. * by <code>classname</code>. This method transforms the instruction
  60. * in that case unless the subclass overrides the target method.
  61. */
  62. @Override
  63. public int transform(CtClass clazz, int pos, CodeIterator iterator,
  64. ConstPool cp) throws BadBytecode
  65. {
  66. int c = iterator.byteAt(pos);
  67. if (c == INVOKEINTERFACE || c == INVOKESPECIAL
  68. || c == INVOKESTATIC || c == INVOKEVIRTUAL) {
  69. int index = iterator.u16bitAt(pos + 1);
  70. String cname = cp.eqMember(methodname, methodDescriptor, index);
  71. if (cname != null && matchClass(cname, clazz.getClassPool())) {
  72. int ntinfo = cp.getMemberNameAndType(index);
  73. pos = match(c, pos, iterator,
  74. cp.getNameAndTypeDescriptor(ntinfo), cp);
  75. }
  76. }
  77. return pos;
  78. }
  79. private boolean matchClass(String name, ClassPool pool) {
  80. if (classname.equals(name))
  81. return true;
  82. try {
  83. CtClass clazz = pool.get(name);
  84. CtClass declClazz = pool.get(classname);
  85. if (clazz.subtypeOf(declClazz))
  86. try {
  87. CtMethod m = clazz.getMethod(methodname, methodDescriptor);
  88. return m.getDeclaringClass().getName().equals(classname);
  89. }
  90. catch (NotFoundException e) {
  91. // maybe the original method has been removed.
  92. return true;
  93. }
  94. }
  95. catch (NotFoundException e) {
  96. return false;
  97. }
  98. return false;
  99. }
  100. protected int match(int c, int pos, CodeIterator iterator,
  101. int typedesc, ConstPool cp) throws BadBytecode
  102. {
  103. if (newIndex == 0) {
  104. int nt = cp.addNameAndTypeInfo(cp.addUtf8Info(newMethodname),
  105. typedesc);
  106. int ci = cp.addClassInfo(newClassname);
  107. if (c == INVOKEINTERFACE)
  108. newIndex = cp.addInterfaceMethodrefInfo(ci, nt);
  109. else {
  110. if (newMethodIsPrivate && c == INVOKEVIRTUAL)
  111. iterator.writeByte(INVOKESPECIAL, pos);
  112. newIndex = cp.addMethodrefInfo(ci, nt);
  113. }
  114. constPool = cp;
  115. }
  116. iterator.write16bit(newIndex, pos + 1);
  117. return pos;
  118. }
  119. }