2
* ProGuard -- shrinking, optimization, obfuscation, and preverification
5
* Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
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)
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
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
22
package proguard.shrink;
24
import proguard.classfile.*;
25
import proguard.classfile.attribute.*;
26
import proguard.classfile.attribute.visitor.*;
27
import proguard.classfile.constant.Constant;
28
import proguard.classfile.constant.visitor.ConstantVisitor;
29
import proguard.classfile.util.SimplifiedVisitor;
30
import proguard.classfile.visitor.ClassVisitor;
33
* This AttributeVisitor recursively marks all information that points to used
34
* classes, in the LocalVariableTable and LocalVariableTypeTable attributes that
39
* @author Eric Lafortune
41
public class LocalVariableTypeUsageMarker
42
extends SimplifiedVisitor
43
implements AttributeVisitor,
44
LocalVariableInfoVisitor,
45
LocalVariableTypeInfoVisitor,
49
private final UsageMarker usageMarker;
51
// Fields acting as a return parameters for several methods.
52
private boolean tableUsed;
53
private boolean variableInfoUsed;
57
* Creates a new LocalVariableTypeUsageMarker.
58
* @param usageMarker the usage marker that is used to mark the classes
61
public LocalVariableTypeUsageMarker(UsageMarker usageMarker)
63
this.usageMarker = usageMarker;
67
// Implementations for AttributeVisitor.
69
public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
72
public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
74
// Check and mark the individual entries.
76
localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
78
// Mark the table if any of the entries is marked.
81
usageMarker.markAsUsed(localVariableTableAttribute);
83
markConstant(clazz, localVariableTableAttribute.u2attributeNameIndex);
88
public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
90
// Check and mark the individual entries.
92
localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
94
// Mark the table if any of the entries is marked.
97
usageMarker.markAsUsed(localVariableTypeTableAttribute);
99
markConstant(clazz, localVariableTypeTableAttribute.u2attributeNameIndex);
104
// Implementations for LocalVariableInfoVisitor.
106
public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
108
// Only keep the local variable info if all of its classes are used.
109
variableInfoUsed = true;
110
localVariableInfo.referencedClassAccept(this);
112
if (variableInfoUsed)
114
// We got a positive used flag, so the local variable info is useful.
115
usageMarker.markAsUsed(localVariableInfo);
117
markConstant(clazz, localVariableInfo.u2nameIndex);
118
markConstant(clazz, localVariableInfo.u2descriptorIndex);
125
// Implementations for LocalVariableTypeInfoVisitor.
127
public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
129
// Only keep the local variable info if all of its classes are used.
130
variableInfoUsed = true;
131
localVariableTypeInfo.referencedClassesAccept(this);
133
if (variableInfoUsed)
135
// We got a positive used flag, so the local variable info is useful.
136
usageMarker.markAsUsed(localVariableTypeInfo);
138
markConstant(clazz, localVariableTypeInfo.u2nameIndex);
139
markConstant(clazz, localVariableTypeInfo.u2signatureIndex);
146
// Implementations for ClassVisitor.
148
public void visitLibraryClass(LibraryClass libraryClass) {}
151
public void visitProgramClass(ProgramClass programClass)
153
// Don't keep the local variable info if one of its classes is not used.
154
if (!usageMarker.isUsed(programClass))
156
variableInfoUsed = false;
161
// Implementations for ConstantVisitor.
163
public void visitAnyConstant(Clazz clazz, Constant constant)
165
usageMarker.markAsUsed(constant);
169
// Small utility methods.
172
* Marks the given constant pool entry of the given class.
174
private void markConstant(Clazz clazz, int index)
176
clazz.constantPoolEntryAccept(index, this);