KickJava   Java API By Example, From Geeks To Geeks.

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


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.parser.SourceInfo;
28
29 import org.aspectj.compiler.base.*;
30 import org.aspectj.compiler.base.cst.CUScope;
31
32 import java.util.*;
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35
36 /**
37   * @grammar package imports decs
38   * @property String packageName
39   * @child Imports imports
40   * @child Decs decs
41   * @property SourceInfo sourceInfo
42   * @property boolean scanned
43   */

44 public class CompilationUnit extends ASTObject {
45
46     private CUScope scope = new CUScope(getCompiler(), null, this);
47
48     private ArrayList ownDecs = new ArrayList();
49
50 // private class CUSourceLocation extends SourceLocation {
51
// private JavaCompiler compiler;
52
// public CUSourceLocation(JavaCompiler compiler) {
53
// super(null, 0, 0);
54
// this.compiler = compiler;
55
// }
56
//
57
// public CompilationUnit getCompilationUnit() { return CompilationUnit.this; }
58
// public JavaCompiler getCompiler() { return compiler; }
59
// }
60

61     // An empty compilation unit constructor
62
public CompilationUnit(JavaCompiler compiler, File JavaDoc _file) {
63         this(new DummySourceLocation(compiler), null, null, null, new SourceInfo(_file, null), false);
64         sourceLocation = new TextSourceLocation(this, 0, 0);
65         setDecs(new Decs(getSourceLocation()));
66     }
67
68     public List getDefinedTypes() {
69         return ownDecs;
70     }
71
72     public void addDefinedType(TypeDec dec) {
73         //System.out.println("adding " + dec + " to " + this);
74
ownDecs.add(dec);
75     }
76
77
78     public Type getDeclaringType() {
79         return null;
80     }
81     public Type getLexicalType() {
82         return null;
83     }
84
85     public TypeDec getBytecodeTypeDec() {
86         return null;
87     }
88
89     public CodeDec getEnclosingCodeDec() {
90         return null;
91     }
92
93     public CUScope getScope() {
94         return scope;
95     }
96
97     //INTRO from ScopePass
98
public void preScope(ScopeWalker walker) { walker.enterCU(this); }
99     //???public ASTObject postScope(ScopeWalker walker) { walker.popBlock(); return this; }
100

101 //
102
// class ImplementationCleanupWalker extends ASTWalker {
103
// ImplementationCleanupWalker(JavaCompiler compiler) {
104
// super(compiler);
105
// }
106
// public ASTObject walk0(ASTObject obj) {
107
// if (obj == null)
108
// return obj;
109
// obj.clearComment();
110
// if (obj instanceof CodeDec) {
111
// obj.cleanup();
112
// }
113
// else if (obj instanceof CodeBody) {
114
// obj.cleanup();
115
// return obj;
116
// }
117
// else if (obj instanceof ArrayInitializer) {
118
// obj.cleanup();
119
// return obj;
120
// }
121
// super.walk1(obj);
122
// return obj;
123
// }
124
// }
125
//
126
public String JavaDoc toString() { return getSourceFile().toString(); }
127
128     // cleanup implementations
129
public void cleanup() {
130 // ImplementationCleanupWalker w = new ImplementationCleanupWalker(getCompiler());
131
// this.walk(w);
132
}
133     public void cleanupDefinedTypes() {
134         ownDecs = new ArrayList();
135     }
136
137     private String JavaDoc sourceCanonicalPath = null;
138     public String JavaDoc getSourceCanonicalPath() {
139         if (sourceCanonicalPath != null) return sourceCanonicalPath;
140         File JavaDoc file = getSourceFile();
141         if (file == null) return null;
142
143         try {
144             sourceCanonicalPath = file.getCanonicalPath();
145         } catch (IOException JavaDoc ioe) {}
146
147         return sourceCanonicalPath;
148     }
149
150     private String JavaDoc sourceDirectory = null;
151     public String JavaDoc getSourceDirectory() {
152         if (sourceDirectory != null) return sourceDirectory;
153
154         sourceDirectory = new File JavaDoc(getSourceCanonicalPath()).getParent();
155
156         return sourceDirectory;
157     }
158
159
160     public File JavaDoc getSourceFile() { return sourceInfo.getFile(); }
161     public int getLine(int position) { if (sourceInfo == null) return -1; return sourceInfo.getLine(position); }
162     public int getColumn(int position) { return sourceInfo.getColumn(position); }
163
164     public CompilationUnit getCompilationUnit() {
165         return this;
166     }
167
168     public void addTypesToTypeGraph() {
169         ScopeWalker walker = new ScopeWalker(getCompiler(), getScope());
170         Decs decs = getDecs();
171         for (int i=0, N=decs.size(); i<N; i++) {
172             TypeDec typeDec = (TypeDec)decs.get(i);
173             typeDec.addToTypeGraph(walker);
174         }
175     }
176
177     public void buildSignatures() {
178         ScopeWalker walker = new ScopeWalker(getCompiler(), getScope());
179         Decs decs = getDecs();
180         for (int i=0, N=decs.size(); i<N; i++) {
181             TypeDec typeDec = (TypeDec)decs.get(i);
182             typeDec.buildSignatures(walker);
183         }
184     }
185
186
187     public void checkSpec() {
188         Imports imports = getImports();
189         Map importedTypeNames = new HashMap();
190         for (int i=0,N=imports.size(); i<N; i++) {
191             Import imp = imports.get(i);
192             if (!imp.getStar()) {
193                 Type t = imp.getType();
194                 // check to see if there's a type in this cu
195
Decs decs = getDecs();
196                 for (int j=0, M=decs.size(); j<M; j++) {
197                     TypeDec typeDec = (TypeDec)decs.get(j);
198                     String JavaDoc id = typeDec.getId();
199                     if (t.getId().equals(id) && !(t == typeDec.getType())) {
200                         imp.showError("import conflicts with definition of " +
201                                        typeDec.getPrettyString());
202                     }
203                 }
204                 Type iType = (Type)importedTypeNames.get(t.getId());
205                 if (iType != null && iType != t) {
206                     imp.showError("import conflicts with previous import of " +
207                                   iType.getPrettyString());
208                 }
209                 importedTypeNames.put(t.getId(), t);
210             }
211         }
212         
213         
214         // in strict mode, check that public types are in source files of same name
215
if (getOptions().strict && sourceInfo != null) {
216         Decs decs = getDecs();
217             for (int i=0, N=decs.size(); i<N; i++) {
218                 TypeDec typeDec = (TypeDec)decs.get(i);
219                 if (typeDec.isPublic()) {
220                     String JavaDoc id = typeDec.getId();
221                     String JavaDoc filename = sourceInfo.getFile().getName();
222                     //XXX lazy string matching
223
if (!filename.startsWith(id)) {
224                         this.showError("public class must be in file of same name, i.e " +
225                                        id + ".java");
226                     }
227                 }
228             }
229         }
230     }
231
232
233 // public String getPackageName() {
234
// return _package == null ? null : _package.getName();
235
// }
236

237     public void unparse(CodeWriter writer) {
238         if (getPackageName() != null) {
239             writer.writeKeyword("package");
240             writer.requiredSpace();
241             writer.write(getPackageName());
242             writer.closeStmt();
243         }
244
245         writer.writeChildren(imports);
246         writer.write(decs);
247     }
248
249     // ------------------------------
250
// bcg
251
public final void generateBytecode(File JavaDoc outputDir) throws IOException JavaDoc {
252         for (Iterator i = getDefinedTypes().iterator(); i.hasNext(); ) {
253             TypeDec typeDec = (TypeDec) i.next();
254             typeDec.generateBytecode(outputDir);
255         }
256     }
257
258     //BEGIN: Generated from @child and @property
259
protected String JavaDoc packageName;
260     public String JavaDoc getPackageName() { return packageName; }
261     public void setPackageName(String JavaDoc _packageName) { packageName = _packageName; }
262
263     protected Imports imports;
264     public Imports getImports() { return imports; }
265     public void setImports(Imports _imports) {
266         if (_imports != null) _imports.setParent(this);
267         imports = _imports;
268     }
269
270     protected Decs decs;
271     public Decs getDecs() { return decs; }
272     public void setDecs(Decs _decs) {
273         if (_decs != null) _decs.setParent(this);
274         decs = _decs;
275     }
276
277     protected SourceInfo sourceInfo;
278     public SourceInfo getSourceInfo() { return sourceInfo; }
279     public void setSourceInfo(SourceInfo _sourceInfo) { sourceInfo = _sourceInfo; }
280
281     protected boolean scanned;
282     public boolean getScanned() { return scanned; }
283     public void setScanned(boolean _scanned) { scanned = _scanned; }
284
285     public CompilationUnit(SourceLocation location, String JavaDoc _packageName, Imports _imports, Decs _decs, SourceInfo _sourceInfo, boolean _scanned) {
286         super(location);
287         setPackageName(_packageName);
288         setImports(_imports);
289         setDecs(_decs);
290         setSourceInfo(_sourceInfo);
291         setScanned(_scanned);
292     }
293     protected CompilationUnit(SourceLocation source) {
294         super(source);
295     }
296
297     public ASTObject copyWalk(CopyWalker walker) {
298         CompilationUnit ret = new CompilationUnit(getSourceLocation());
299         ret.preCopy(walker, this);
300         ret.packageName = packageName;
301         if (imports != null) ret.setImports( (Imports)walker.process(imports) );
302         if (decs != null) ret.setDecs( (Decs)walker.process(decs) );
303         ret.sourceInfo = sourceInfo;
304         ret.scanned = scanned;
305         return ret;
306     }
307
308     public ASTObject getChildAt(int childIndex) {
309         switch(childIndex) {
310         case 0: return imports;
311         case 1: return decs;
312         default: return super.getChildAt(childIndex);
313         }
314     }
315      public String JavaDoc getChildNameAt(int childIndex) {
316         switch(childIndex) {
317         case 0: return "imports";
318         case 1: return "decs";
319         default: return super.getChildNameAt(childIndex);
320         }
321     }
322      public void setChildAt(int childIndex, ASTObject child) {
323         switch(childIndex) {
324         case 0: setImports((Imports)child); return;
325         case 1: setDecs((Decs)child); return;
326         default: super.setChildAt(childIndex, child); return;
327         }
328     }
329      public int getChildCount() {
330         return 2;
331     }
332
333     public String JavaDoc getDefaultDisplayName() {
334         return "CompilationUnit(packageName: "+packageName+", "+"sourceInfo: "+sourceInfo+", "+"scanned: "+scanned+")";
335     }
336
337     //END: Generated from @child and @property
338
}
339
340
341
342
Popular Tags