001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This library is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This library is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this library; if not, write to the Free Software Foundation,
019: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.optimize.peephole;
022:
023: import proguard.classfile.*;
024: import proguard.classfile.attribute.*;
025: import proguard.classfile.attribute.visitor.AttributeVisitor;
026: import proguard.classfile.editor.VariableEditor;
027: import proguard.classfile.util.*;
028: import proguard.classfile.visitor.MemberVisitor;
029: import proguard.optimize.*;
030: import proguard.optimize.info.*;
031:
032: /**
033: * This MemberVisitor removes unused local variables from the code of the methods
034: * that it visits.
035: *
036: * @see ParameterUsageMarker
037: * @see MethodStaticizer
038: * @see MethodDescriptorShrinker
039: * @author Eric Lafortune
040: */
041: public class VariableShrinker extends SimplifiedVisitor implements
042: AttributeVisitor {
043: private static final boolean DEBUG = false;
044:
045: private final MemberVisitor extraVariableMemberVisitor;
046:
047: private final VariableUsageMarker variableUsageMarker = new VariableUsageMarker();
048: private final VariableEditor variableEditor = new VariableEditor();
049:
050: /**
051: * Creates a new VariableShrinker.
052: */
053: public VariableShrinker() {
054: this (null);
055: }
056:
057: /**
058: * Creates a new VariableShrinker with an extra visitor.
059: * @param extraVariableMemberVisitor an optional extra visitor for all
060: * removed variables.
061: */
062: public VariableShrinker(MemberVisitor extraVariableMemberVisitor) {
063: this .extraVariableMemberVisitor = extraVariableMemberVisitor;
064: }
065:
066: // Implementations for AttributeVisitor.
067:
068: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
069: }
070:
071: public void visitCodeAttribute(Clazz clazz, Method method,
072: CodeAttribute codeAttribute) {
073: if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0) {
074: // Compute the parameter size.
075: int parameterSize = ClassUtil.internalMethodParameterSize(
076: method.getDescriptor(clazz), method
077: .getAccessFlags());
078:
079: // Get the total size of the local variable frame.
080: int maxLocals = codeAttribute.u2maxLocals;
081:
082: if (DEBUG) {
083: System.out.println("VariableShrinker: "
084: + clazz.getName() + "." + method.getName(clazz)
085: + method.getDescriptor(clazz));
086: System.out.println(" Parameter size = "
087: + parameterSize);
088: System.out.println(" Max locals = " + maxLocals);
089: }
090:
091: // Figure out the local variables that are used by the code.
092: variableUsageMarker.visitCodeAttribute(clazz, method,
093: codeAttribute);
094:
095: // Delete unused local variables from the local variable frame.
096: variableEditor.reset(maxLocals);
097:
098: for (int variableIndex = parameterSize + 1; variableIndex < maxLocals; variableIndex++) {
099: // Is the variable not required?
100: if (!variableUsageMarker.isVariableUsed(variableIndex)) {
101: if (DEBUG) {
102: System.out
103: .println(" Deleting local variable #"
104: + variableIndex);
105: }
106:
107: // Delete the unused variable.
108: variableEditor.deleteVariable(variableIndex);
109:
110: // Visit the method, if required.
111: if (extraVariableMemberVisitor != null) {
112: method
113: .accept(clazz,
114: extraVariableMemberVisitor);
115: }
116: }
117: }
118:
119: // Shift all remaining parameters and variables in the byte code.
120: variableEditor.visitCodeAttribute(clazz, method,
121: codeAttribute);
122: }
123: }
124: }
|