KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > TypeManager


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;
26
27 import org.aspectj.compiler.base.ast.*;
28 import org.aspectj.compiler.base.bytecode.*;
29
30 import java.util.*;
31 import java.io.*;
32
33 public final class TypeManager extends CompilerObject {
34     protected SourceLocation dummySource = new DummySourceLocation(getCompiler());
35     public final TypeDec TYPE_DEC_NOT_FOUND = new ClassDec(dummySource,
36         new Modifiers(dummySource,0),
37         "*NF*",null, null, new Decs(dummySource));
38     
39     public final NameType TYPE_NOT_FOUND = new NotFoundType(TYPE_DEC_NOT_FOUND);
40     {
41         TYPE_DEC_NOT_FOUND.setType(TYPE_NOT_FOUND);
42         //((NameType)TYPE_NOT_FOUND).builtTypeGraph = true;
43
//((NameType)TYPE_NOT_FOUND).isFinished = true;
44
}
45     
46     private ClassPathManager classPathManager;
47     private Map packages = new HashMap();
48     private PackageSO defaultPackage;
49     private Collection loadedTypes = new HashSet();
50     
51     public TypeManager(JavaCompiler compiler) {
52         super(compiler);
53         Options options = compiler.getOptions();
54         if (options.classpath == null) options.classpath = options.defaultClasspath;
55         classPathManager =
56           new ClassPathManager(compiler,
57                                options.classpath, options.bootclasspath,
58                                options.extdirs);
59         defaultPackage = new PackageSO(compiler, null, classPathManager);
60         //astConnection = new ASTConnection(compiler);
61
}
62     
63     private Collection allVisibleTypeNames = null;
64     
65     /** return all type names that are visible, meaning passed in, or on the classpath.
66      *
67      * This returns a name twice if shadowing occurs on the classpath.
68      * This must only be called after {@link World#getTypes()}.
69      */

70     public Collection getAllVisibleTypeNames() {
71         if (allVisibleTypeNames == null) {
72             allVisibleTypeNames = classPathManager.getAllPossibleTypeNames();
73             for (Iterator i = getWorld().getTypes().iterator(); i.hasNext();) {
74                 TypeDec t = (TypeDec) i.next();
75                 allVisibleTypeNames.add(t.getType().getNamePieces());
76             }
77             for (Iterator i = getPrimitiveTypeMap().values().iterator(); i.hasNext(); ) {
78                 Type t = (Type)i.next();
79                 allVisibleTypeNames.add(t.getNamePieces());
80             }
81         }
82         return allVisibleTypeNames;
83     }
84     
85     /** returns a string representation of each element on the classpath.
86      * useful for diagnostic error messages
87      */

88     public Collection getClassPathStrings() {
89         return classPathManager.getPathStrings();
90     }
91     
92     /**
93      * returns all Types that have been loaded by this TypeManager
94      */

95     public Collection getLoadedTypes() {
96         return loadedTypes;
97     }
98     
99         
100     public Type getType(String JavaDoc packageName, String JavaDoc className) {
101         Type type = findType(packageName, className);
102         if (type == null) {
103             getCompiler().showError("missing type " + packageName + '.' + className);
104             return TYPE_NOT_FOUND;
105         } else {
106             return type;
107         }
108     }
109
110     public synchronized Type findType(String JavaDoc packageName, String JavaDoc className) {
111         //System.out.println("finding " + packageName + "::" + className);
112
PackageSO p = findPackage(packageName);
113         Type ret = p.findType(className);
114         if (ret != null) loadedTypes.add(ret);
115         //if (ret == null) { System.out.println(" NOT FOUND"); }
116
return ret;
117     }
118     
119     public void addType(Type type) {
120         //System.out.println("adding " + type.getPackageName() + "::" + type.getId());
121

122         PackageSO p = findPackage(type.getPackageName());
123         p.addType(type);
124         loadedTypes.add(type);
125     }
126
127     PackageSO findTopLevelPackage(String JavaDoc name) {
128         PackageSO p = (PackageSO)packages.get(name);
129         if (p != null) return p;
130
131         p = new PackageSO(getCompiler(), name,
132                         classPathManager.makeSubPackageManager(name));
133         //getCompiler().showMessage("made top package: " + name);
134
// System.out.println("sub: " + classPathManager + ", " +
135
// classPathManager.makeSubPackageManager(name));
136
packages.put(name, p);
137         return p;
138     }
139     
140     public PackageSO findPackage(String JavaDoc packageName) {
141         if (packageName == null) return defaultPackage;
142
143         PackageSO p = (PackageSO)packages.get(packageName);
144         if (p != null) return p;
145         
146         String JavaDoc[] names = splitDottedName(packageName);
147         p = findTopLevelPackage(names[0]);
148         for (int i=1; i<names.length; i++) {
149             //System.out.println(p + ": " + names[i]);
150
p = p.findPackage(names[i]);
151         }
152         //getCompiler().showMessage("adding " + packageName);
153

154         packages.put(packageName, p);
155         return p;
156     }
157     
158     String JavaDoc[] splitDottedName(String JavaDoc name) {
159         //getCompiler().showMessage("splitting: " + name);
160
int nameCount = 0;
161         int lastDot=-1;
162         do {
163             lastDot = name.indexOf('.', lastDot+1);
164             nameCount++;
165         } while (lastDot != -1);
166
167         String JavaDoc[] names = new String JavaDoc[nameCount];
168         lastDot = 0;
169         int index=0;
170         while (true) {
171             int nextDot = name.indexOf('.', lastDot);
172             if (nextDot == -1) {
173                 names[index] = name.substring(lastDot);
174                 break;
175             } else {
176                 names[index++] = name.substring(lastDot, nextDot);
177                 lastDot = nextDot+1;
178             }
179         }
180
181         return names;
182     }
183     
184     /** Check that specified type exists in classpath and is loadable */
185     public boolean checkLoadable(String JavaDoc packageName, String JavaDoc className) {
186         PackageSO p = findPackage(packageName);
187         return p.findTypeOnClassPath(className) != null;
188     }
189
190     
191     private final Map primitiveTypeMap = new HashMap();
192     
193     public final Type nullType = new NullType(getCompiler());
194     public final Type voidType = new VoidType(getCompiler());
195     { primitiveTypeMap.put("void", voidType); }
196     public final Type anyType = TYPE_NOT_FOUND;// new AnyType(getCompiler());
197

198     public final PrimitiveType intType = new IntType(getCompiler());
199     { primitiveTypeMap.put("int", intType); }
200     public final PrimitiveType byteType = new ByteType(getCompiler());
201     { primitiveTypeMap.put("byte", byteType); }
202     public final PrimitiveType shortType = new ShortType(getCompiler());
203     { primitiveTypeMap.put("short", shortType); }
204     public final PrimitiveType longType = new LongType(getCompiler());
205     { primitiveTypeMap.put("long", longType); }
206     public final PrimitiveType doubleType = new DoubleType(getCompiler());
207     { primitiveTypeMap.put("double", doubleType); }
208     public final PrimitiveType floatType = new FloatType(getCompiler());
209     { primitiveTypeMap.put("float", floatType); }
210     public final PrimitiveType charType = new CharType(getCompiler());
211     { primitiveTypeMap.put("char", charType); }
212     public final PrimitiveType booleanType = new BooleanType(getCompiler());
213     { primitiveTypeMap.put("boolean", booleanType); }
214
215
216     public Type findPrimitiveType(String JavaDoc id) {
217         return (Type)getPrimitiveTypeMap().get(id);
218     }
219
220     public Map getPrimitiveTypeMap() {
221         return primitiveTypeMap;
222     }
223     
224
225     public final NameType getObjectType() {
226         return (NameType)getType("java.lang", "Object");
227     }
228     public final NameType getStringType() {
229         return (NameType)getType("java.lang", "String");
230     }
231     public final NameType getClassType() {
232         return (NameType)getType("java.lang", "Class");
233     }
234     public final NameType getCloneableType() {
235         return (NameType)getType("java.lang", "Cloneable");
236     }
237     public final NameType getSerializableType() {
238         return (NameType)getType("java.io", "Serializable");
239     }
240     public final NameType getRuntimeExceptionType() {
241         return (NameType)getType("java.lang", "RuntimeException");
242     }
243     public final NameType getErrorType() {
244         return (NameType)getType("java.lang", "Error");
245     }
246     public final NameType getThrowableType() {
247         return (NameType)getType("java.lang", "Throwable");
248     }
249     public final NameType getExceptionType() {
250         return (NameType)getType("java.lang", "Exception");
251     }
252     public final NameType getStringBufferType() {
253         return (NameType)getType("java.lang", "StringBuffer");
254     }
255
256     //ASPECT introduce from crosscuts
257
public final Type getJoinPointType() {
258         return getType("org.aspectj.lang", "JoinPoint");
259     }
260     public final Type getJoinPointStaticPartType() {
261         return getType("org.aspectj.lang", "JoinPoint$StaticPart");
262     }
263     public final Type getAroundClosureType() {
264         return getType("org.aspectj.runtime.internal", "AroundClosure");
265     }
266     public final Type getConversionsType() {
267         return getType("org.aspectj.runtime.internal", "Conversions");
268     }
269
270 // public final NameType objectType = new NameType(getCompiler(), null);
271
// public final NameType stringType = new NameType(getCompiler(), null);
272
// public final NameType cloneableType = new NameType(getCompiler(), null);
273
// public final NameType runtimeExceptionType = new NameType(getCompiler(), null);
274
// public final NameType errorType = new NameType(getCompiler(), null);
275

276     
277     /**
278      * Get bytecode for predefined types, except those were compiled
279      */

280 // public void fixPredefinedTypes() {
281
// fixPredefinedType("java.lang.Object");
282
// fixPredefinedType("java.io.Serializable");
283
// fixPredefinedType("java.lang.Comparable");
284
// fixPredefinedType("java.lang.String");
285
// fixPredefinedType("java.lang.Cloneable");
286
// fixPredefinedType("java.lang.Throwable");
287
// fixPredefinedType("java.lang.Exception");
288
// fixPredefinedType("java.lang.RuntimeException");
289
// fixPredefinedType("java.lang.Error");
290
// }
291
//
292
// private void fixPredefinedType(String fullName) {
293
// TypeDec typeDec = (TypeDec)typeDecCache.get(fullName);
294
// if (typeDec.getBody() == null) {
295
// internalFindTypeDec(fullName);
296
// typeDec.rebuildTypeGraph();
297
// }
298
// }
299

300     public Type unaryNumericPromotion(Type ty) {
301         if (ty == doubleType || ty == floatType || ty == longType) {
302             return ty;
303         }
304         return intType;
305     }
306
307     public Type binaryNumericPromotion(Type ty1, Type ty2) {
308         if (ty1 == doubleType || ty2 == doubleType) {
309             return doubleType;
310         }
311         if (ty1 == floatType || ty2 == floatType) {
312             return floatType;
313         }
314         if (ty1 == longType || ty2 == longType) {
315             return longType;
316         }
317         return intType;
318     }
319 }
320
321
Popular Tags