1b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/*
2b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ProGuard -- shrinking, optimization, obfuscation, and preverification
3b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *             of Java bytecode.
4b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
5b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Copyright (c) 2002-2009 Eric Lafortune (eric@graphics.cornell.edu)
6b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
7b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is free software; you can redistribute it and/or modify it
8b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * under the terms of the GNU General Public License as published by the Free
9b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * Software Foundation; either version 2 of the License, or (at your option)
10b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * any later version.
11b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
12b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This program is distributed in the hope that it will be useful, but WITHOUT
13b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * more details.
16b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
17b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * You should have received a copy of the GNU General Public License along
18b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * with this program; if not, write to the Free Software Foundation, Inc.,
19b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */
21b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopackage proguard.shrink;
22b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
23b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.*;
24b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.constant.*;
25b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.constant.visitor.ConstantVisitor;
26b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.util.SimplifiedVisitor;
27b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimport proguard.classfile.visitor.ClassVisitor;
28b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
29b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
30b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato/**
31b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * This ClassVisitor recursively marks all interface
32b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * classes that are being used in the visited class.
33b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
34b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @see UsageMarker
35b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato *
36b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato * @author Eric Lafortune
37b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato */
38b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratopublic class InterfaceUsageMarker
39b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoextends      SimplifiedVisitor
40b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onoratoimplements   ClassVisitor,
41b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato             ConstantVisitor
42b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato{
43b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private final UsageMarker usageMarker;
44b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
45b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Fields acting as a return parameters for several methods.
46b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private boolean used;
47b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    private boolean anyUsed;
48b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
49b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
50b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    /**
51b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * Creates a new InterfaceUsageMarker.
52b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     * @param usageMarker the usage marker that is used to mark the classes
53b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     *                    and class members.
54b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato     */
55b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public InterfaceUsageMarker(UsageMarker usageMarker)
56b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
57b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        this.usageMarker = usageMarker;
58b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
59b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
60b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
61b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Implementations for ClassVisitor.
62b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
63b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitProgramClass(ProgramClass programClass)
64b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
65b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        boolean classUsed         = usageMarker.isUsed(programClass);
66b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        boolean classPossiblyUsed = usageMarker.isPossiblyUsed(programClass);
67b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
68b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        if (classUsed || classPossiblyUsed)
69b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
70b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // Check if any interfaces are being used.
71b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            boolean oldAnyUsed = anyUsed;
72b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            anyUsed = false;
73b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
74b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            programClass.interfaceConstantsAccept(this);
75b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
76b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            classUsed |= anyUsed;
77b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            anyUsed = oldAnyUsed;
78b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
79b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // Is this an interface with a preliminary mark?
80b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            if (classPossiblyUsed)
81b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            {
82b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                // Should it be included now?
83b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                if (classUsed)
84b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                {
85b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    // At least one if this interface's interfaces is being used.
86b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    // Mark this interface as well.
87b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    usageMarker.markAsUsed(programClass);
88b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
89b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    // Mark this interface's name.
90b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    programClass.thisClassConstantAccept(this);
91b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
92b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    // Mark the superclass (java/lang/Object).
93b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    programClass.superClassConstantAccept(this);
94b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                }
95b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                else
96b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                {
97b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    // Unmark this interface, so we don't bother looking at it again.
98b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                    usageMarker.markAsUnused(programClass);
99b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                }
100b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            }
101b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
102b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
103b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // The return value.
104b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        used = classUsed;
105b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
106b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
107b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
108b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitLibraryClass(LibraryClass libraryClass)
109b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
110b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // The return values.
111b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        used    = true;
112b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        anyUsed = true;
113b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
114b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
115b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
116b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    // Implementations for ConstantVisitor.
117b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
118b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
119b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
120b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        boolean classUsed = usageMarker.isUsed(classConstant);
121b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
122b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        if (!classUsed)
123b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
124b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // The ClassConstant isn't marked as being used yet. But maybe it
125b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            // should be included as an interface, so check the actual class.
126b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            classConstant.referencedClassAccept(this);
127b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            classUsed = used;
128b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
129b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            if (classUsed)
130b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            {
131b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                // The class is being used. Mark the ClassConstant as being used
132b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                // as well.
133b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                usageMarker.markAsUsed(classConstant);
134b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
135b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato                clazz.constantPoolEntryAccept(classConstant.u2nameIndex, this);
136b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            }
137b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
138b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
139b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        // The return values.
140b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        used    =  classUsed;
141b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        anyUsed |= classUsed;
142b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
143b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
144b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato
145b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    public void visitUtf8Constant(Clazz clazz, Utf8Constant utf8Constant)
146b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    {
147b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        if (!usageMarker.isUsed(utf8Constant))
148b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        {
149b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato            usageMarker.markAsUsed(utf8Constant);
150b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato        }
151b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato    }
152b72c5c2e5482cf10117b2b25f642f7616b2326c3Joe Onorato}
153