KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > optimize > info > AccessMethodMarker


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 library 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 library 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 Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.optimize.info;
22
23 import proguard.classfile.util.*;
24 import proguard.classfile.instruction.visitor.InstructionVisitor;
25 import proguard.classfile.instruction.*;
26 import proguard.classfile.constant.visitor.ConstantVisitor;
27 import proguard.classfile.constant.*;
28 import proguard.classfile.visitor.*;
29 import proguard.classfile.*;
30 import proguard.classfile.attribute.CodeAttribute;
31
32 /**
33  * This InstructionVisitor marks the types of class accesses and class member
34  * accesses of the methods whose instructions it visits.
35  *
36  * @author Eric Lafortune
37  */

38 public class AccessMethodMarker
39 extends SimplifiedVisitor
40 implements InstructionVisitor,
41              ConstantVisitor,
42              ClassVisitor,
43              MemberVisitor
44 {
45     private Method invokingMethod;
46
47
48     // Implementations for InstructionVisitor.
49

50     public void visitAnyInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, Instruction instruction) {}
51
52
53     public void visitConstantInstruction(Clazz clazz, Method method, CodeAttribute codeAttribute, int offset, ConstantInstruction constantInstruction)
54     {
55         invokingMethod = method;
56
57         clazz.constantPoolEntryAccept(constantInstruction.constantIndex, this);
58     }
59
60
61     // Implementations for ConstantVisitor.
62

63     public void visitAnyConstant(Clazz clazz, Constant constant) {}
64
65
66     public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
67     {
68         // Check the referenced class or class member, if any.
69
stringConstant.referencedClassAccept(this);
70        stringConstant.referencedMemberAccept(this);
71     }
72
73
74     public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant)
75     {
76         // Check the referenced class.
77
clazz.constantPoolEntryAccept(refConstant.u2classIndex, this);
78
79         // Check the referenced class member itself.
80
refConstant.referencedClassAccept(this);
81         refConstant.referencedMemberAccept(this);
82     }
83
84
85     public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
86     {
87         // Check the referenced class.
88
classConstant.referencedClassAccept(this);
89     }
90
91
92     // Implementations for ClassVisitor.
93

94     public void visitAnyClass(Clazz clazz)
95     {
96         int accessFlags = clazz.getAccessFlags();
97
98         if ((accessFlags & ClassConstants.INTERNAL_ACC_PUBLIC) == 0)
99         {
100             setAccessesPackageCode(invokingMethod);
101         }
102     }
103
104
105     // Implementations for MemberVisitor.
106

107     public void visitAnyMember(Clazz clazz, Member member)
108     {
109         int accessFlags = member.getAccessFlags();
110
111         if ((accessFlags & ClassConstants.INTERNAL_ACC_PRIVATE) != 0)
112         {
113             setAccessesPrivateCode(invokingMethod);
114         }
115         else if ((accessFlags & ClassConstants.INTERNAL_ACC_PROTECTED) != 0)
116         {
117             setAccessesProtectedCode(invokingMethod);
118         }
119         else if ((accessFlags & ClassConstants.INTERNAL_ACC_PUBLIC) == 0)
120         {
121             setAccessesPackageCode(invokingMethod);
122         }
123     }
124
125
126     // Small utility methods.
127

128     private static void setAccessesPrivateCode(Method method)
129     {
130         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
131         if (info != null)
132         {
133             info.setAccessesPrivateCode();
134         }
135     }
136
137
138     public static boolean accessesPrivateCode(Method method)
139     {
140         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
141         return info == null || info.accessesPrivateCode();
142     }
143
144
145     private static void setAccessesPackageCode(Method method)
146     {
147         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
148         if (info != null)
149         {
150             info.setAccessesPackageCode();
151         }
152     }
153
154
155     public static boolean accessesPackageCode(Method method)
156     {
157         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
158         return info == null || info.accessesPackageCode();
159     }
160
161
162     private static void setAccessesProtectedCode(Method method)
163     {
164         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
165         if (info != null)
166         {
167             info.setAccessesProtectedCode();
168         }
169     }
170
171
172     public static boolean accessesProtectedCode(Method method)
173     {
174         MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
175         return info == null || info.accessesProtectedCode();
176     }
177 }
178
Popular Tags