KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > AspectJCompiler


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.crosscuts;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.base.parser.*;
29 import org.aspectj.compiler.base.ast.*;
30 import org.aspectj.compiler.base.cst.*;
31
32 import java.util.*;
33 import java.io.*;
34
35 public class AspectJCompiler extends JavaCompiler {
36 // public CompilationUnit newCompilationUnit(File _file) {
37
// return new CompilationUnit(this,_file);
38
// }
39

40     public AspectJCompiler(ErrorHandler errorHandler) {
41         super(errorHandler);
42         this.scanner = AspectJParser.class;
43         //addPasses();
44
}
45
46     public AspectJCompiler() {
47         this(null);
48     }
49
50     public JavaParser makeJavaParser() {
51         return new AspectJParser(this);
52     }
53
54     //protected List/*CompilerPass*/ passes = null;
55
public void addPass(CompilerPass pass) { addPass(pass, false); }
56     public void addPass(CompilerPass pass, boolean exitOnErrors) {
57         passes.add(pass);
58         totalWorkEstimate += pass.getWorkEstimate();
59         //if (getOptions().torture) passes.add(new TreeCheckerPass(...));
60
if (exitOnErrors) passes.add(new ExitOnErrorsPass(this));
61     }
62
63     protected void addPreSymbolPasses() {
64         // This allows ajdoc to print out the files, classes, and
65
// packages being documented
66
addPass(createParserPass(), true);
67         addPass(new TypeGraphBuilderPass(getCompiler()));
68         //addPass(new IntroductionPlannerPass(getCompiler()));
69
addPass(new SignatureBinderPass(getCompiler()));
70         addPass(new BodyBinderPass(getCompiler()));
71         addPass(new ForwardReferenceChecker(getCompiler()));
72         addPass(new InnerInfoPass(getCompiler()));
73         addPass(new AssignmentCheckerPass(getCompiler()));
74         addPass(new SpecChecker(getCompiler()), true);
75
76         addPass(new BuildAdvicePlannersPass(getCompiler()), true);
77         addPass(new InitializerCollector(getCompiler(), false));
78
79         addPass(new JoinPointCollectorPass(getCompiler()), true);
80         // this planner also softens exceptions, which is why it's
81
// before flow analysis.
82
addPass(new StaticJpPlannerPass(getCompiler()));
83
84         addPass(new FlowCheckerPass(getCompiler()));
85         addPass(new ThrowCheckerPass(getCompiler()));
86
87         addPass(new AdvicePlannerPass(getCompiler()), true);
88
89         addPass(new AdviceWeaver(getCompiler()), true);
90     }
91
92     protected void addPostSymbolPasses() {
93 // addPass(new SymbolWriter(getCompiler()));
94

95         addPass(new AbstractCompilerPass(getCompiler()) {
96             public String JavaDoc getDisplayName() {
97                 return "emacssym ";
98             }
99             public void transformWorld() {
100                 if (super.getCompiler().getOptions().emacssym) {
101                     new org.aspectj.asm.views.EmacsViewManager().externalizeStructureView((AspectJCompiler)super.getCompiler());
102                 }
103             }
104         });
105
106         //XXX there is an assumption in the implementation of introduction that this
107
//XXX pass is run in a topo-sorted order. obviously that's not true yet.
108
addPass(new MixinImplementationPass(getCompiler()));
109
110         // move these after SymbolWriter because they can change the symbols
111
addPass(new PrivilegeFixer(getCompiler(), true));
112         addPass(new ASTFixerPass(getCompiler()), true);
113
114         addPass(new InitializerCollector(getCompiler(), true));
115
116         addPass(new LocalClassPass(getCompiler()), true);
117     }
118
119     protected void addSourceGenerationPasses() {
120         addPass(new NameHygienePass(getCompiler()), true);
121         addPass(new CodeGenerator(getCompiler()), true);
122         // runs javac on generated sources
123
addPass(new RunJavacPass(getCompiler()));
124         //new CodeGenerator(getCompiler()).transformWorld();
125
}
126
127     protected void addBytecodeGenerationPasses() {
128         addPass(new MemberClassMunger(getCompiler()));
129
130         if (getOptions().bcgverbose) {
131             // XXX DANGER: unsets FINAL, somehow...
132
addPass(new CodeGenerator(getCompiler()));
133         }
134
135         addPass(new ClassExprPass(getCompiler()));
136         addPass(new InnerAccessFixer(getCompiler()));
137
138
139         addPass(new NameHygienePass(getCompiler()), true);
140
141         // XXX this pass ELIMINATES some statements, which will
142
// mess up the control pass when we start checking for
143
// checked exceptions.
144
addPass(new ByteCodeCleanupPass(getCompiler()));
145         addPass(new FrameLocPass(getCompiler()));
146
147         addPass(new ByteCodeGenerator(getCompiler()));
148
149         // Note that we're not writing line maps. We're going to hell.
150
}
151
152     protected void addPasses() {
153         passes = new ArrayList();
154         addPreSymbolPasses();
155         addPostSymbolPasses();
156         if (getOptions().usejavac || getOptions().preprocess) {
157             addSourceGenerationPasses();
158         } else {
159             addBytecodeGenerationPasses();
160         }
161     }
162
163
164     protected JavaCompiler getCompiler() { return this; }
165
166     /**
167      * Override this to return true for compilers that don't want to generate code.
168      */

169     //protected boolean stopAfterWeaving() { return false; }
170

171
172     private Correspondences correspondences = new Correspondences(this);
173     public Correspondences getCorrespondences() {
174         return correspondences;
175     }
176
177     public String JavaDoc getBuildConfigFile() { return null; }
178
179     protected class ExitOnErrorsPass extends AbstractCompilerPass {
180         public ExitOnErrorsPass(JavaCompiler jc) { super(jc); }
181
182         public String JavaDoc getDisplayName() { return null; }
183         public double getWorkEstimate() { return 0.0; }
184
185         public void transformWorld() {
186             errorHandler.exitOnErrors();
187         }
188     }
189
190     protected AbstractCompilerPass createParserPass() {
191         return new ParserPass(getCompiler());
192     }
193
194     protected static class ParserPass extends AbstractCompilerPass {
195         public ParserPass(JavaCompiler jc) { super(jc); }
196
197         public String JavaDoc getDisplayName() { return "parse sources"; }
198         public double getWorkEstimate() { return 4.0; }
199
200         public void transform(CompilationUnit cu) {
201             this.getCompiler().showMessage(" parsing " + cu.getSourceFile());
202             try {
203                 JavaParser parser = this.getCompiler().allocateParser();
204                 parser.parseInterfaceOnly = false;
205                 parser.interfaceParse(cu);
206                 this.getCompiler().freeParser(parser);
207             } catch (ParseException pe) {
208                 if (getOptions().dumpstack) {
209                     pe.printStackTrace();
210                 }
211                 this.getCompiler().showError(cu.getSourceFile(),
212                                         pe.getLine(),
213                                         pe.getColumn(),
214                                         pe.getMessage());
215             } catch (IOException e) {
216                 this.getCompiler().showError(null,e.toString());
217             }
218
219             for (Iterator j = cu.getDefinedTypes().iterator(); j.hasNext(); ) {
220                 TypeDec td = (TypeDec)j.next();
221                 getWorld().addSourceType(td.getType());
222                 addInnerTypes(td); //, td.getId());
223
}
224         }
225
226         private void addInnerTypes(TypeDec typeDec) { //, String id) {
227
Decs body = typeDec.getBody();
228             for (int i=0; i < body.size(); i++) {
229                 if (body.get(i) instanceof TypeDec) {
230                     TypeDec innerTypeDec = (TypeDec)body.get(i);
231 // String innerId = id + '$' + innerTypeDec.getId();
232
// innerTypeDec.setExtendedId(innerId);
233
getTypeManager().addType( innerTypeDec.getType() );
234                     addInnerTypes(innerTypeDec); //, innerId);
235
}
236             }
237         }
238     }
239
240     protected static class TypeGraphBuilderPass extends AbstractCompilerPass {
241         public TypeGraphBuilderPass(JavaCompiler jc) { super(jc); }
242
243         public String JavaDoc getDisplayName() { return "build type graph"; }
244         public void transform(CompilationUnit cu) {
245             cu.addTypesToTypeGraph();
246         }
247     }
248
249     protected static class SignatureBinderPass extends WalkerPass {
250         public SignatureBinderPass(JavaCompiler jc) { super(jc); }
251
252         public String JavaDoc getDisplayName() { return "resolve type signatures"; }
253
254         public void transform(CompilationUnit cu) {
255             cu.buildSignatures();
256         }
257         /*
258          * @see CompilerPass#transformWorld()
259          */

260         public void transformWorld() {
261             super.transformWorld();
262             getWorld().collectedTypeDecPlanners = true;
263         }
264     }
265
266     protected static class BodyBinderPass extends AbstractCompilerPass {
267         public BodyBinderPass(JavaCompiler jc) { super(jc); }
268
269         public String JavaDoc getDisplayName() { return "bind names in bodies"; }
270         public void transform(CompilationUnit cu) {
271             new ScopeWalker(this.getCompiler()).process(cu);
272         }
273     }
274
275     protected static class JoinPointCollectorPass extends AbstractCompilerPass {
276         public JoinPointCollectorPass(JavaCompiler jc) { super(jc); }
277
278         public String JavaDoc getDisplayName() { return "collect join point shadows"; }
279         public void transform(TypeDec td) {
280             new JoinPointCollector(td).collect();
281         }
282     }
283
284     protected static class ThrowCheckerPass extends WalkerPass {
285         public ThrowCheckerPass(JavaCompiler jc) { super(jc); }
286
287         public String JavaDoc getDisplayName() { return "checking throws"; }
288         public ASTObject process(ASTObject node) {
289
290             if (node instanceof CodeDec) {
291                 CanThrowWalker.checkThrows((CodeDec)node);
292             }
293             return super.process(node);
294         }
295     }
296 }
297
298
Popular Tags