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.5KB

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