1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 Eric Lafortune (eric (at) 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 */ 21 package proguard.classfile.editor; 22 23 import proguard.classfile.*; 24 import proguard.classfile.attribute.*; 25 import proguard.classfile.attribute.visitor.AttributeVisitor; 26 import proguard.classfile.util.SimplifiedVisitor; 27 28 /** 29 * This AttributeVisitor accumulates specified changes to local variables, and 30 * then applies these accumulated changes to the code attributes that it visits. 31 * 32 * @author Eric Lafortune 33 */ 34 public class VariableEditor 35 extends SimplifiedVisitor 36 implements AttributeVisitor 37 { 38 private boolean modified; 39 40 private boolean[] deleted = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE]; 41 private int[] variableMap = new int[ClassConstants.TYPICAL_VARIABLES_SIZE]; 42 43 private final VariableRemapper variableRemapper = new VariableRemapper(); 44 45 46 /** 47 * Resets the accumulated code changes. 48 * @param maxLocals the length of the local variable frame that will be 49 * edited next. 50 */ 51 public void reset(int maxLocals) 52 { 53 // Try to reuse the previous array. 54 if (deleted.length < maxLocals) 55 { 56 deleted = new boolean[maxLocals]; 57 } 58 else 59 { 60 for (int index = 0; index < maxLocals; index++) 61 { 62 deleted[index] = false; 63 } 64 } 65 66 modified = false; 67 } 68 69 70 /** 71 * Remembers to delete the given variable. 72 * @param variableIndex the index of the variable to be deleted. 73 */ 74 public void deleteVariable(int variableIndex) 75 { 76 deleted[variableIndex] = true; 77 78 modified = true; 79 } 80 81 82 /** 83 * Returns whether the given variable at the given offset has deleted. 84 */ 85 public boolean isDeleted(int instructionOffset) 86 { 87 return deleted[instructionOffset]; 88 } 89 90 91 // Implementations for AttributeVisitor. 92 93 public void visitAnyAttribute(Clazz clazz, Attribute attribute) {} 94 95 96 public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute) 97 { 98 // Avoid doing any work if nothing is changing anyway. 99 if (!modified) 100 { 101 return; 102 } 103 104 int oldMaxLocals = codeAttribute.u2maxLocals; 105 106 // Make sure there is a sufficiently large variable map. 107 if (variableMap.length < oldMaxLocals) 108 { 109 variableMap = new int[oldMaxLocals]; 110 } 111 112 // Fill out the variable map. 113 int newVariableIndex = 0; 114 for (int oldVariableIndex = 0; oldVariableIndex < oldMaxLocals; oldVariableIndex++) 115 { 116 variableMap[oldVariableIndex] = deleted[oldVariableIndex] ? 117 -1 : newVariableIndex++; 118 } 119 120 // Set the map. 121 variableRemapper.setVariableMap(variableMap); 122 123 // Remap the variables. 124 variableRemapper.visitCodeAttribute(clazz, method, codeAttribute); 125 126 // Update the length of local variable frame. 127 codeAttribute.u2maxLocals = newVariableIndex; 128 } 129 } 130