KickJava   Java API By Example, From Geeks To Geeks.

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


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.ast.*;
29 import org.aspectj.compiler.crosscuts.ast.*;
30 import org.aspectj.util.CollectionUtil;
31
32 import org.aspectj.tools.ide.Declaration;
33 import org.aspectj.tools.ide.SymbolManager;
34
35 import org.aspectj.compiler.base.JavaCompiler;
36 import org.aspectj.compiler.base.CompilerObject;
37
38
39
40 import java.io.*;
41 //import java.io.Writer;
42
//import java.io.File;
43
//import java.io.FileWriter;
44
//import java.io.BufferedWriter;
45
//import java.io.IOException;
46

47
48 import java.util.*;
49
50 // Make sure that references to Vector use the standard version
51
// This lets us generate serialized files which interoperate with other code better
52
import java.util.Vector JavaDoc;
53
54
55 // ultimate goal is probably to do dependency tracking here to make enviro
56
// much nicer to use...
57

58 class CompilationUnitCorrespondences extends CompilerObject {
59     // a map of cu's that this one depends on, where each entry in the map
60
// is the ast node that causes the depend
61
// this should allow sophisticated dependency checking sometime in the future
62
private CompilationUnit compilationUnit;
63     public CompilationUnitCorrespondences(CompilationUnit cu) {
64         super(cu.getCompiler());
65         compilationUnit = cu;
66     }
67
68     //private Map dependencies = new HashMap();
69

70     //private Map correspondences = new HashMap();
71

72     /** the code in this cu at myNode either impacts, or is impacted by
73       the code at otherNode **/

74     //public void addCorrespondence(ASTObject myNode, ASTObject otherNode) {
75
// CollectionUtil.getSetInMap(correspondences, myNode).add(otherNode);
76
//}
77

78     private Map pointsTo = new HashMap();
79     private Map pointedToBy = new HashMap();
80
81     public Set getPointsToSet(ASTObject fromNode) {
82         return CollectionUtil.getSetInMap(pointsTo, fromNode);
83     }
84
85     public Set getPointedToBySet(ASTObject fromNode) {
86         return CollectionUtil.getSetInMap(pointedToBy, fromNode);
87     }
88
89     public void addPointsTo(ASTObject fromNode, ASTObject toNode) {
90         // backwards compatibility
91
//addCorrespondence(fromNode, toNode);
92

93         CollectionUtil.getSetInMap(pointsTo, fromNode).add(toNode);
94     }
95
96     public void addPointedToBy(ASTObject toNode, ASTObject fromNode) {
97         // backwards compatibility
98
//addCorrespondence(toNode, fromNode);
99

100         CollectionUtil.getSetInMap(pointedToBy, toNode).add(fromNode);
101     }
102
103     private String JavaDoc quoteString(String JavaDoc s) {
104         return "\""+s+"\" ";
105     }
106
107     private String JavaDoc getKind(ASTObject node) {
108         if (node instanceof TypeDec) {
109             TypeDec typeDec = (TypeDec)node;
110             if (typeDec instanceof InterfaceDec) return "interface";
111             if (typeDec instanceof ClassDec) return "class";
112             if (typeDec instanceof AspectDec) return "aspect";
113         }
114
115         if (node instanceof VarDec) return "field";
116         if (node instanceof InitializerDec) return "initializer";
117         if (node instanceof ConstructorDec) return "constructor";
118         if (node instanceof MethodDec) return "method";
119
120
121 // if (node instanceof IntroduceOnDec) return "introduction";
122
if (node instanceof IntroducedDec) return "introduction";
123         if (node instanceof IntroducedSuperDec) return "introduction";
124
125         //********************The new types**********************
126

127         if (node instanceof AdviceDec) return "advice";
128         if (node instanceof PointcutDec) return "pointcut";
129
130         // internal error
131
return "unknown";
132     }
133
134     private String JavaDoc getDeclaringType(ASTObject node) {
135         Type declaringType = node.getDeclaringType();
136         if (declaringType == null) return null;
137         return declaringType.getTypeDec().getExtendedId().replace('$', '.');
138     }
139
140     private String JavaDoc getSignature(ASTObject node) {
141         if (node instanceof TypeDec) {
142             return ((TypeDec)node).getId();
143         }
144
145
146 // if (node instanceof IntroduceOnDec) {
147
// return "introduction";
148
// }
149

150         if (node instanceof PointcutDec) {
151             PointcutDec ccDec = (PointcutDec)node;
152             String JavaDoc name = ccDec.getName();
153
154             return name+ccDec.getFormals().toShortString();
155         }
156
157
158         if (node instanceof AdviceDec) {
159             AdviceDec aDec = (AdviceDec)node;
160             return aDec.toShortString();
161         }
162
163
164         if (node instanceof VarDec) {
165             return ((VarDec)node).getName();
166         }
167
168         if (node instanceof CodeDec) {
169             CodeDec codeDec = (CodeDec)node;
170             String JavaDoc name = codeDec.getName();
171             if (name.equals("<init>")) name = getDeclaringType(node);
172
173             return name+codeDec.getFormals().toShortString();
174         }
175
176         if (node instanceof IntroducedDec) {
177             IntroducedDec idec = (IntroducedDec)node;
178             return idec.getTargets().toShortString()+"."+getSignature(idec.getDec());
179         }
180         if (node instanceof IntroducedSuperDec) {
181             IntroducedSuperDec idec = (IntroducedSuperDec)node;
182
183             return idec.getTargets().toShortString()+"+" +
184                ( idec.getIsImplements() ? "implements" : "extends" );
185         }
186
187         return "...";
188     }
189
190     public String JavaDoc trimSig(String JavaDoc sig) {
191         int index = sig.length()-1;
192         while (index > 0) {
193             char ch = sig.charAt(index);
194             if (ch == '{' || ch == '}' || ch == ';' || Character.isWhitespace(ch)) {
195                 index--;
196                 continue;
197             }
198             break;
199         }
200         return sig.substring(0, index+1);
201     }
202
203     public String JavaDoc getModifiers(ASTObject node) {
204         if (node instanceof IntroducedDec) {
205             return ((IntroducedDec)node).getDec().getModifiers().toShortString();
206         } else if (node instanceof Dec) {
207             return ((Dec)node).getModifiers().toShortString();
208         } else {
209             return "";
210         }
211     }
212
213     /*
214     public Crosscut getCrosscut(ASTObject node) {
215         if (node instanceof CrosscutActionDec) {
216             CrosscutActionDec ccDec = (CrosscutActionDec)node;
217             return ccDec.getCrosscut();
218         } else if (node instanceof AdviceDec1) {
219             AdviceDec1 adviceDec = (AdviceDec1)node;
220             return adviceDec.getCrosscut();
221         } else {
222             return null;
223         }
224     }
225
226     public String getCrosscutDesignator(ASTObject node) {
227         Crosscut cc = getCrosscut(node);
228         if (cc == null) return null;
229         else return cc.toShortString();
230     }
231
232     public Declaration getCrosscutDeclaration(ASTObject node) {
233         Crosscut cc = getCrosscut(node);
234         if (cc == null) return null;
235         if (!(cc instanceof NameCrosscut)) return null;
236
237         NameCrosscut nameCC = (NameCrosscut)cc;
238         return makeBaseDeclaration(nameCC.getCrosscutDec());
239     }
240     */

241
242     public Pcd getPcd(ASTObject node) {
243         if (node instanceof AdviceDec) {
244             AdviceDec adviceDec = (AdviceDec)node;
245             return adviceDec.getPcd();
246         } else if (node instanceof PointcutDec) {
247             return ((PointcutDec)node).getPcd();
248         } else {
249             return null;
250         }
251     }
252
253     public String JavaDoc getPcdString(ASTObject node) {
254         Pcd cc = getPcd(node);
255         if (cc == null) return null;
256         return cc.toShortString();
257     }
258
259     public Declaration getPointcutDeclaration(ASTObject node) {
260         Pcd cc = getPcd(node);
261         if (cc == null) return null;
262         if (!(cc instanceof NamePcd)) return null;
263
264         NamePcd nameCC = (NamePcd)cc;
265         return makeBaseDeclaration(nameCC.getPointcutDec());
266     }
267
268
269     public String JavaDoc getFullSignature(ASTObject node) {
270         if (node instanceof Dec) {
271             Dec dec = (Dec)node;
272             return dec.getSignatureString();
273         } else {
274             return null;
275         }
276     }
277
278     public void doIndent(Writer writer, int size) throws IOException {
279         for(int i=0; i<size; i++) writer.write(" ");
280     }
281
282     public Declaration makeBaseDeclaration(ASTObject node) {
283         if (node == null) return null;
284
285         //XXX hack for IDE folks for now
286
if (node instanceof InitializerDec) return null;
287
288         //??? I don't think we want to show these
289
if (node instanceof Dec) {
290             if ( ((Dec)node).isIntroduced()) return null;
291         }
292
293         String JavaDoc kindPrefix = "";
294 // if (node.flag == IntroductionWeaver.INTRODUCED) {
295
// kindPrefix = "introduced-";
296
// }
297
if (node.getSourceFile() == null) {
298             //!!!node.showWarning("no source file");
299
return null;
300         }
301
302         Declaration dec =
303                new Declaration(node.getBeginLine(), node.getEndLine(),
304                                node.getBeginColumn(), node.getEndColumn(),
305                                getModifiers(node),
306                                getSignature(node),
307                                getFullSignature(node),
308                                getPcdString(node),
309                                getDeclaringType(node),
310                                kindPrefix+getKind(node),
311                                node.getSourceFile().getAbsolutePath(),
312                                node.getFormalComment(),
313                                node.getCompilationUnit().getPackageName()
314                                );
315         dec.setCrosscutDeclaration(getPointcutDeclaration(node));
316         return dec;
317     }
318
319     private Declaration[] makeDeclarations(Collection nodes) {
320         if (nodes == null) return new Declaration[0];
321
322         List newDeclarations = new ArrayList();
323         for (Iterator iter = nodes.iterator(); iter.hasNext(); ) {
324             Declaration dec = makeBaseDeclaration((ASTObject)iter.next());
325             //System.err.println(" add: "+dec.getSignature());
326
if (dec == null) continue;
327             newDeclarations.add(dec);
328         }
329
330         return (Declaration[])newDeclarations.toArray(new Declaration[0]);
331     }
332
333     private Declaration[] decsToDeclarations(Decs decs) {
334         if (decs == null) return new Declaration[0];
335
336         List newDeclarations = new ArrayList();
337         for (int i=0; i<decs.size(); i++) {
338             if (!decs.get(i).isLanguageVisible()) {
339                 continue;
340             }
341             Declaration dec = makeDeclaration(decs.get(i));
342             //System.err.println(" add: "+dec.getSignature());
343
if (dec == null) continue;
344             newDeclarations.add(dec);
345         }
346
347         return (Declaration[])newDeclarations.toArray(new Declaration[newDeclarations.size()]);
348     }
349
350     private Declaration makeDeclaration(ASTObject node) {
351         Declaration declaration = makeBaseDeclaration(node);
352         if (declaration == null) return null;
353
354         declaration.setPointsTo(makeDeclarations((Collection)pointsTo.get(node)));
355         declaration.setPointedToBy(makeDeclarations((Collection)pointedToBy.get(node)));
356
357         if (node instanceof TypeDec) {
358             Decs enclosedDecs = ((TypeDec)node).getBody();
359             declaration.setDeclarations(decsToDeclarations(enclosedDecs));
360         }
361 // if (node instanceof IntroduceOnDec) {
362
// Decs enclosedDecs = ((IntroduceOnDec)node).getBody();
363
// declaration.setDeclarations(decsToDeclarations(enclosedDecs));
364
// }
365

366         return declaration;
367     }
368
369     public void dumpSymbolFile(OutputStream stream) throws IOException {
370         Declaration[] declarations = decsToDeclarations(compilationUnit.getDecs());
371         //System.err.println("size: "+ declarations.size());
372

373         ObjectOutputStream objectStream = new ObjectOutputStream(stream);
374         objectStream.writeObject(declarations);
375         objectStream.close();
376     }
377 }
378
Popular Tags