KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
28 import java.util.Enumeration JavaDoc;
29 import org.aspectj.compiler.base.*;
30 import org.aspectj.util.CollectionUtil;
31 import org.aspectj.compiler.base.parser.SourceInfo;
32
33 //XXX this doesn't belong here
34
import org.aspectj.compiler.crosscuts.joinpoints.*;
35 import org.aspectj.compiler.crosscuts.ast.*;
36
37
38 import java.util.*;
39 import java.lang.reflect.*;
40
41 public class World extends CompilerObject {
42     private List compilationUnits = new ArrayList();
43     private List files = new LinkedList();
44     private ArrayList codeDecs = new ArrayList();
45
46     //public final Modifiers EmptyModifiers = new Modifiers(source,0);
47

48     public final SourceLocation source = new DummySourceLocation(getCompiler());
49
50     public final CompilationUnit missingCompilationUnit =
51         new CompilationUnit(source, null, null, new Decs(source), new SourceInfo(null, null),true);
52
53     public final FieldDec FIELD_DEC_NOT_FOUND =
54         new FieldDec(source,new Modifiers(source,1),
55                 getTypeManager().anyType.makeTypeD(), "not$found", null);
56     public final FieldDec FIELD_NOT_FOUND = FIELD_DEC_NOT_FOUND;
57
58     public final VarDec VAR_DEC_NOT_FOUND =
59         new VarDec(source,getTypeManager().anyType.makeTypeD(), "not$found", null);
60
61     public final MethodDec METHOD_DEC_NOT_FOUND =
62         new MethodDec(source,new Modifiers(source, 0),getTypeManager().anyType.makeTypeD(),
63             "not$found", new Formals(source),
64                 new TypeDs(source), null);
65
66     public final ConstructorDec CONSTRUCTOR_DEC_NOT_FOUND =
67         new ConstructorDec(source,new Modifiers(source, 0), new Formals(source),
68                 new TypeDs(source), null);
69
70     public final PointcutDec POINTCUT_DEC_NOT_FOUND =
71         new PointcutDec(source,new Modifiers(source, 0), "NOT_FOUND", null, null, new EmptyPcd(source));
72     public final PointcutDec POINTCUT_NOT_FOUND = POINTCUT_DEC_NOT_FOUND;
73
74     public List getFiles() {
75       return files;
76     }
77
78     private class CompilationUnitEnumerator implements Enumeration JavaDoc {
79         int idx = 0;
80         public boolean hasMoreElements() { return idx < compilationUnits.size(); }
81         public Object JavaDoc nextElement() { return compilationUnits.get(idx++); }
82     }
83     private class FileEnumerator implements Enumeration JavaDoc {
84         int idx = 0;
85         public boolean hasMoreElements() { return idx < files.size(); }
86         public Object JavaDoc nextElement() { return files.get(idx++); }
87     }
88
89     //public static World getDefault() { return JavaCompiler.TheCompiler.getWorld(); }
90

91     // this function will be used for cleaning
92
// world before next compilation (in incremental mode)
93
public void cleanup() {
94         files = new LinkedList();
95         codeDecs = new ArrayList();
96         compilationUnits = new ArrayList();
97     }
98
99     public World(JavaCompiler compiler) {
100         super(compiler);
101     }
102
103 // public Collection getLoadedTypes() {
104
// return getTypeManager().getLoadedTypes();
105
// }
106

107     public Enumeration JavaDoc enumerateCompilationUnits() {
108         return new CompilationUnitEnumerator();
109     }
110
111     public List getCompilationUnits() {
112         return compilationUnits;
113     }
114
115     public void addCompilationUnit(CompilationUnit cu) {
116         compilationUnits.add(cu);
117     }
118
119     public Enumeration JavaDoc enumerateFiles() {
120         return new FileEnumerator();
121     }
122
123     public void addFile(String JavaDoc filename) {
124         if (files.contains(filename))
125             return;
126         files.add(filename);
127     }
128     
129     /** returns the next break-label.
130      */

131     public String JavaDoc genLabel() {
132         return "aj$label" + labelCount++;
133     }
134     private int labelCount = 0;
135     
136     /**
137      * returns all types passed to the compiler in compilation units
138      *
139      * does not include types loaded from the classpath
140      */

141     public List getTypes() {
142         ArrayList types = new ArrayList();
143         Enumeration JavaDoc en = enumerateCompilationUnits();
144         while (en.hasMoreElements()) {
145             CompilationUnit cu = (CompilationUnit)en.nextElement();
146             types.addAll(cu.getDefinedTypes());
147         }
148         return types;
149     }
150
151 // public TypeDec findTypeDec(String packageName, String className) {
152
// return getTypeManager().findTypeDec(packageName, className);
153
// }
154

155
156     private List sourceTypes = new LinkedList();
157
158     public void addSourceType(Type type) {
159         //System.out.println("adding source type: " + type.toShortString());
160
sourceTypes.add(type);
161         getTypeManager().addType(type);
162     }
163
164     public Iterator sourceTypesIterator() {
165         return new Iterator() {
166             int index = 0;
167             public boolean hasNext() { return index < sourceTypes.size(); }
168             public Object JavaDoc next() { return sourceTypes.get(index++); }
169             public void remove() { throw new RuntimeException JavaDoc("can't remove from this"); }
170         };
171 // return sourceTypes.iterator();
172
}
173
174
175     private LinkedList/*TypeDec*/ buildingTypeGraph = new LinkedList();
176     
177     public void pushBuildingTypeGraph(TypeDec td) {
178         buildingTypeGraph.addLast(td);
179         //System.out.println(buildingTypeGraph);
180
}
181     
182     public void popBuildingTypeGraph() {
183         buildingTypeGraph.removeLast();
184     }
185     
186     public LinkedList getBuildingTypeGraph() {
187         return buildingTypeGraph;
188     }
189
190
191     //static List typeDecPlanners;
192

193
194     // ASPECT from crosscut package
195
//static List typeDecPlanners;
196
private List jpPlanners = new LinkedList();
197
198     public void addJpPlanner(JpPlanner planner) {
199         if (planner.isStaticPlanner()) {
200             staticJpPlanners.add(new TopJpPlanner(planner));
201         } else {
202             jpPlanners.add(new TopJpPlanner(planner));
203         }
204     }
205
206     public List getJpPlanners() { return jpPlanners; }
207
208     private List staticJpPlanners = new LinkedList();
209
210     public List getStaticJpPlanners() { return staticJpPlanners; }
211
212     //XXX use insertion order for these planners
213
//XXX this is an approximation to the correct behavior
214
private Collection typePlanners = new LinkedList(); //new HashSet();
215

216     public void addTypeDecPlanner(TypeDecPlanner planner) {
217         if (!typePlanners.contains(planner)) typePlanners.add(planner);
218     }
219     
220     public boolean collectedTypeDecPlanners = false;
221
222     public void runTypeDecPlanners(TypeDec typeDec, int phase) {
223         //XXX don't like this restiction
224
if (!typeDec.fromSource()) return;
225
226         //XXX add error checking that each phase only runs once
227
for (Iterator i = typePlanners.iterator(); i.hasNext(); ) {
228             TypeDecPlanner planner = (TypeDecPlanner)i.next();
229
230             //System.out.println("running planner: " + planner + " on " + typeDec);
231
planner.plan(typeDec, phase);
232         }
233     }
234
235     public Map/*MethodDec, CalleeSideCallPoint*/ calleeSideCallPoints = new HashMap();
236
237     private Map sourceToOutput = new HashMap();
238     private Map outputToSource = new HashMap();
239
240     public Map getSourceToOutputLineMap() { return sourceToOutput; }
241     public Map getOutputToSourceLineMap() { return sourceToOutput; }
242
243     public void clearState() {
244         sourceToOutput = null;
245         outputToSource = null;
246     }
247 }
248
249
250
Popular Tags