KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > cst > CUScope


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler 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  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.cst;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.*;
29 import java.util.*;
30
31 /**
32  * The scope of a compilation unit, typically a single source file
33  */

34 public class CUScope extends Scope {
35     private CompilationUnit cu;
36     
37     public CUScope(JavaCompiler compiler, Scope parent, CompilationUnit cu) {
38         super(compiler, parent);
39         this.cu = cu;
40     }
41     public String JavaDoc shortToString() { return "CU(" + cu.getSourceFileName() + ")"; }
42     
43     public Expr bindUnqualifiedName(String JavaDoc name, ASTObject fromWhere) {
44         return null;
45     }
46
47     public Type findMethodLookupType(String JavaDoc name, ASTObject fromWhere) {
48         return null;
49     }
50     
51     HashMap nameToType = new HashMap();
52     public Type findType(String JavaDoc name, ASTObject fromWhere) {
53         Type type = (Type)nameToType.get(name);
54         if (type != null) return type;
55
56         type = internalFindType(name, fromWhere);
57         if (type != null) {
58             nameToType.put(name, type);
59         }
60         return type;
61     }
62     
63     protected boolean isPackageAccessible(TypeDec typeDec) {
64         String JavaDoc p1 = typeDec.getPackageName();
65         String JavaDoc p2 = cu.getPackageName();
66         if (p1 == null) return p2 == null;
67         else return p1.equals(p2);
68     }
69     
70     protected boolean checkAccessibility(TypeDec typeDec, ASTObject fromWhere) {
71         if (typeDec.isPublic()) return true;
72         if (typeDec.isPrivate()) return false;
73         if (typeDec.isProtected()) {
74             if (isPackageAccessible(typeDec)) return true;
75             return typeDec.isAccessible(fromWhere);
76         }
77         return isPackageAccessible(typeDec);
78     }
79
80         
81     
82     protected Type checkFoundType(String JavaDoc id, Type oldType, Type foundType, ASTObject fromWhere) {
83         if (foundType == null) return oldType;
84         if (foundType == getTypeManager().TYPE_NOT_FOUND) return oldType;
85         
86         if (!checkAccessibility(foundType.getTypeDec(), fromWhere)) return oldType;
87         
88         if (oldType != null && foundType != oldType) {
89             return handleAmbiguousImport(id, foundType, oldType, fromWhere);
90         } else {
91             return foundType;
92         }
93     }
94         
95
96     protected Type internalFindType(String JavaDoc id, ASTObject fromWhere) {
97         Type type = null;
98
99         //System.err.println("looking in source for "+id);
100

101         // first loop for single import names
102
Imports imports = cu.getImports();
103         if (imports != null) {
104             for (int i=0; i<imports.size(); i++) {
105                 Import _import = imports.get(i);
106                 if (!_import.getStar()) {
107                     if (_import.capturesId(id)) {
108                         return _import.getType();
109                     }
110                 }
111             }
112         }
113         
114         // check the current package for this name
115
String JavaDoc packageName = cu.getPackageName();
116         type = getTypeManager().findType(packageName, id);
117         if (type != null) return type;
118
119
120         //!!! This should raise an error if the name is found in more than
121
//!!! one import-on-demand package
122

123         // now loop for import-on-demand names
124
//Type foundOnDemandType = null;
125
if (imports != null) {
126             for (int i=0; i<imports.size(); i++) {
127                 Import _import = imports.get(i);
128                 if (_import.getStar()) {
129                     Object JavaDoc o = _import.getTypeName().resolve(_import.makeTypeNameScope(), false);
130                     Type tryType = null;
131                     if (o == null) continue;
132                     
133                     if (o instanceof String JavaDoc) {
134                         tryType = getTypeManager().findType((String JavaDoc)o, id);
135                     } else if (o instanceof Type) {
136                         tryType = ((Type)o).getInnerType(id, _import, false);
137                     }
138                     type = checkFoundType(id, type, tryType, fromWhere);
139                 }
140             }
141         }
142         
143         //if (type != null) return type;
144

145         // finally check in java.lang which is always import-on-demand
146
Type javaLangType = getTypeManager().findType("java.lang", id);
147         type = checkFoundType(id, type, javaLangType, fromWhere);
148
149         // finally, look for the name itself
150
//Type noPackageType = getTypeManager().findType(null, id);
151
//type = checkFoundType(id, type, noPackageType, fromWhere);
152

153         return type;
154     }
155     
156     protected Type handleAmbiguousImport(String JavaDoc id, Type type1, Type type2, ASTObject showWhere) {
157         getCompiler().showError(showWhere, "ambiguous type reference, could be either " +
158                         type1.getString() + " or " + type2.getString());
159         return null;
160     }
161 }
162
Popular Tags