1/* 2 * Javassist, a Java-bytecode translator toolkit. 3 * Copyright (C) 1999-2007 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 16package javassist.convert; 17 18import javassist.bytecode.*; 19import javassist.CtClass; 20import javassist.CtField; 21import javassist.Modifier; 22 23final public class TransformFieldAccess extends Transformer { 24 private String newClassname, newFieldname; 25 private String fieldname; 26 private CtClass fieldClass; 27 private boolean isPrivate; 28 29 /* cache */ 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 /** 51 * Modify GETFIELD, GETSTATIC, PUTFIELD, and PUTSTATIC so that 52 * a different field is accessed. The new field must be declared 53 * in a superclass of the class in which the original field is 54 * declared. 55 */ 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