KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ide > SymbolManager


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.tools.ide;
26
27
28 import java.util.*;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.io.*;
33
34
35
36
37 public class SymbolManager {
38     private static final String JavaDoc SYMBOL_FILE_EXTENSION = ".ajsym";
39     private static final String JavaDoc SOURCE_LINES_FILE_EXTENSION = ".ajsline";
40
41     public static File mapFilenameToSymbolFile(String JavaDoc filename) {
42         return mapFilenameToNewExtensionFile(filename, SYMBOL_FILE_EXTENSION);
43     }
44
45     public static File mapFilenameToSourceLinesFile(String JavaDoc filename) {
46         return mapFilenameToNewExtensionFile(filename, SOURCE_LINES_FILE_EXTENSION);
47     }
48
49     public static File getSourceToOutputFile(String JavaDoc dirname) {
50         return new File(dirname, ".ajsline");
51     }
52
53     public static File getOutputToSourceFile(String JavaDoc dirname) {
54         return new File(dirname, ".ajoline");
55     }
56
57
58     private static File mapFilenameToNewExtensionFile(String JavaDoc filename, String JavaDoc ext) {
59         int lastDot = filename.lastIndexOf('.');
60         String JavaDoc basename = filename;
61         if (lastDot != -1) {
62             basename = basename.substring(0, lastDot);
63         }
64
65         return new File(basename+ext);
66     }
67
68     private static SymbolManager symbolManagerInstance = new SymbolManager();
69
70     public static SymbolManager getSymbolManager() {
71         return symbolManagerInstance;
72     }
73
74     /**
75      * @param filePath the full path to the preprocessed source file
76      * @param lineNumber line number in the preprocessed source file
77      * @return the <CODE>SourceLine</CODE> corresponding to the original file/line
78      * @see SourceLine
79      */

80     public SourceLine mapToSourceLine(String JavaDoc filePath, int lineNumber) {
81         Map map = lookupOutputToSource(filePath);
82
83         if (map == null) return null;
84
85         return (SourceLine)map.get(new SourceLine(filePath, lineNumber));
86     }
87
88
89     /**
90      * @param filePath the full path to the original source file
91      * @param lineNumber line number in the original source file
92      * @return the <CODE>SourceLine</CODE> corresponding to the preprocessed file/line
93      * @see SourceLine
94      */

95     public SourceLine mapToOutputLine(String JavaDoc filePath, int lineNumber) {
96         Map map = lookupSourceToOutput(filePath);
97
98         if (map == null) return null;
99
100         return (SourceLine)map.get(new SourceLine(filePath, lineNumber));
101     }
102
103
104
105     /****
106     public int mapToOutputLine(String filename, int line) {
107         Vector sourceLines = lookupSourceLines(filename);
108
109         // do linear search here
110         if (sourceLines == null) return -1;
111
112         for(int outputLine = 0; outputLine < sourceLines.size(); outputLine++) {
113             SourceLine sl = (SourceLine)sourceLines.elementAt(outputLine);
114
115             if (sl == null) continue;
116             if (sl.line == line) {
117                 String outputRoot = new File(filename).getName();
118                 String sourceRoot = new File(sl.filename).getName();
119                 if (outputRoot.equals(sourceRoot)) return outputLine + 1;
120             }
121         }
122
123         return -1;
124     }
125     ****/

126
127
128     public Declaration[] getDeclarations(String JavaDoc filename) {
129         return lookupDeclarations(filename);
130     }
131
132     // In the unusual case that there are multiple declarations on a single line
133
// This will return a random one
134
public Declaration getDeclarationAtLine(String JavaDoc filename, int line) {
135         return getDeclarationAtPoint(filename, line, -1);
136     }
137
138     public Declaration getDeclarationAtPoint(String JavaDoc filename, int line, int column) {
139
140         Declaration[] declarations = lookupDeclarations(filename);
141         //System.out.println("getting "+filename+", "+line+":"+column);
142
//System.out.println("decs: "+declarations);
143
return getDeclarationAtPoint(declarations, line, column);
144     }
145
146     public Declaration getDeclarationAtPoint(Declaration[] declarations, int line, int column) {
147         //!!! when we care about the performance of this method
148
//!!! these should be guaranteed to be sorted and a binary search used here
149
//!!! for now we use the simple (and reliable) linear search
150
if (declarations == null) return null;
151
152         for(int i=0; i<declarations.length; i++) {
153             Declaration dec = declarations[i];
154             if (dec.getBeginLine() == line) { // && dec.getEndLine() >= line) {
155
if (column == -1) return dec;
156                 if (dec.getBeginColumn() == column) { // && dec.getEndColumn() >= column) {
157
return dec;
158                 }
159             }
160             Declaration[] enclosedDecs = dec.getDeclarations();
161             if (enclosedDecs.length == 0) continue;
162
163             Declaration dec1 = getDeclarationAtPoint(enclosedDecs, line, column);
164             if (dec1 != null) return dec1;
165         }
166
167         //??? what should be returned for no declaration found
168
return null;
169     }
170
171     private Hashtable JavaDoc symbolFileEntryCache = new Hashtable JavaDoc();
172
173     private Declaration[] lookupDeclarations(String JavaDoc filename) {
174         CorrFileEntry entry = lookup(filename, mapFilenameToSymbolFile(filename),
175                                      symbolFileEntryCache);
176         return (Declaration[])entry.data;
177     }
178
179     private Hashtable JavaDoc sourceToOutputCache = new Hashtable JavaDoc();
180     private Hashtable JavaDoc outputToSourceCache = new Hashtable JavaDoc();
181
182     private Map lookupSourceToOutput(String JavaDoc filename) {
183         CorrFileEntry entry = lookup(filename,
184                       getSourceToOutputFile(new File(filename).getParent()),
185                       sourceToOutputCache);
186         return (Map)entry.data;
187     }
188
189     private Map lookupOutputToSource(String JavaDoc filename) {
190         CorrFileEntry entry = lookup(filename,
191                       getOutputToSourceFile(new File(filename).getParent()),
192                       outputToSourceCache);
193         return (Map)entry.data;
194     }
195
196     /* generic code for dealing with correlation files, serialization, and caching */
197     private static class CorrFileEntry {
198         public long lastModified;
199         public Object JavaDoc data;
200
201         public CorrFileEntry(long lastModified, Object JavaDoc data) {
202             this.lastModified = lastModified;
203             this.data = data;
204         }
205     }
206
207     private CorrFileEntry lookup(String JavaDoc filename, File file, Hashtable JavaDoc cache) {
208         CorrFileEntry entry = (CorrFileEntry)cache.get(filename);
209         if (entry != null && entry.lastModified == file.lastModified()) {
210             return entry;
211         }
212
213         entry = createCorrFileEntry(file);
214         cache.put(filename, entry);
215         return entry;
216     }
217
218     private CorrFileEntry createCorrFileEntry(File file) {
219         if (!file.exists()) {
220             return new CorrFileEntry(0l, null);
221         }
222
223         try {
224             long lastModified = file.lastModified();
225             ObjectInputStream stream =
226                 new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
227             Object JavaDoc data = stream.readObject();
228             stream.close();
229             return new CorrFileEntry(lastModified, data);
230         } catch (IOException ioe) {
231             //System.err.println("ERROR!!!");
232
//ioe.printStackTrace();
233
return new CorrFileEntry(0l, null);
234         } catch (ClassNotFoundException JavaDoc cce) {
235             //System.err.println("ERROR!!!");
236
//cce.printStackTrace();
237
return new CorrFileEntry(0l, null);
238         }
239     }
240
241
242     /**
243       * @param methodName method name without type or parameter list
244       * @return method name with ajc-specific name mangling removed,
245       * unchanged if there's not ajc name mangling present
246       */

247     public static String JavaDoc translateMethodName(String JavaDoc methodName) {
248         int firstDollar = methodName.indexOf('$');
249
250         if (firstDollar == -1) return methodName;
251
252         String JavaDoc baseName = methodName.substring(firstDollar);
253
254         if (methodName.indexOf("ajc") != -1) {
255             return "<" + baseName + " advice>";
256         } else {
257             return baseName;
258         }
259     }
260
261
262     /************************************************************************
263       The rest of the code in this file is just for testing purposes
264      ************************************************************************/

265
266     private static final void printIndentation(int indent, String JavaDoc prefix) {
267         for(int i=0; i< indent; i++) System.out.print(" ");
268         System.out.print(prefix);
269     }
270
271
272     private static final void printDeclaration(Declaration dec, int indent, String JavaDoc prefix) {
273         printIndentation(indent, prefix);
274         if (dec == null) {
275             System.out.println("null");
276             return;
277         }
278
279         System.out.println(dec.getKind()+": "+dec.getDeclaringType()+": "+
280                                             dec.getModifiers()+": "+dec.getSignature()+": " +
281                                             //dec.getFullSignature()+": "+
282
dec.getCrosscutDesignator()+
283                                             ": "+dec.isIntroduced()+": "+dec.getPackageName()+": "+dec.getBeginLine()+":"+dec.getBeginColumn()
284                                             );
285
286         //printIndentation(indent, "\"\"\"");
287
//System.out.println(dec.getFormalComment());
288
/*
289         if (dec.getParentDeclaration() != null) {
290             printDeclaration(dec.getParentDeclaration(), indent+INDENT, "PARENT ");
291         }
292        if (dec.getCrosscutDeclaration() != null) {
293             printDeclaration(dec.getCrosscutDeclaration(), indent+INDENT, "XC ");
294         }
295         */

296         if (prefix.equals("")) {
297             printDeclarations(dec.getTargets(), indent+INDENT, "T> ");
298             printDeclarations(dec.getPointsTo(), indent+INDENT, ">> ");
299             printDeclarations(dec.getPointedToBy(), indent+INDENT, "<< ");
300             printDeclarations(dec.getDeclarations(), indent+INDENT, "");
301         }
302     }
303
304     private static final void printDeclarations(Declaration[] decs, int indent, String JavaDoc prefix) {
305         for(int i=0; i<decs.length; i++) {
306             printDeclaration(decs[i], indent, prefix);
307         }
308     }
309
310     private static final int INDENT = 2;
311
312     static void printLines(String JavaDoc filename, Map baseMap) throws IOException {
313         if (baseMap == null) return;
314
315         String JavaDoc fullName = new File(filename).getCanonicalPath();
316         java.util.TreeMap JavaDoc map = new java.util.TreeMap JavaDoc();
317
318         for (Iterator i = baseMap.entrySet().iterator(); i.hasNext(); ) {
319             Map.Entry entry = (Map.Entry)i.next();
320             SourceLine keyLine = (SourceLine)entry.getKey();
321             if (!keyLine.filename.equals(fullName)) continue;
322
323             map.put(new Integer JavaDoc(keyLine.line), entry.getValue());
324         }
325
326         for (java.util.Iterator JavaDoc j = map.entrySet().iterator(); j.hasNext(); ) {
327             java.util.Map.Entry entry = (java.util.Map.Entry)j.next();
328
329             System.out.println(entry.getKey() + ":\t" + entry.getValue());
330         }
331     }
332
333     public static void main(String JavaDoc[] args) throws IOException {
334         for(int i=0; i<args.length; i++) {
335             String JavaDoc filename = args[i];
336             System.out.println(filename);
337
338             System.out.println("declaration mappings");
339             System.out.println("kind: declaringType: modifiers: signature: fullSignature: crosscutDesignator: isIntroduced: packageName: parentDeclaration");
340
341             Declaration[] declarations = getSymbolManager().getDeclarations(filename);
342             if (declarations != null) {
343                 printDeclarations(declarations, INDENT, "");
344             }
345
346             System.out.println("source to output");
347             printLines(filename, getSymbolManager().lookupSourceToOutput(filename));
348             System.out.println("output to source");
349             printLines(filename, getSymbolManager().lookupOutputToSource(filename));
350         }
351     }
352 }
353
Popular Tags