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