KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > info > VariableUsageMarker


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 program 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 program 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 General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize.info;
22
23 import proguard.classfile.*;
24 import proguard.classfile.attribute.*;
25 import proguard.classfile.attribute.visitor.AttributeVisitor;
26 import proguard.classfile.instruction.*;
27 import proguard.classfile.instruction.visitor.InstructionVisitor;
28 import proguard.classfile.util.SimplifiedVisitor;
29
30 /**
31  * This AttributeVisitor marks the local variables that are used in the code
32  * attributes that it visits.
33  *
34  * @author Eric Lafortune
35  */

36 public class VariableUsageMarker
37 extends SimplifiedVisitor
38 implements AttributeVisitor,
39              InstructionVisitor
40 {
41     private int variablesSize;
42     private boolean[] variableUsed = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE];
43
44
45     /**
46      * Returns the size of the variables frame of the most recently visited
47      * code attribute.
48      */

49     public int getVariablesSize()
50     {
51         return variablesSize;
52     }
53
54
55     /**
56      * Returns whether the given variable has been marked as being used.
57      */

58     public boolean isVariableUsed(int variableIndex)
59     {
60         return variableUsed[variableIndex];
61     }
62
63
64     // Implementations for AttributeVisitor.
65

66     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
67
68
69     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
70     {
71         int maxLocals = codeAttribute.u2maxLocals;
72
73         // Try to reuse the previous array.
74
if (variableUsed.length < maxLocals)
75         {
76             variableUsed = new boolean[maxLocals];
77         }
78         else
79         {
80             for (int index = 0; index < maxLocals; index++)
81             {
82                 variableUsed[index] = false;
83             }
84         }
85
86         variablesSize = maxLocals;
87
88         codeAttribute.instructionsAccept(clazz, method, this);
89     }
90
91
92     // Implementations for InstructionVisitor.
93

94     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
95
96
97     public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
98     {
99         // Mark the variable.
100
variableUsed[variableInstruction.variableIndex] = true;
101
102         // Account for Category 2 instructions, which take up two entries.
103
if (variableInstruction.isCategory2())
104         {
105             variableUsed[variableInstruction.variableIndex + 1] = true;
106         }
107     }
108 }
109
Popular Tags