VariableRemapper.java revision cfead78069f3dc32998dc118ee08cab3867acea2
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2011 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.classfile.editor;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.*;
25import proguard.classfile.attribute.visitor.*;
26import proguard.classfile.instruction.*;
27import proguard.classfile.instruction.visitor.InstructionVisitor;
28import proguard.classfile.util.SimplifiedVisitor;
29
30/**
31 * This AttributeVisitor remaps variable indexes in all attributes that it
32 * visits, based on a given index map.
33 *
34 * @author Eric Lafortune
35 */
36public class VariableRemapper
37extends      SimplifiedVisitor
38implements   AttributeVisitor,
39             InstructionVisitor,
40             LocalVariableInfoVisitor,
41             LocalVariableTypeInfoVisitor
42{
43    private static final boolean DEBUG = false;
44
45
46    private final CodeAttributeEditor codeAttributeEditor = new CodeAttributeEditor();
47
48    private int[] variableMap;
49
50
51    /**
52     * Sets the given mapping of old variable indexes to their new indexes.
53     * Variables that should disappear can be mapped to -1.
54     */
55    public void setVariableMap(int[] variableMap)
56    {
57        this.variableMap = variableMap;
58    }
59
60
61    // Implementations for AttributeVisitor.
62
63    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
64
65
66    public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
67    {
68        if (DEBUG)
69        {
70            System.out.println("VariableRemapper: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
71            for (int index= 0; index < codeAttribute.u2maxLocals; index++)
72            {
73                System.out.println("  v"+index+" -> "+variableMap[index]);
74            }
75        }
76
77        // Remap the variables of the attributes, before editing the code and
78        // cleaning up its local variable frame.
79        codeAttribute.attributesAccept(clazz, method, this);
80
81        // Initially, the code attribute editor doesn't contain any changes.
82        codeAttributeEditor.reset(codeAttribute.u4codeLength);
83
84        // Remap the variables of the instructions.
85        codeAttribute.instructionsAccept(clazz, method, this);
86
87        // Apply the code atribute editor.
88        codeAttributeEditor.visitCodeAttribute(clazz, method, codeAttribute);
89    }
90
91
92    public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
93    {
94        // Remap the variable references of the local variables.
95        localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
96    }
97
98
99    public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
100    {
101        // Remap the variable references of the local variables.
102        localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
103    }
104
105
106    // Implementations for LocalVariableInfoVisitor.
107
108    public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
109    {
110        localVariableInfo.u2index =
111            remapVariable(localVariableInfo.u2index);
112    }
113
114
115    // Implementations for LocalVariableTypeInfoVisitor.
116
117    public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
118    {
119        localVariableTypeInfo.u2index =
120            remapVariable(localVariableTypeInfo.u2index);
121    }
122
123
124    // Implementations for InstructionVisitor.
125
126    public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
127
128
129    public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
130    {
131        // Is the new variable index different from the original one?
132        int oldVariableIndex = variableInstruction.variableIndex;
133        int newVariableIndex = remapVariable(oldVariableIndex);
134        if (newVariableIndex != oldVariableIndex)
135        {
136            // Replace the instruction.
137            Instruction replacementInstruction =
138                new VariableInstruction(variableInstruction.opcode,
139                                        newVariableIndex,
140                                        variableInstruction.constant).shrink();
141
142            codeAttributeEditor.replaceInstruction(offset, replacementInstruction);
143        }
144    }
145
146
147    // Small utility methods.
148
149    /**
150     * Returns the new variable index of the given variable.
151     */
152    private int remapVariable(int variableIndex)
153    {
154        return variableMap[variableIndex];
155    }
156}
157