KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > UnusedParameterInvocationUnit


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.*;
24 import proguard.classfile.visitor.MemberVisitor;
25 import proguard.classfile.util.SimplifiedVisitor;
26 import proguard.classfile.constant.*;
27 import proguard.classfile.constant.visitor.ConstantVisitor;
28 import proguard.classfile.instruction.*;
29 import proguard.classfile.attribute.CodeAttribute;
30 import proguard.evaluation.*;
31 import proguard.evaluation.value.*;
32 import proguard.optimize.info.ParameterUsageMarker;
33
34 /**
35  * This InvocationUnit removes unused parameters from the stack before invoking
36  * a method, and then delegates to another given InvocationUnit.
37  *
38  * @see ParameterUsageMarker
39  * @author Eric Lafortune
40  */

41 public class UnusedParameterInvocationUnit
42 extends SimplifiedVisitor
43 implements InvocationUnit,
44              ConstantVisitor,
45              MemberVisitor
46 {
47     private static final boolean DEBUG = false;
48
49
50     private InvocationUnit invocationUnit;
51
52     private Stack stack;
53
54
55     public UnusedParameterInvocationUnit(InvocationUnit invocationUnit)
56     {
57         this.invocationUnit = invocationUnit;
58     }
59
60
61     // Implementations for InvocationUnit.
62

63
64     public void enterMethod(Clazz clazz, Method method, Variables variables)
65     {
66         invocationUnit.enterMethod(clazz, method, variables);
67     }
68
69
70     public void exitMethod(Clazz clazz, Method method, Value returnValue)
71     {
72         invocationUnit.exitMethod(clazz, method, returnValue);
73     }
74
75
76     public void invokeMember(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction, Stack stack)
77     {
78         // Fix the stack if this is a method invocation of which some
79
// parameters are marked as unused.
80
this.stack = stack;
81         clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
82
83         invocationUnit.invokeMember(clazz,
84                                     method,
85                                     codeAttribute,
86                                     offset,
87                                     constantInstruction,
88                                     stack);
89     }
90
91
92     // Implementations for ConstantVisitor.
93

94     public void visitAnyConstant(Clazz clazz, Constant constant) {}
95
96
97     public void visitAnyMethodrefConstant(Clazz clazz, RefConstant refConstant)
98     {
99         refConstant.referencedMemberAccept(this);
100     }
101
102
103     // Implementations for MemberVisitor.
104

105     public void visitProgramMethod(ProgramClass programClass, ProgramMethod programMethod)
106     {
107         if (DEBUG)
108         {
109             System.out.println("UnusedParameterInvocationUnit: "+programMethod.getName(programClass)+programMethod.getDescriptor(programClass));
110         }
111
112         // Get the total size of the parameters.
113
int parameterSize = ParameterUsageMarker.getParameterSize(programMethod);
114
115         // Remove unused parameters.
116
for (int index = 0; index < parameterSize; index++)
117         {
118             if (!ParameterUsageMarker.isParameterUsed(programMethod, index))
119             {
120                 if (DEBUG)
121                 {
122                     System.out.println(" removing stack entry #"+(parameterSize - index - 1)+" ("+stack.getTop(parameterSize - index - 1)+")");
123                 }
124
125                 stack.removeTop(parameterSize - index - 1);
126             }
127         }
128     }
129
130
131     public void visitLibraryMethod(LibraryClass libraryClass, LibraryMethod libraryMethod) {}
132 }
133
Popular Tags