/Users/lyon/j4p/src/javassist/convert/TransformWriteField.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.CtClass; 
19   import javassist.CtField; 
20   import javassist.bytecode.*; 
21   import javassist.CannotCompileException; 
22    
23   final public class TransformWriteField extends TransformReadField { 
24       public TransformWriteField(Transformer next, CtField field, 
25                                  String methodClassname, String methodName) { 
26           super(next, field, methodClassname, methodName); 
27       } 
28    
29       public int transform(CtClass tclazz, int pos, CodeIterator iterator, 
30                            ConstPool cp) throws BadBytecode { 
31           int c = iterator.byteAt(pos); 
32           if (c == PUTFIELD || c == PUTSTATIC) { 
33               int index = iterator.u16bitAt(pos + 1); 
34               String typedesc = isField(tclazz.getClassPool(), cp, 
35                       fieldClass, fieldname, isPrivate, index); 
36               if (typedesc != null) { 
37                   if (c == PUTSTATIC) { 
38                       CodeAttribute ca = iterator.get(); 
39                       iterator.move(pos); 
40                       char c0 = typedesc.charAt(0); 
41                       if (c0 == 'J' || c0 == 'D') {       // long or double 
42                           // insertGap() may insert 4 bytes. 
43                           iterator.insertGap(3); 
44                           iterator.writeByte(ACONST_NULL, pos); 
45                           iterator.writeByte(DUP_X2, pos + 1); 
46                           iterator.writeByte(POP, pos + 2); 
47                           ca.setMaxStack(ca.getMaxStack() + 2); 
48                       } else { 
49                           // insertGap() may insert 4 bytes. 
50                           iterator.insertGap(2); 
51                           iterator.writeByte(ACONST_NULL, pos); 
52                           iterator.writeByte(SWAP, pos + 1); 
53                           ca.setMaxStack(ca.getMaxStack() + 1); 
54                       } 
55    
56                       pos = iterator.next(); 
57                   } 
58    
59                   int mi = cp.addClassInfo(methodClassname); 
60                   String type = "(Ljava/lang/Object;" + typedesc + ")V"; 
61                   int methodref = cp.addMethodrefInfo(mi, methodName, type); 
62                   iterator.writeByte(INVOKESTATIC, pos); 
63                   iterator.write16bit(methodref, pos + 1); 
64               } 
65           } 
66    
67           return pos; 
68       } 
69   } 
70