KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > convert > TransformBefore


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 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.CtMethod;
20 import javassist.NotFoundException;
21 import javassist.bytecode.*;
22
23 public class TransformBefore extends TransformCall {
24     protected CtClass[] parameterTypes;
25     protected int locals;
26     protected int maxLocals;
27     protected byte[] saveCode, loadCode;
28
29     public TransformBefore(Transformer next,
30                            CtMethod origMethod, CtMethod beforeMethod)
31         throws NotFoundException
32     {
33         super(next, origMethod, beforeMethod);
34         parameterTypes = origMethod.getParameterTypes();
35         locals = 0;
36         maxLocals = 0;
37         saveCode = loadCode = null;
38     }
39
40     public void initialize(ConstPool cp, CodeAttribute attr) {
41         super.initialize(cp, attr);
42         locals = 0;
43         maxLocals = attr.getMaxLocals();
44         saveCode = loadCode = null;
45     }
46
47     protected int match(int c, int pos, CodeIterator iterator,
48                         int typedesc, ConstPool cp) throws BadBytecode
49     {
50         if (newIndex == 0) {
51             String JavaDoc desc = Descriptor.ofParameters(parameterTypes) + 'V';
52             desc = Descriptor.insertParameter(classname, desc);
53             int nt = cp.addNameAndTypeInfo(newMethodname, desc);
54             int ci = cp.addClassInfo(newClassname);
55             newIndex = cp.addMethodrefInfo(ci, nt);
56             constPool = cp;
57         }
58
59         if (saveCode == null)
60             makeCode(parameterTypes, cp);
61
62         return match2(pos, iterator);
63     }
64
65     protected int match2(int pos, CodeIterator iterator) throws BadBytecode {
66         iterator.move(pos);
67         iterator.insert(saveCode);
68         iterator.insert(loadCode);
69         int p = iterator.insertGap(3);
70         iterator.writeByte(INVOKESTATIC, p);
71         iterator.write16bit(newIndex, p + 1);
72         iterator.insert(loadCode);
73         return iterator.next();
74     }
75
76     public int extraLocals() { return locals; }
77
78     protected void makeCode(CtClass[] paramTypes, ConstPool cp) {
79         Bytecode save = new Bytecode(cp, 0, 0);
80         Bytecode load = new Bytecode(cp, 0, 0);
81
82         int var = maxLocals;
83         int len = (paramTypes == null) ? 0 : paramTypes.length;
84         load.addAload(var);
85         makeCode2(save, load, 0, len, paramTypes, var + 1);
86         save.addAstore(var);
87
88         saveCode = save.get();
89         loadCode = load.get();
90     }
91
92     private void makeCode2(Bytecode save, Bytecode load,
93                            int i, int n, CtClass[] paramTypes, int var)
94     {
95         if (i < n) {
96             int size = load.addLoad(var, paramTypes[i]);
97             makeCode2(save, load, i + 1, n, paramTypes, var + size);
98             save.addStore(var, paramTypes[i]);
99         }
100         else
101             locals = var - maxLocals;
102     }
103 }
104
Popular Tags