KickJava   Java API By Example, From Geeks To Geeks.

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


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.Vector JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.lang.String JavaDoc;
31 import java.io.BufferedReader JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35
36 public class StringBasedSymbolManager {
37
38     public static void main(String JavaDoc[] args) {
39     fetchDeclarations(args[0]);
40     //fetchSourceLine(args[0], 154);
41
}
42
43     public static void fetchDeclarations(String JavaDoc filename) {
44     try {
45         SymbolManager mgr = SymbolManager.getSymbolManager();
46         Declaration[] decls = mgr.getDeclarations(filename);
47         if ( decls == null )
48         throw new Error JavaDoc("Can't find declarations file for " + filename);
49         printDecls(decls, true);
50     }
51     catch (Error JavaDoc e) {
52         lose(e);
53     }
54     }
55
56     public static void fetchSourceLine(String JavaDoc filename, int line) {
57     try {
58         SymbolManager mgr = SymbolManager.getSymbolManager();
59         SourceLine srl = mgr.mapToSourceLine(filename, line);
60         if ( srl == null )
61         print("nil");
62         else {
63         String JavaDoc fn = srl.filename;
64         int ln = srl.line;
65         print("(\"" + fixFilename(fn) + "\" . " + ln + ")");
66         }
67     }
68     catch (Error JavaDoc e) {
69         lose(e);
70     }
71     }
72
73     static void lose(Error JavaDoc e) {
74     try {
75         print("(ERROR \"" + e.toString() + "\")");
76     }
77     catch(Error JavaDoc ex) { }
78     }
79
80     static void printDecls(Declaration[] decls, boolean recurse) {
81         print("(");
82     for (int i = 0; i < decls.length; i++) {
83         Declaration decl = decls[i];
84         /*
85          * null means that one symbol file pointed to a declaration
86          * that another symbol file should have contained but didn't.
87          * This can happen if the symbol files are out of date with
88          * each other.
89          */

90         if ( decl != null )
91         printDecl(decl, recurse);
92         }
93         print(") ");
94     }
95
96     static void printDecl(Declaration decl, boolean recurse) {
97         String JavaDoc crosscutDesignator = decl.getCrosscutDesignator();
98     String JavaDoc kind = decl.getKind().toLowerCase();
99     Declaration mappedDecl = nearestMappedDecl(decl);
100         print("(");
101     print("(" + mappedDecl.getBeginLine() + " . " + mappedDecl.getBeginColumn() + ") ");
102         print("(" + mappedDecl.getEndLine() + " . " + mappedDecl.getEndColumn() + ") ");
103         print(kind + " "); //2
104
if (crosscutDesignator == null)
105         print("\"" + decl.getSignature() + "\" "); //3
106
else if (kind.equals("introduction"))
107         print("\"" + decl.getSignature() + " " + crosscutDesignator + "\" "); //3
108
else /* advice */
109         print("\"" + decl.getSignature() + ": " + crosscutDesignator + "\" "); //3
110
print("\"" + fixFilename(decl.getFilename()) + "\""); //4
111
print("\"" + decl.getDeclaringType() + "\" "); //5
112
if (recurse) {
113         printDecls(decl.getTargets(), false); //6
114
printDecls(decl.getPointedToBy(), false); //7
115
printDecls(decl.getDeclarations(), true); //8
116
}
117     else {
118         print("nil");
119         print("nil");
120         print("nil");
121     }
122     print(fixBoolean(decl.isType())); //9
123
print(fixBoolean(decl.isIntroduced())); //10
124
print(fixBoolean(decl.hasBody())); //11
125
print(fixBoolean(decl.hasSignature())); //12
126

127         print(")");
128     }
129
130     /*
131      * this method and maybe the next should really be in Declaration.
132      */

133
134     static boolean legalLineNumber(int ln) { return (ln != -1); }
135
136     /*
137      * Searches up declaration hierarchy until it finds a declaration
138      * that has useable line numbers.
139      */

140     static Declaration nearestMappedDecl(Declaration decl) {
141         Declaration parentDecl;
142         if (legalLineNumber(decl.getBeginLine()))
143             return decl;
144         else if ((parentDecl = decl.getParentDeclaration()) != null)
145             return nearestMappedDecl(parentDecl);
146         else
147             return decl; // bail with self if no parent
148
}
149
150     /*
151      * this should really be in Declaration. it only does something for
152      * introduction?
153      */

154
155     static String JavaDoc fixBoolean(boolean flag) {
156     if ( flag ) { return "t "; } else { return "nil ";}
157     }
158
159
160     static String JavaDoc fixFilename(String JavaDoc filename) {
161         return subst("\\\\", "\\", filename);
162     }
163
164     static int convertLineToPosition(String JavaDoc filename, int lineNumber)
165     throws FileNotFoundException JavaDoc {
166
167     FileReader JavaDoc freader = new FileReader JavaDoc(filename);
168     BufferedReader JavaDoc breader = new BufferedReader JavaDoc(freader);
169     int position = 0;
170     try {
171         for(int n = 0; n < lineNumber; n++) {
172         String JavaDoc line = breader.readLine();
173         position += line.length();
174         }
175     }
176     catch (IOException JavaDoc e) {}
177     return position;
178     }
179
180
181     static void print(String JavaDoc string) {
182     System.out.println(string);
183     }
184
185     static String JavaDoc subst(String JavaDoc n, String JavaDoc o, String JavaDoc in) {
186     int pos = in.indexOf(o);
187     if (pos == -1)
188         return in;
189     return in.substring(0, pos) +
190            n +
191            subst(n, o, (in.substring(pos + o.length())));
192     }
193
194
195 }
196
Popular Tags