KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > classfile > editor > AccessFixer


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.classfile.editor;
22
23 import proguard.classfile.util.*;
24 import proguard.classfile.visitor.*;
25 import proguard.classfile.constant.visitor.ConstantVisitor;
26 import proguard.classfile.constant.*;
27 import proguard.classfile.*;
28
29 /**
30  * This ConstantVisitor fixes the access modifiers of all classes and class
31  * members that are referenced by the constants that it visits.
32  *
33  * @author Eric Lafortune
34  */

35 public class AccessFixer
36 extends SimplifiedVisitor
37 implements ConstantVisitor,
38              ClassVisitor,
39              MemberVisitor
40 {
41     private Clazz referencingClass;
42
43
44     // Implementations for ConstantVisitor.
45

46     public void visitAnyConstant(Clazz clazz, Constant constant) {}
47
48
49     public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
50     {
51         referencingClass = clazz;
52
53         // Make sure the access flags of the referenced class or class member,
54
// if any, are acceptable.
55
stringConstant.referencedClassAccept(this);
56         stringConstant.referencedMemberAccept(this);
57     }
58
59
60     public void visitAnyRefConstant(Clazz clazz, RefConstant refConstant)
61     {
62         referencingClass = clazz;
63
64         // Make sure the access flags of the referenced class member are acceptable.
65
refConstant.referencedMemberAccept(this);
66     }
67
68
69     public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
70     {
71         referencingClass = clazz;
72
73         // Make sure the access flags of the referenced class are acceptable.
74
classConstant.referencedClassAccept(this);
75     }
76
77
78     // Implementations for ClassVisitor.
79

80     public void visitLibraryClass(LibraryClass libraryClass) {}
81
82
83     public void visitProgramClass(ProgramClass programClass)
84     {
85         int currentAccessFlags = programClass.getAccessFlags();
86         int currentAccessLevel = AccessUtil.accessLevel(currentAccessFlags);
87
88         // Compute the required access level.
89
Clazz referencingClass = this.referencingClass;
90         int requiredAccessLevel =
91             programClass.equals(this.referencingClass) ? AccessUtil.PRIVATE :
92             inSamePackage(programClass, referencingClass) ? AccessUtil.PACKAGE_VISIBLE :
93             referencingClass.extends_(programClass) ? AccessUtil.PROTECTED :
94                                                             AccessUtil.PUBLIC;
95
96         // Fix the class access flags if necessary.
97
if (currentAccessLevel < requiredAccessLevel)
98         {
99             programClass.u2accessFlags =
100                 AccessUtil.replaceAccessFlags(currentAccessFlags,
101                                               AccessUtil.accessFlags(requiredAccessLevel));
102         }
103     }
104
105
106     // Implementations for MemberVisitor.
107

108     public void visitLibraryMember(LibraryClass libraryClass, LibraryMember libraryMember) {}
109
110
111     public void visitProgramMember(ProgramClass programClass, ProgramMember programMember)
112     {
113         int currentAccessFlags = programMember.getAccessFlags();
114         int currentAccessLevel = AccessUtil.accessLevel(currentAccessFlags);
115
116         // Compute the required access level.
117
int requiredAccessLevel =
118             programClass.equals(referencingClass) ? AccessUtil.PRIVATE :
119             inSamePackage(programClass, referencingClass) ? AccessUtil.PACKAGE_VISIBLE :
120                                                             AccessUtil.PUBLIC;
121         // Fix the class member access flags if necessary.
122
if (currentAccessLevel < requiredAccessLevel)
123         {
124             programMember.u2accessFlags =
125                 AccessUtil.replaceAccessFlags(currentAccessFlags,
126                                               AccessUtil.accessFlags(requiredAccessLevel));
127         }
128     }
129
130
131     // Small utility methods.
132

133     private boolean inSamePackage(ProgramClass class1, Clazz class2)
134     {
135         return ClassUtil.internalPackageName(class1.getName()).equals(
136                ClassUtil.internalPackageName(class2.getName()));
137     }
138 }
139
Popular Tags