01: /*
02: * Javassist, a Java-bytecode translator toolkit.
03: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
04: *
05: * The contents of this file are subject to the Mozilla Public License Version
06: * 1.1 (the "License"); you may not use this file except in compliance with
07: * the License. Alternatively, the contents of this file may be used under
08: * the terms of the GNU Lesser General Public License Version 2.1 or later.
09: *
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:
22: final public class TransformWriteField extends TransformReadField {
23: public TransformWriteField(Transformer next, CtField field,
24: String methodClassname, String methodName) {
25: super (next, field, methodClassname, methodName);
26: }
27:
28: public int transform(CtClass tclazz, int pos,
29: CodeIterator iterator, ConstPool cp) throws BadBytecode {
30: int c = iterator.byteAt(pos);
31: if (c == PUTFIELD || c == PUTSTATIC) {
32: int index = iterator.u16bitAt(pos + 1);
33: String typedesc = isField(tclazz.getClassPool(), cp,
34: fieldClass, fieldname, isPrivate, index);
35: if (typedesc != null) {
36: if (c == PUTSTATIC) {
37: CodeAttribute ca = iterator.get();
38: iterator.move(pos);
39: char c0 = typedesc.charAt(0);
40: if (c0 == 'J' || c0 == 'D') { // long or double
41: // insertGap() may insert 4 bytes.
42: iterator.insertGap(3);
43: iterator.writeByte(ACONST_NULL, pos);
44: iterator.writeByte(DUP_X2, pos + 1);
45: iterator.writeByte(POP, pos + 2);
46: ca.setMaxStack(ca.getMaxStack() + 2);
47: } else {
48: // insertGap() may insert 4 bytes.
49: iterator.insertGap(2);
50: iterator.writeByte(ACONST_NULL, pos);
51: iterator.writeByte(SWAP, pos + 1);
52: ca.setMaxStack(ca.getMaxStack() + 1);
53: }
54:
55: pos = iterator.next();
56: }
57:
58: int mi = cp.addClassInfo(methodClassname);
59: String type = "(Ljava/lang/Object;" + typedesc + ")V";
60: int methodref = cp.addMethodrefInfo(mi, methodName,
61: type);
62: iterator.writeByte(INVOKESTATIC, pos);
63: iterator.write16bit(methodref, pos + 1);
64: }
65: }
66:
67: return pos;
68: }
69: }
|