AccessMethodMarker.java revision b72c5c2e5482cf10117b2b25f642f7616b2326c3
1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2009 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.optimize.info;
22
23import proguard.classfile.*;
24import proguard.classfile.attribute.CodeAttribute;
25import proguard.classfile.constant.*;
26import proguard.classfile.constant.visitor.ConstantVisitor;
27import proguard.classfile.instruction.*;
28import proguard.classfile.instruction.visitor.InstructionVisitor;
29import proguard.classfile.util.SimplifiedVisitor;
30import proguard.classfile.visitor.*;
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 */
38public class AccessMethodMarker
39extends      SimplifiedVisitor
40implements   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    /**
139     * Returns whether the given method accesses private class members.
140     */
141    public static boolean accessesPrivateCode(Method method)
142    {
143        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
144        return info == null || info.accessesPrivateCode();
145    }
146
147
148    private static void setAccessesPackageCode(Method method)
149    {
150        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
151        if (info != null)
152        {
153            info.setAccessesPackageCode();
154        }
155    }
156
157
158    /**
159     * Returns whether the given method accesses package visible classes or class
160     * members.
161     */
162    public static boolean accessesPackageCode(Method method)
163    {
164        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
165        return info == null || info.accessesPackageCode();
166    }
167
168
169    private static void setAccessesProtectedCode(Method method)
170    {
171        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
172        if (info != null)
173        {
174            info.setAccessesProtectedCode();
175        }
176    }
177
178
179    /**
180     * Returns whether the given method accesses protected class members.
181     */
182    public static boolean accessesProtectedCode(Method method)
183    {
184        MethodOptimizationInfo info = MethodOptimizationInfo.getMethodOptimizationInfo(method);
185        return info == null || info.accessesProtectedCode();
186    }
187}
188