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;
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.info.ParameterUsageMarker;
030:
031: /**
032: * This MemberVisitor removes unused parameters from the code of the methods
033: * that it visits.
034: *
035: * @see ParameterUsageMarker
036: * @see MethodStaticizer
037: * @see MethodDescriptorShrinker
038: * @author Eric Lafortune
039: */
040: public class ParameterShrinker extends SimplifiedVisitor implements
041: AttributeVisitor {
042: private static final boolean DEBUG = false;
043:
044: private final MemberVisitor extraVariableMemberVisitor;
045:
046: private final VariableEditor variableEditor = new VariableEditor();
047:
048: /**
049: * Creates a new ParameterShrinker.
050: */
051: public ParameterShrinker() {
052: this (null);
053: }
054:
055: /**
056: * Creates a new ParameterShrinker with an extra visitor.
057: * @param extraVariableMemberVisitor an optional extra visitor for all
058: * removed parameters.
059: */
060: public ParameterShrinker(MemberVisitor extraVariableMemberVisitor) {
061: this .extraVariableMemberVisitor = extraVariableMemberVisitor;
062: }
063:
064: // Implementations for AttributeVisitor.
065:
066: public void visitAnyAttribute(Clazz clazz, Attribute attribute) {
067: }
068:
069: public void visitCodeAttribute(Clazz clazz, Method method,
070: CodeAttribute codeAttribute) {
071: // Get the original parameter size that was saved.
072: int oldParameterSize = ParameterUsageMarker
073: .getParameterSize(method);
074:
075: // Compute the new parameter size from the shrunk descriptor.
076: int newParameterSize = ClassUtil.internalMethodParameterSize(
077: method.getDescriptor(clazz), method.getAccessFlags());
078:
079: if (oldParameterSize > newParameterSize) {
080: // Get the total size of the local variable frame.
081: int maxLocals = codeAttribute.u2maxLocals;
082:
083: if (DEBUG) {
084: System.out.println("ParameterShrinker: "
085: + clazz.getName() + "." + method.getName(clazz)
086: + method.getDescriptor(clazz));
087: System.out.println(" Old parameter size = "
088: + oldParameterSize);
089: System.out.println(" New parameter size = "
090: + newParameterSize);
091: System.out.println(" Max locals = "
092: + maxLocals);
093: }
094:
095: // Delete unused variables from the local variable frame.
096: variableEditor.reset(maxLocals);
097:
098: for (int parameterIndex = 0; parameterIndex < oldParameterSize; parameterIndex++) {
099: // Is the variable not required as a parameter?
100: if (!ParameterUsageMarker.isParameterUsed(method,
101: parameterIndex)) {
102: if (DEBUG) {
103: System.out.println(" Deleting parameter #"
104: + parameterIndex);
105: }
106:
107: // Delete the unused variable.
108: variableEditor.deleteVariable(parameterIndex);
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: }
|