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.

TransformReadField.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.bytecode.*;
  17. import javassist.ClassPool;
  18. import javassist.CtClass;
  19. import javassist.CtField;
  20. import javassist.CannotCompileException;
  21. import javassist.NotFoundException;
  22. import javassist.Modifier;
  23. public class TransformReadField extends Transformer {
  24. protected String fieldname;
  25. protected CtClass fieldClass;
  26. protected boolean isPrivate;
  27. protected String methodClassname, methodName;
  28. public TransformReadField(Transformer next, CtField field,
  29. String methodClassname, String methodName)
  30. {
  31. super(next);
  32. this.fieldClass = field.getDeclaringClass();
  33. this.fieldname = field.getName();
  34. this.methodClassname = methodClassname;
  35. this.methodName = methodName;
  36. this.isPrivate = Modifier.isPrivate(field.getModifiers());
  37. }
  38. static String isField(ClassPool pool, ConstPool cp, CtClass fclass,
  39. String fname, boolean is_private, int index) {
  40. if (!cp.getFieldrefName(index).equals(fname))
  41. return null;
  42. try {
  43. CtClass c = pool.get(cp.getFieldrefClassName(index));
  44. if (is_private ? c == fclass : c.subclassOf(fclass))
  45. return cp.getFieldrefType(index);
  46. }
  47. catch (NotFoundException e) {}
  48. return null;
  49. }
  50. public int transform(CtClass tclazz, int pos, CodeIterator iterator,
  51. ConstPool cp) throws BadBytecode
  52. {
  53. int c = iterator.byteAt(pos);
  54. if (c == GETFIELD || c == GETSTATIC) {
  55. int index = iterator.u16bitAt(pos + 1);
  56. String typedesc = isField(tclazz.getClassPool(), cp,
  57. fieldClass, fieldname, isPrivate, index);
  58. if (typedesc != null) {
  59. if (c == GETSTATIC) {
  60. iterator.move(pos);
  61. iterator.insertGap(1); // insertGap() may insert 4 bytes.
  62. iterator.writeByte(ACONST_NULL, pos);
  63. pos = iterator.next();
  64. }
  65. String type = "(Ljava/lang/Object;)" + typedesc;
  66. int mi = cp.addClassInfo(methodClassname);
  67. int methodref = cp.addMethodrefInfo(mi, methodName, type);
  68. iterator.writeByte(INVOKESTATIC, pos);
  69. iterator.write16bit(methodref, pos + 1);
  70. return pos;
  71. }
  72. }
  73. return pos;
  74. }
  75. }