KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > AccessChecker


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.tools.ajdoc;
23
24 import org.aspectj.compiler.base.ast.Modifiers;
25 import org.aspectj.compiler.base.ast.FieldDec;
26 import org.aspectj.compiler.base.ast.TypeDec;
27 import org.aspectj.compiler.base.ast.NameType;
28 import org.aspectj.compiler.base.ast.CodeDec;
29 import org.aspectj.compiler.crosscuts.ast.PointcutDec;
30
31 /**
32  * This utility tells whether a declaration is accessible
33  * based on its access modifiers (and that of its declaring
34  * class, if it is a member (i.e., including inner classes).
35  * <p><u>Instantiation and subclassing</u>:
36  * The constants should suffice for most uses, but subclassing
37  * is permitted if you need to implement new functionality or
38  * make new instances.
39  */

40 public abstract class AccessChecker {
41     // constants open doCanAccess to public to permit direct use
42

43     /** return true only for public elements */
44     public static final AccessChecker PUBLIC = new AccessChecker("public") {
45             public boolean doCanAccess(Modifiers mods, Object JavaDoc object) {
46                 return mods.isPublic();
47             }
48         };
49
50     /** return true for public and protected elements */
51     public static final AccessChecker PROTECTED = new AccessChecker("protected") {
52             public boolean doCanAccess(Modifiers mods, Object JavaDoc object) {
53                 return mods.isPublic() || mods.isProtected();
54             }
55         };
56
57     /** return true unless private elements */
58     public static final AccessChecker PACKAGE = new AccessChecker("package") {
59             public boolean doCanAccess(Modifiers mods, Object JavaDoc object) {
60                 return !mods.isPrivate();
61             }
62         };
63
64     /** return true for all elements */
65     public static final AccessChecker PRIVATE = new AccessChecker("private") {
66             public boolean doCanAccess(Modifiers mods, Object JavaDoc object) {
67                 return true;
68             }
69         };
70
71     /** lowercase option without - */
72     protected final String JavaDoc optionName;
73
74     /**
75      * Encourage use of static constants by prohibiting construction
76      * but permit new subclasses.
77      * Subclasses should ensure optionName is lowercase and
78      * doCanAccess is public if need be.
79      */

80     protected AccessChecker(String JavaDoc optionName){
81         this.optionName = optionName;
82     }
83
84     /** @return true if modifiers permitted for self and declaring type */
85     public boolean canAccess(FieldDec dec) {
86         if (null == dec) return false;
87         if (!canAccess(dec.getModifiers(), dec)) {
88             return false;
89         } else {
90             return canAccess(dec.getBytecodeTypeDec());
91         }
92     }
93
94     /** @return true if modifiers permitted for self and declaring type */
95     public boolean canAccess(CodeDec dec) {
96         if (null == dec) return false;
97         if (!canAccess(dec.getModifiers(), dec)) {
98             return false;
99         } else {
100             return canAccess(dec.getBytecodeTypeDec());
101         }
102     }
103
104     /** @return true if modifiers permitted for self and declaring type */
105     public boolean canAccess(PointcutDec dec) {
106         if (null == dec) return false;
107         if (!canAccess(dec.getModifiers(), dec)) {
108             return false;
109         } else {
110             return canAccess(dec.getBytecodeTypeDec());
111         }
112     }
113
114     /** decode dec modifiers and return whether access is permitted
115      * Access is permitted if it is permitted to the dec.
116      * The caller must prohibit access when displaying in the aspect
117      * (i.e., <code>canAccess(dec.getLexicalType())</code> or in
118      * the target class
119      * (i.e., <code>canAccess(dec.getDeclaringType())</code>)
120      * and to the enclosing lexical type (i.e,. the enclosing aspect).
121      */

122     /*
123     public boolean canAccess(IntroducedDec dec) { // todo: users
124         if (null == dec) return false;
125         if (!canAccess(dec.getModifiers(), dec)) {
126             return false;
127         } else {
128             return canAccess(dec.getLexicalType());
129         }
130     }
131     */

132
133     /** @return true if modifiers permitted for self and any enclosing type */
134     public boolean canAccess(TypeDec dec) {
135         if (null == dec) return false;
136         boolean result = canAccess(dec.getModifiers(), dec);
137         if (result) {
138             // avoiding NPE in getEnclosingInstanceTypeDec
139
NameType outerType = dec.getEnclosingInstanceType();
140             TypeDec outer = (null == outerType? null
141                              : outerType.getTypeDec()); // todo: typeDec?
142
result = ((null == outer) || canAccess(outer));
143         }
144         return result;
145     }
146
147     /**
148      * This is called from <code>canAccess</code> to log any results
149      * of <code>doCanAccess</code>
150      * and should return the result or a principled variant thereof.
151      */

152     protected boolean canAccessLog(Modifiers mods, Object JavaDoc object,
153                                     boolean result) {
154         return result;
155     }
156
157     /**
158      * Check whether client has access to the object
159      * based on the modifiers.
160      * @param object the Object with the modifier flags - may be null
161      * @param modifiers the Modifiers to check
162      * @return false if modifiers are null or true if access is permitted
163      */

164     // todo: object unused but useful for logging
165
public final boolean canAccess(Modifiers mods, Object JavaDoc object) {
166         boolean result = (null == mods? false : doCanAccess(mods, object));
167         return canAccessLog(mods, object, result);
168     }
169
170     /** @return lowercase variant of option name (e.g., "private" for -private) */
171     public final String JavaDoc getOption() { return optionName; }
172
173     /** @return UPPERCASE variant of option name (e.g., "PRIVATE" for -private) */
174     public final String JavaDoc toString() { return optionName.toUpperCase(); }
175
176     /** subclasses implement semantics here. */
177     abstract protected boolean doCanAccess(Modifiers mods, Object JavaDoc object);
178 }
179
Popular Tags