Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TransformNewClass.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999-2007 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.bytecode.*;
  17. import javassist.CtClass;
  18. import javassist.CannotCompileException;
  19. final public class TransformNewClass extends Transformer {
  20. private int nested;
  21. private String classname, newClassName;
  22. private int newClassIndex, newMethodNTIndex, newMethodIndex;
  23. public TransformNewClass(Transformer next,
  24. String classname, String newClassName) {
  25. super(next);
  26. this.classname = classname;
  27. this.newClassName = newClassName;
  28. }
  29. public void initialize(ConstPool cp, CodeAttribute attr) {
  30. nested = 0;
  31. newClassIndex = newMethodNTIndex = newMethodIndex = 0;
  32. }
  33. /**
  34. * Modifies a sequence of
  35. * NEW classname
  36. * DUP
  37. * ...
  38. * INVOKESPECIAL classname:method
  39. */
  40. public int transform(CtClass clazz, int pos, CodeIterator iterator,
  41. ConstPool cp) throws CannotCompileException
  42. {
  43. int index;
  44. int c = iterator.byteAt(pos);
  45. if (c == NEW) {
  46. index = iterator.u16bitAt(pos + 1);
  47. if (cp.getClassInfo(index).equals(classname)) {
  48. if (iterator.byteAt(pos + 3) != DUP)
  49. throw new CannotCompileException(
  50. "NEW followed by no DUP was found");
  51. if (newClassIndex == 0)
  52. newClassIndex = cp.addClassInfo(newClassName);
  53. iterator.write16bit(newClassIndex, pos + 1);
  54. ++nested;
  55. }
  56. }
  57. else if (c == INVOKESPECIAL) {
  58. index = iterator.u16bitAt(pos + 1);
  59. int typedesc = cp.isConstructor(classname, index);
  60. if (typedesc != 0 && nested > 0) {
  61. int nt = cp.getMethodrefNameAndType(index);
  62. if (newMethodNTIndex != nt) {
  63. newMethodNTIndex = nt;
  64. newMethodIndex = cp.addMethodrefInfo(newClassIndex, nt);
  65. }
  66. iterator.write16bit(newMethodIndex, pos + 1);
  67. --nested;
  68. }
  69. }
  70. return pos;
  71. }
  72. }