169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Javassist, a Java-bytecode translator toolkit. 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 1999-2007 Shigeru Chiba. All Rights Reserved. 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * The contents of this file are subject to the Mozilla Public License Version 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1.1 (the "License"); you may not use this file except in compliance with 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the License. Alternatively, the contents of this file may be used under 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * the terms of the GNU Lesser General Public License Version 2.1 or later. 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Software distributed under the License is distributed on an "AS IS" basis, 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * for the specific language governing rights and limitations under the 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * License. 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage javassist.convert; 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtClass; 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtField; 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.bytecode.*; 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalfinal public class TransformWriteField extends TransformReadField { 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public TransformWriteField(Transformer next, CtField field, 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String methodClassname, String methodName) 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal super(next, field, methodClassname, methodName); 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public int transform(CtClass tclazz, int pos, CodeIterator iterator, 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ConstPool cp) throws BadBytecode 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal { 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int c = iterator.byteAt(pos); 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (c == PUTFIELD || c == PUTSTATIC) { 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int index = iterator.u16bitAt(pos + 1); 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String typedesc = isField(tclazz.getClassPool(), cp, 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal fieldClass, fieldname, isPrivate, index); 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (typedesc != null) { 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (c == PUTSTATIC) { 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CodeAttribute ca = iterator.get(); 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.move(pos); 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal char c0 = typedesc.charAt(0); 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal if (c0 == 'J' || c0 == 'D') { // long or double 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal // insertGap() may insert 4 bytes. 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal pos = iterator.insertGap(3); 4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(ACONST_NULL, pos); 4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(DUP_X2, pos + 1); 4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(POP, pos + 2); 4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ca.setMaxStack(ca.getMaxStack() + 2); 4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal else { 5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal // insertGap() may insert 4 bytes. 5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal pos = iterator.insertGap(2); 5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(ACONST_NULL, pos); 5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(SWAP, pos + 1); 5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ca.setMaxStack(ca.getMaxStack() + 1); 5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal pos = iterator.next(); 5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int mi = cp.addClassInfo(methodClassname); 6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal String type = "(Ljava/lang/Object;" + typedesc + ")V"; 6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal int methodref = cp.addMethodrefInfo(mi, methodName, type); 6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.writeByte(INVOKESTATIC, pos); 6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal iterator.write16bit(methodref, pos + 1); 6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return pos; 7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 72