/Users/lyon/j4p/src/javassist/convert/TransformFieldAccess.java

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    
16   package javassist.convert; 
17    
18   import javassist.bytecode.*; 
19   import javassist.CtClass; 
20   import javassist.CtField; 
21   import javassist.Modifier; 
22   import javassist.CannotCompileException; 
23    
24   final public class TransformFieldAccess extends Transformer { 
25       private String newClassname, newFieldname; 
26       private String fieldname; 
27       private CtClass fieldClass; 
28       private boolean isPrivate; 
29    
30       /* cache */ 
31       private int newIndex; 
32       private ConstPool constPool; 
33    
34       public TransformFieldAccess(Transformer next, CtField field, 
35                                   String newClassname, String newFieldname) { 
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           int c = iterator.byteAt(pos); 
59           if (c == GETFIELD || c == GETSTATIC 
60                   || c == PUTFIELD || c == PUTSTATIC) { 
61               int index = iterator.u16bitAt(pos + 1); 
62               String typedesc 
63                       = TransformReadField.isField(clazz.getClassPool(), cp, 
64                               fieldClass, fieldname, isPrivate, index); 
65               if (typedesc != null) { 
66                   if (newIndex == 0) { 
67                       int nt = cp.addNameAndTypeInfo(newFieldname, 
68                               typedesc); 
69                       newIndex = cp.addFieldrefInfo( 
70                               cp.addClassInfo(newClassname), nt); 
71                       constPool = cp; 
72                   } 
73    
74                   iterator.write16bit(newIndex, pos + 1); 
75               } 
76           } 
77    
78           return pos; 
79       } 
80   } 
81