LocalVariableTypeUsageMarker.java revision b9cc48a43ed984587c939d02fba5316bf5c0df6e
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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
22package proguard.shrink;
23
24import proguard.classfile.*;
25import proguard.classfile.attribute.*;
26import proguard.classfile.attribute.visitor.*;
27import proguard.classfile.constant.Constant;
28import proguard.classfile.constant.visitor.ConstantVisitor;
29import proguard.classfile.util.SimplifiedVisitor;
30import proguard.classfile.visitor.ClassVisitor;
31
32/**
33 * This AttributeVisitor recursively marks all information that points to used
34 * classes, in the LocalVariableTable and LocalVariableTypeTable attributes that
35 * it visits.
36 *
37 * @see UsageMarker
38 *
39 * @author Eric Lafortune
40 */
41public class LocalVariableTypeUsageMarker
42extends      SimplifiedVisitor
43implements   AttributeVisitor,
44             LocalVariableInfoVisitor,
45             LocalVariableTypeInfoVisitor,
46             ClassVisitor,
47             ConstantVisitor
48{
49    private final UsageMarker usageMarker;
50
51    // Fields acting as a return parameters for several methods.
52    private boolean tableUsed;
53    private boolean variableInfoUsed;
54
55
56    /**
57     * Creates a new LocalVariableTypeUsageMarker.
58     * @param usageMarker the usage marker that is used to mark the classes
59     *                    and class members.
60     */
61    public LocalVariableTypeUsageMarker(UsageMarker usageMarker)
62    {
63        this.usageMarker = usageMarker;
64    }
65
66
67    // Implementations for AttributeVisitor.
68
69    public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
70
71
72    public void visitLocalVariableTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTableAttribute localVariableTableAttribute)
73    {
74        // Check and mark the individual entries.
75        tableUsed = false;
76        localVariableTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
77
78        // Mark the table if any of the entries is marked.
79        if (tableUsed)
80        {
81            usageMarker.markAsUsed(localVariableTableAttribute);
82
83            markConstant(clazz, localVariableTableAttribute.u2attributeNameIndex);
84        }
85    }
86
87
88    public void visitLocalVariableTypeTableAttribute(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeTableAttribute localVariableTypeTableAttribute)
89    {
90        // Check and mark the individual entries.
91        tableUsed = false;
92        localVariableTypeTableAttribute.localVariablesAccept(clazz, method, codeAttribute, this);
93
94        // Mark the table if any of the entries is marked.
95        if (tableUsed)
96        {
97            usageMarker.markAsUsed(localVariableTypeTableAttribute);
98
99            markConstant(clazz, localVariableTypeTableAttribute.u2attributeNameIndex);
100        }
101    }
102
103
104    // Implementations for LocalVariableInfoVisitor.
105
106    public void visitLocalVariableInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableInfo localVariableInfo)
107    {
108        // Only keep the local variable info if all of its classes are used.
109        variableInfoUsed = true;
110        localVariableInfo.referencedClassAccept(this);
111
112        if (variableInfoUsed)
113        {
114            // We got a positive used flag, so the local variable info is useful.
115            usageMarker.markAsUsed(localVariableInfo);
116
117            markConstant(clazz, localVariableInfo.u2nameIndex);
118            markConstant(clazz, localVariableInfo.u2descriptorIndex);
119
120            tableUsed = true;
121        }
122    }
123
124
125    // Implementations for LocalVariableTypeInfoVisitor.
126
127    public void visitLocalVariableTypeInfo(Clazz clazz, Method method, CodeAttribute codeAttribute, LocalVariableTypeInfo localVariableTypeInfo)
128    {
129        // Only keep the local variable info if all of its classes are used.
130        variableInfoUsed = true;
131        localVariableTypeInfo.referencedClassesAccept(this);
132
133        if (variableInfoUsed)
134        {
135            // We got a positive used flag, so the local variable info is useful.
136            usageMarker.markAsUsed(localVariableTypeInfo);
137
138            markConstant(clazz, localVariableTypeInfo.u2nameIndex);
139            markConstant(clazz, localVariableTypeInfo.u2signatureIndex);
140
141            tableUsed = true;
142        }
143    }
144
145
146    // Implementations for ClassVisitor.
147
148    public void visitLibraryClass(LibraryClass libraryClass) {}
149
150
151    public void visitProgramClass(ProgramClass programClass)
152    {
153        // Don't keep the local variable info if one of its classes is not used.
154        if (!usageMarker.isUsed(programClass))
155        {
156            variableInfoUsed = false;
157        }
158    }
159
160
161    // Implementations for ConstantVisitor.
162
163    public void visitAnyConstant(Clazz clazz, Constant constant)
164    {
165        usageMarker.markAsUsed(constant);
166    }
167
168
169    // Small utility methods.
170
171    /**
172     * Marks the given constant pool entry of the given class.
173     */
174    private void markConstant(Clazz clazz, int index)
175    {
176         clazz.constantPoolEntryAccept(index, this);
177    }
178}
179