1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  *             of Java bytecode.
4  *
5  * Copyright (c) 2002-2014 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 import java.util.Arrays;
31 
32 /**
33  * This AttributeVisitor marks the local variables that are used in the code
34  * attributes that it visits.
35  *
36  * @author Eric Lafortune
37  */
38 public class VariableUsageMarker
39 extends      SimplifiedVisitor
40 implements   AttributeVisitor,
41              InstructionVisitor
42 {
43     private boolean[] variableUsed = new boolean[ClassConstants.TYPICAL_VARIABLES_SIZE];
44 
45 
46     /**
47      * Returns whether the given variable has been marked as being used.
48      */
isVariableUsed(int variableIndex)49     public boolean isVariableUsed(int variableIndex)
50     {
51         return variableUsed[variableIndex];
52     }
53 
54 
55     // Implementations for AttributeVisitor.
56 
visitAnyAttribute(Clazz clazz, Attribute attribute)57     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
58 
59 
visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)60     public void visitCodeAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute)
61     {
62         int maxLocals = codeAttribute.u2maxLocals;
63 
64         // Try to reuse the previous array.
65         if (variableUsed.length < maxLocals)
66         {
67             // Create a new array.
68             variableUsed = new boolean[maxLocals];
69         }
70         else
71         {
72             // Reset the array.
73             Arrays.fill(variableUsed, 0, maxLocals, false);
74         }
75 
76         codeAttribute.instructionsAccept(clazz, method, this);
77     }
78 
79 
80     // Implementations for InstructionVisitor.
81 
visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction)82     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
83 
84 
visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)85     public void visitVariableInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, VariableInstruction variableInstruction)
86     {
87         // Mark the variable.
88         variableUsed[variableInstruction.variableIndex] = true;
89 
90         // Account for Category 2 instructions, which take up two entries.
91         if (variableInstruction.isCategory2())
92         {
93             variableUsed[variableInstruction.variableIndex + 1] = true;
94         }
95     }
96 }
97