KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > ParameterShrinker


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize;
22
23 import proguard.classfile.util.SimplifiedVisitor;
24 import proguard.classfile.visitor.MemberVisitor;
25 import proguard.classfile.editor.VariableEditor;
26 import proguard.classfile.*;
27 import proguard.classfile.attribute.*;
28 import proguard.classfile.attribute.visitor.AttributeVisitor;
29 import proguard.optimize.info.ParameterUsageMarker;
30
31 /**
32  * This MemberVisitor removes unused parameters from the code of the methods
33  * that it visits.
34  *
35  * @see ParameterUsageMarker
36  * @see MethodStaticizer
37  * @see MethodDescriptorShrinker
38  * @author Eric Lafortune
39  */

40 public class ParameterShrinker
41 extends SimplifiedVisitor
42 implements AttributeVisitor
43 {
44     private static final boolean DEBUG = false;
45
46
47     private MemberVisitor extraVariableMemberVisitor;
48
49     private VariableEditor variableEditor = new VariableEditor();
50
51
52     /**
53      * Creates a new ParameterShrinker.
54      */

55     public ParameterShrinker()
56     {
57         this(null);
58     }
59
60
61     /**
62      * Creates a new ParameterShrinker with an extra visitor.
63      * @param extraVariableMemberVisitor an optional extra visitor for all
64      * removed parameters.
65      */

66     public ParameterShrinker(MemberVisitor extraVariableMemberVisitor)
67     {
68         this.extraVariableMemberVisitor = extraVariableMemberVisitor;
69     }
70
71
72     // Implementations for AttributeVisitor.
73

74     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
75
76
77     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
78     {
79         if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0)
80         {
81             // Get the total size of the local variable frame.
82
int variablesSize = codeAttribute.u2maxLocals;
83
84             // The descriptor may have been been shrunk already, so get the
85
// original parameter size.
86
int parameterSize = ParameterUsageMarker.getParameterSize(method);
87
88             if (DEBUG)
89             {
90                 System.out.println("ParameterShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
91                 System.out.println(" parameter size = " + parameterSize);
92             }
93
94             // Delete unused variables from the local variable frame.
95
variableEditor.reset(variablesSize);
96
97             for (int parameterIndex = 0; parameterIndex < parameterSize; parameterIndex++)
98             {
99                 // Is the variable not required as a parameter?
100
if (!ParameterUsageMarker.isParameterUsed(method, parameterIndex))
101                 {
102                     if (DEBUG)
103                     {
104                         System.out.println(" Deleting parameter #"+parameterIndex);
105                     }
106
107                     // Delete the unused variable.
108
variableEditor.deleteVariable(parameterIndex);
109
110                     // Visit the method, if required.
111
if (extraVariableMemberVisitor != null)
112                     {
113                         method.accept(clazz, extraVariableMemberVisitor);
114                     }
115                 }
116             }
117
118             // Shift all remaining parameters and variables in the byte code.
119
variableEditor.visitCodeAttribute(clazz, method, codeAttribute);
120         }
121     }
122 }
123
Popular Tags