KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > PackageSO


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.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.cst.*;
29 import org.aspectj.compiler.base.bytecode.ClassManager;
30
31 import java.util.*;
32
33 /**
34  * Represents a single package
35  * Almost certainly should extends SemanticObject
36  */

37
38 public class PackageSO extends CompilerObject {
39     private String JavaDoc packageName;
40     private Map typeCache = new HashMap();
41     private Map childPackages = new HashMap();
42
43     public ClassManager classManager;
44
45     public PackageSO(JavaCompiler compiler, String JavaDoc packageName,
46                    ClassManager classManager) {
47         super(compiler);
48         this.packageName = packageName;
49         this.classManager = classManager;
50     }
51
52     public void addType(Type newType) {
53         String JavaDoc className = newType.getTypeDec().getExtendedId();
54         
55         Type type = (Type)typeCache.get(className);
56         if (type != null && type != newType) {
57             type.getTypeDec().showError("duplicate type name not allowed: " +
58                                         type.getString());
59             newType.getTypeDec().showError("duplicate type name not allowed: " +
60                                            type.getString());
61         }
62         if (childPackages.containsKey(className)) {
63             newType.getTypeDec().showError(newType.getString() +
64                                            " clashes with package of the same name");
65             return;
66         }
67         typeCache.put(className, newType);
68     }
69     
70     private static final Type NOT_FOUND = new NullType(null);
71             
72     public Type findType(String JavaDoc className) {
73         Type type = (Type)typeCache.get(className);
74         if (type != null) {
75             if (type == NOT_FOUND) return null;
76             else return type;
77         }
78         
79         type = findTypeOnClassPath(className); //makeName(packageName, className));
80

81         if (type != null) {
82             typeCache.put(className, type);
83             ((NameType)type).setLazy();
84             return type;
85         } else {
86             typeCache.put(className, NOT_FOUND);
87             return null;
88         }
89     }
90
91     public boolean containsSubPackage(String JavaDoc packageName) {
92         PackageSO p = (PackageSO)childPackages.get(packageName);
93         if (p == null) return false;
94         return p.size() > 0;
95     }
96
97     public PackageSO findPackage(String JavaDoc packageName) {
98         //???
99
//if (findType(packageName) != null) return null;
100

101         PackageSO p = (PackageSO)childPackages.get(packageName);
102         if (p != null) return p;
103
104 // getCompiler().showMessage("making sub package: " +
105
// makeName(this.packageName, packageName));
106
p = new PackageSO(getCompiler(), makeName(this.packageName, packageName),
107                         classManager.makeSubPackageManager(packageName));
108         //getCompiler().showMessage("made sub package: " + p.packageName);
109
//Thread.currentThread().dumpStack();
110
childPackages.put(packageName, p);
111         return p;
112     }
113
114     public int size() { return typeCache.size(); }
115         
116     private final String JavaDoc makeName(String JavaDoc n1, String JavaDoc n2) {
117         if (n1 == null) return n2;
118         return n1 + '.' + n2;
119     }
120
121     //!!! made public for use by TypeManager.checkLoadable
122
public Type findTypeOnClassPath(String JavaDoc name) {
123         //System.out.println("finding " + name);
124
//System.out.println(packageName + ", " + name + ", " + classManager);
125
TypeDec ret = classManager.findTypeDec(name);
126         if (ret == null) return null;
127
128         // make sure that the type's name matches the name that was searched for
129
// this is to catch case errors on Windows
130
if (!(ret.getFullName().equals(makeName(packageName, name)))) {
131             getCompiler().showMessage(" case mismatch, not loading: " + ret.getFullName());
132             return null;
133         }
134         return ret.getType();
135     }
136 }
137
Popular Tags