KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > SourceLineFinder


1 /* -*- Mode: JDE; 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 package org.aspectj.debugger.base;
25
26 import org.aspectj.tools.ide.SourceLine;
27 import com.sun.jdi.*;
28 import java.util.*;
29 import java.io.*;
30
31 import org.aspectj.util.LineNumberTableMapper;
32
33 import org.aspectj.asm.*;
34
35 public class SourceLineFinder {
36
37 // private SymbolManager sm;
38
private String JavaDoc fullSrcPath;
39     private Location loc;
40
41     static boolean debug = false;
42     static void db(Object JavaDoc o) {
43         if (debug) System.out.println(o);
44     }
45
46     public SourceLineFinder(String JavaDoc fullSrcPath) {
47 // this.sm = sm;
48
this.fullSrcPath = fullSrcPath;
49 // System.err.println(">> fullSrcPath: " + fullSrcPath);
50
}
51
52     public SourceLine findMethod(Location loc) {
53         try {
54             String JavaDoc pkgfullName = loc.declaringType().name();
55             int pkgLoc = pkgfullName.lastIndexOf('.');
56             String JavaDoc pkg = (-1 == pkgLoc) ? null : pkgfullName.substring(0, pkgLoc).replace('.', '/');
57             String JavaDoc attrib = loc.sourceName();
58             String JavaDoc srcPath = org.aspectj.debugger.base.AJDebugger.INSTANCE.getSourcePath();
59             File srcRoot = new File(srcPath);
60
61 // System.err.println(">> findMethod attrib: " + attrib + ", pkg: " + pkg + ", src: " + srcPath);
62
LineNumberTableMapper lntm = new LineNumberTableMapper(attrib, pkg, srcRoot);
63             System.err.println(">> lntm: " + lntm.getCorrespondingFile(loc.lineNumber()).getPath() + " : " + lntm.getCorrespondingLineNumber(loc.lineNumber()));
64 // throw new RuntimeException();
65
return new SourceLine(lntm.getCorrespondingFile(loc.lineNumber()).getPath(), lntm.getCorrespondingLineNumber(loc.lineNumber()));
66
67 // return findMethod(method(this.loc = loc), classDec(className(loc)));
68
} catch (com.sun.jdi.AbsentInformationException aie) {
69             System.err.println("> ERROR: absent information.");
70             aie.printStackTrace();
71             return null;
72         }
73     }
74
75     public SourceLine findMethod(String JavaDoc className, String JavaDoc methodName) {
76         System.err.println(">> findMethod className: " + className + ", methodName: " + methodName);
77 // return findMethod(methodName, classDec(className));
78
// return null;
79
return new SourceLine(className, -1);
80     }
81
82 // private Method method(Location loc) {
83
// return (loc != null && loc.codeIndex() == 0) ? loc.method() : null;
84
// }
85
//
86
// private String className(Location loc) {
87
// return loc != null ? loc.declaringType().name() : null;
88
// }
89
//
90
// private Declaration classDec(String className) {
91
// return classDec(sm.getDeclarations(fullSrcPath), className);
92
// }
93
//
94
// private Declaration classDec(Declaration[] decs, String className) {
95
// if (decs == null) return null;
96
// List q = new Vector();
97
// for (int i = 0; i < decs.length; i++) {
98
// q.add(decs[i]);
99
// }
100
// while (!q.isEmpty()) {
101
// Declaration dec = (Declaration) q.remove(0);
102
// if (matchesClass(dec, className)) return dec;
103
// Declaration[] newDecs = dec.getDeclarations();
104
// if (newDecs != null) {
105
// for (int i = 0; i < newDecs.length; i++) {
106
// q.add(newDecs[i]);
107
// }
108
// }
109
// } while (!q.isEmpty());
110
// return null;
111
// }
112
//
113
// private boolean matchesClass(Declaration dec, String className) {
114
// return new ClassMatcher().matches(dec, className);
115
// }
116
//
117
// static class ClassMatcher {
118
// public boolean matches(Declaration dec, String className) {
119
// int idot = className.lastIndexOf('.');
120
// if (idot != -1) {
121
// className = className.substring(idot+1);
122
// }
123
// if (dec == null || className == null) return false;
124
// String kind = dec.getKind();
125
// String signature = dec.getSignature();
126
// return kind.equals("class") && signature.equals(className);
127
// }
128
// }
129
//
130
// private SourceLine findMethod(Method method, Declaration classDec) {
131
// db("findMethod: method=" + method + " class=" + classDec + " loc=" + loc);
132
// if (classDec == null || method == null) return null;
133
// SourceLine sl = null;
134
// Declaration[] decs = classDec.getDeclarations();
135
// if (decs == null) return null;
136
// int N = decs.length;
137
// for (int i = 0; i < N; i++) {
138
// Declaration dec = decs[i];
139
// if (!dec.getKind().equals("method")) continue;
140
// if (!methodMatches(dec, method)) continue;
141
// sl = new SourceLine(fullSrcPath, dec.getBeginLine());
142
// db("!!! Returning sl=" + sl + " for " + method + " loc=" + loc);
143
// break;
144
// }
145
// return sl;
146
// }
147
//
148
// private SourceLine findMethod(String name, Declaration classDec) {
149
// db("findMethod: name=" + name + " class=" + classDec + " loc=" + loc);
150
// if (classDec == null || name == null) return null;
151
// SourceLine sl = null;
152
// Declaration[] decs = classDec.getDeclarations();
153
// if (decs == null) return null;
154
// int N = decs.length;
155
// for (int i = 0; i < N; i++) {
156
// Declaration dec = decs[i];
157
// if (!isMethod(dec)) continue;
158
// if (!methodMatches(dec, name)) continue;
159
// sl = new SourceLine(fullSrcPath, dec.getBeginLine());
160
// db("!!! Returning sl=" + sl + " for " + name + " loc=" + loc);
161
// break;
162
// }
163
// return sl;
164
// }
165
//
166
// private boolean isMethod(Declaration dec) {
167
// return dec.getKind().equals("method") || dec.getKind().equals("constructor");
168
// }
169
//
170
// private boolean methodMatches(Declaration methodDec, Method method) {
171
// return new MethodMatcher().matches(methodDec, method);
172
// }
173
//
174
// private boolean methodMatches(Declaration methodDec, String name) {
175
// return new MethodMatcher().matches(methodDec, name);
176
// }
177
//
178
// public static class MethodMatcher {
179
//
180
// public boolean matches(Declaration methodDec, Object methodObject) {
181
// String nomods = stripModifiers(methodDec.getFullSignature());
182
// String name = nomods;
183
// int iparen = name.indexOf("(");
184
// name = name.substring(0, iparen); // we want the exception!
185
// if (methodObject instanceof String) {
186
// String methodName = methodObject+"";
187
// return methodName.equals(name);
188
// }
189
// Method method = (Method) methodObject; // we want the exception!
190
// if (!name.equals(method.name())) {
191
// return false;
192
// }
193
//
194
// //!! Come back and do this in one iteration
195
// String args = nomods.substring(iparen+1, nomods.length()-1); // no last paren
196
// return method.argumentTypeNames().equals(args(args));
197
// }
198
//
199
// private List args(String argsString) {
200
// List args = new Vector();
201
// StringTokenizer tok = new StringTokenizer(argsString, "(),");
202
// while (tok.hasMoreTokens()) {
203
// String arg = tok.nextToken();
204
// int ispace = arg.indexOf(" ");
205
// arg = arg.substring(0, ispace);
206
// args.add(arg);
207
// }
208
// return args;
209
// }
210
//
211
// private String stripModifiers(String sig) {
212
// int iparen = sig.indexOf("(");
213
// if (iparen != -1) {
214
// for (int i = iparen-1; i >= 0; i--) {
215
// if (sig.charAt(i) == ' ') {
216
// return sig.substring(i+1).trim();
217
// }
218
// }
219
// }
220
// return sig;
221
// }
222
//
223
// }
224
}
225
Popular Tags