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.

TransformFieldAccess.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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.CtField;
  19. import javassist.Modifier;
  20. import javassist.bytecode.CodeAttribute;
  21. import javassist.bytecode.CodeIterator;
  22. import javassist.bytecode.ConstPool;
  23. final public class TransformFieldAccess extends Transformer {
  24. private String newClassname, newFieldname;
  25. private String fieldname;
  26. private CtClass fieldClass;
  27. private boolean isPrivate;
  28. /* cache */
  29. private int newIndex;
  30. private ConstPool constPool;
  31. public TransformFieldAccess(Transformer next, CtField field,
  32. String newClassname, String newFieldname)
  33. {
  34. super(next);
  35. this.fieldClass = field.getDeclaringClass();
  36. this.fieldname = field.getName();
  37. this.isPrivate = Modifier.isPrivate(field.getModifiers());
  38. this.newClassname = newClassname;
  39. this.newFieldname = newFieldname;
  40. this.constPool = null;
  41. }
  42. @Override
  43. public void initialize(ConstPool cp, CodeAttribute attr) {
  44. if (constPool != cp)
  45. newIndex = 0;
  46. }
  47. /**
  48. * Modify GETFIELD, GETSTATIC, PUTFIELD, and PUTSTATIC so that
  49. * a different field is accessed. The new field must be declared
  50. * in a superclass of the class in which the original field is
  51. * declared.
  52. */
  53. @Override
  54. public int transform(CtClass clazz, int pos,
  55. CodeIterator iterator, ConstPool cp)
  56. {
  57. int c = iterator.byteAt(pos);
  58. if (c == GETFIELD || c == GETSTATIC
  59. || c == PUTFIELD || c == PUTSTATIC) {
  60. int index = iterator.u16bitAt(pos + 1);
  61. String typedesc
  62. = TransformReadField.isField(clazz.getClassPool(), cp,
  63. fieldClass, fieldname, isPrivate, index);
  64. if (typedesc != null) {
  65. if (newIndex == 0) {
  66. int nt = cp.addNameAndTypeInfo(newFieldname,
  67. typedesc);
  68. newIndex = cp.addFieldrefInfo(
  69. cp.addClassInfo(newClassname), nt);
  70. constPool = cp;
  71. }
  72. iterator.write16bit(newIndex, pos + 1);
  73. }
  74. }
  75. return pos;
  76. }
  77. }