KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > peephole > VariableShrinker


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.peephole;
22
23 import proguard.optimize.*;
24 import proguard.optimize.info.*;
25 import proguard.classfile.util.*;
26 import proguard.classfile.attribute.visitor.AttributeVisitor;
27 import proguard.classfile.attribute.*;
28 import proguard.classfile.visitor.MemberVisitor;
29 import proguard.classfile.editor.VariableEditor;
30 import proguard.classfile.*;
31
32 /**
33  * This MemberVisitor removes unused local variables from the code of the methods
34  * that it visits.
35  *
36  * @see ParameterUsageMarker
37  * @see MethodStaticizer
38  * @see MethodDescriptorShrinker
39  * @author Eric Lafortune
40  */

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

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

68     public VariableShrinker(MemberVisitor extraVariableMemberVisitor)
69     {
70         this.extraVariableMemberVisitor = extraVariableMemberVisitor;
71     }
72
73
74     // Implementations for AttributeVisitor.
75

76     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
77
78
79     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
80     {
81         if ((method.getAccessFlags() & ClassConstants.INTERNAL_ACC_ABSTRACT) == 0)
82         {
83             // Figure out the local variables that are used by the code.
84
variableUsageMarker.visitCodeAttribute(clazz, method, codeAttribute);
85
86             // Get the total size of the local variable frame.
87
int variablesSize = variableUsageMarker.getVariablesSize();
88
89             // The descriptor may have been been shrunk already, so get the
90
// original parameter size.
91
int parameterSize = ClassUtil.internalMethodParameterSize(method.getDescriptor(clazz));
92
93             if (DEBUG)
94             {
95                 System.out.println("VariableShrinker: "+clazz.getName()+"."+method.getName(clazz)+method.getDescriptor(clazz));
96                 System.out.println(" parameter size = " + parameterSize);
97                 System.out.println(" variables size = " + variablesSize);
98             }
99
100             // Make sure the size of the local variable frame is set correctly.
101
codeAttribute.u2maxLocals = variablesSize;
102
103             // Delete unused local variables from the local variable frame.
104
variableEditor.reset(variablesSize);
105
106             for (int variableIndex = parameterSize+1; variableIndex < variablesSize; variableIndex++)
107             {
108                 // Is the variable not required?
109
if (!variableUsageMarker.isVariableUsed(variableIndex))
110                 {
111                     if (DEBUG)
112                     {
113                         System.out.println(" Deleting local variable #"+variableIndex);
114                     }
115
116                     // Delete the unused variable.
117
variableEditor.deleteVariable(variableIndex);
118
119                     // Visit the method, if required.
120
if (extraVariableMemberVisitor != null)
121                     {
122                         method.accept(clazz, extraVariableMemberVisitor);
123                     }
124                 }
125             }
126
127             // Shift all remaining parameters and variables in the byte code.
128
variableEditor.visitCodeAttribute(clazz, method, codeAttribute);
129         }
130     }
131 }
132
Popular Tags