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.bytecode.*;
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtClass;
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.CtField;
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.Modifier;
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalfinal public class TransformFieldAccess extends Transformer {
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String newClassname, newFieldname;
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private String fieldname;
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private CtClass fieldClass;
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private boolean isPrivate;
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /* cache */
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private int newIndex;
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    private ConstPool constPool;
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public TransformFieldAccess(Transformer next, CtField field,
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                String newClassname, String newFieldname)
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        super(next);
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.fieldClass = field.getDeclaringClass();
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.fieldname = field.getName();
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.isPrivate = Modifier.isPrivate(field.getModifiers());
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.newClassname = newClassname;
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.newFieldname = newFieldname;
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        this.constPool = null;
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public void initialize(ConstPool cp, CodeAttribute attr) {
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (constPool != cp)
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            newIndex = 0;
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    /**
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * Modify GETFIELD, GETSTATIC, PUTFIELD, and PUTSTATIC so that
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * a different field is accessed.  The new field must be declared
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * in a superclass of the class in which the original field is
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     * declared.
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal     */
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    public int transform(CtClass clazz, int pos,
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                         CodeIterator iterator, ConstPool cp)
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    {
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        int c = iterator.byteAt(pos);
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (c == GETFIELD || c == GETSTATIC
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                || c == PUTFIELD || c == PUTSTATIC) {
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            int index = iterator.u16bitAt(pos + 1);
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            String typedesc
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                = TransformReadField.isField(clazz.getClassPool(), cp,
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                fieldClass, fieldname, isPrivate, index);
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (typedesc != null) {
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                if (newIndex == 0) {
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    int nt = cp.addNameAndTypeInfo(newFieldname,
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                                   typedesc);
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    newIndex = cp.addFieldrefInfo(
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                        cp.addClassInfo(newClassname), nt);
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    constPool = cp;
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                }
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                iterator.write16bit(newIndex, pos + 1);
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return pos;
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
82