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