KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > obfuscate > NameAndTypeUsageMarker


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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.obfuscate;
22
23 import proguard.classfile.*;
24 import proguard.classfile.util.SimplifiedVisitor;
25 import proguard.classfile.attribute.*;
26 import proguard.classfile.attribute.visitor.AttributeVisitor;
27 import proguard.classfile.attribute.annotation.*;
28 import proguard.classfile.constant.*;
29 import proguard.classfile.constant.visitor.ConstantVisitor;
30 import proguard.classfile.visitor.*;
31
32 /**
33  * This ClassVisitor marks all NameAndType constant pool entries that are
34  * being used in the program classes it visits.
35  *
36  * @see NameAndTypeShrinker
37  *
38  * @author Eric Lafortune
39  */

40 public class NameAndTypeUsageMarker
41 extends SimplifiedVisitor
42 implements ClassVisitor,
43              ConstantVisitor,
44              AttributeVisitor
45 {
46     // A visitor info flag to indicate the NameAndType constant pool entry is being used.
47
private static final Object JavaDoc USED = new Object JavaDoc();
48
49
50     // Implementations for ClassVisitor.
51

52     public void visitProgramClass(ProgramClass programClass)
53     {
54         // Mark the NameAndType entries referenced by all other constant pool
55
// entries.
56
programClass.constantPoolEntriesAccept(this);
57
58         // Mark the NameAndType entries referenced by all EnclosingMethod
59
// attributes.
60
programClass.attributesAccept(this);
61     }
62
63
64     // Implementations for ConstantVisitor.
65

66     public void visitAnyConstant(Clazz clazz, Constant constant) {}
67
68
69     public void visitFieldrefConstant(Clazz clazz, FieldrefConstant fieldrefConstant)
70     {
71         visitRefConstant(clazz, fieldrefConstant);
72     }
73
74
75     public void visitInterfaceMethodrefConstant(Clazz clazz, InterfaceMethodrefConstant interfaceMethodrefConstant)
76     {
77         visitRefConstant(clazz, interfaceMethodrefConstant);
78     }
79
80
81     public void visitMethodrefConstant(Clazz clazz, MethodrefConstant methodrefConstant)
82     {
83         visitRefConstant(clazz, methodrefConstant);
84     }
85
86
87     private void visitRefConstant(Clazz clazz, RefConstant refConstant)
88     {
89         markNameAndTypeConstant(clazz, refConstant.u2nameAndTypeIndex);
90     }
91
92
93     // Implementations for AttributeVisitor.
94

95     public void visitAnyAttribute(Clazz clazz, Attribute attribute) {}
96
97
98     public void visitEnclosingMethodAttribute(Clazz clazz, EnclosingMethodAttribute enclosingMethodAttribute)
99     {
100         if (enclosingMethodAttribute.u2nameAndTypeIndex != 0)
101         {
102             markNameAndTypeConstant(clazz, enclosingMethodAttribute.u2nameAndTypeIndex);
103         }
104     }
105
106
107     // Small utility methods.
108

109     /**
110      * Marks the given UTF-8 constant pool entry of the given class.
111      */

112     private void markNameAndTypeConstant(Clazz clazz, int index)
113     {
114          markAsUsed((NameAndTypeConstant)((ProgramClass)clazz).getConstant(index));
115     }
116
117
118     /**
119      * Marks the given VisitorAccepter as being used.
120      * In this context, the VisitorAccepter will be a NameAndTypeConstant object.
121      */

122     private static void markAsUsed(VisitorAccepter visitorAccepter)
123     {
124         visitorAccepter.setVisitorInfo(USED);
125     }
126
127
128     /**
129      * Returns whether the given VisitorAccepter has been marked as being used.
130      * In this context, the VisitorAccepter will be a NameAndTypeConstant object.
131      */

132     static boolean isUsed(VisitorAccepter visitorAccepter)
133     {
134         return visitorAccepter.getVisitorInfo() == USED;
135     }
136 }
137
Popular Tags