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.

TransformNewClass.java 2.7KB

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