1 15 16 package javassist.convert; 17 18 import javassist.bytecode.*; 19 import javassist.CtClass; 20 import javassist.CtField; 21 import javassist.Modifier; 22 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 29 30 private int newIndex; 31 private ConstPool constPool; 32 33 public TransformFieldAccess(Transformer next, CtField field, 34 String newClassname, String newFieldname) 35 { 36 super(next); 37 this.fieldClass = field.getDeclaringClass(); 38 this.fieldname = field.getName(); 39 this.isPrivate = Modifier.isPrivate(field.getModifiers()); 40 this.newClassname = newClassname; 41 this.newFieldname = newFieldname; 42 this.constPool = null; 43 } 44 45 public void initialize(ConstPool cp, CodeAttribute attr) { 46 if (constPool != cp) 47 newIndex = 0; 48 } 49 50 56 public int transform(CtClass clazz, int pos, 57 CodeIterator iterator, ConstPool cp) 58 { 59 int c = iterator.byteAt(pos); 60 if (c == GETFIELD || c == GETSTATIC 61 || c == PUTFIELD || c == PUTSTATIC) { 62 int index = iterator.u16bitAt(pos + 1); 63 String typedesc 64 = TransformReadField.isField(clazz.getClassPool(), cp, 65 fieldClass, fieldname, isPrivate, index); 66 if (typedesc != null) { 67 if (newIndex == 0) { 68 int nt = cp.addNameAndTypeInfo(newFieldname, 69 typedesc); 70 newIndex = cp.addFieldrefInfo( 71 cp.addClassInfo(newClassname), nt); 72 constPool = cp; 73 } 74 75 iterator.write16bit(newIndex, pos + 1); 76 } 77 } 78 79 return pos; 80 } 81 } 82 | Popular Tags |