KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > AJStackFrameFormatter


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger 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
23 package org.aspectj.debugger.gui;
24
25 import org.aspectj.debugger.base.*;
26 import java.util.*;
27 import com.sun.jdi.*;
28 import org.aspectj.tools.ide.*;
29 import java.io.File JavaDoc;
30
31 public class AJStackFrameFormatter {
32
33     public final static String JavaDoc INVALID = "Invalid Frame";
34     public final static String JavaDoc reception = "$reception";
35     public final static String JavaDoc execution = "$execution";
36     public final static String JavaDoc around = "$around";
37
38     public static MethodAndSource format(StackFrame frame) {
39         String JavaDoc methodStr = "";
40         String JavaDoc sourceStr = "";
41         int lineNumber = -1;
42         String JavaDoc path = "";
43
44         try {
45             if (frame == null) {
46                 return new MethodAndSource
47                     ("<BAD FRAME>", AJLineMapper.UNKNOWN_SOURCE_STRING,
48                      -1, path);
49             }
50
51             Location loc = frame.location();
52             Method meth = loc.method();
53             String JavaDoc decName = meth.declaringType().name();
54             String JavaDoc methName = ComponentRepository.getAJDebugger().method(loc);
55
56             // Get the path from the declaring type
57
int idot = decName.lastIndexOf('.');
58             if (idot != -1) {
59                 path = decName.substring(0, idot).replace('.', File.separatorChar);
60             } else {
61                 path = "";
62             }
63
64             methodStr = decName + "." + methName;
65             sourceStr = "";
66
67             int locLine = ComponentRepository.getAJDebugger().lineNumber(loc);
68             lineNumber = locLine;
69             if (meth instanceof Method && ((Method)meth).isNative()) {
70                 sourceStr += "native method";
71             } else if (locLine == AJLineMapper.NON_MAPPING_SOURCE_LINE) {
72                 try {
73                     sourceStr =
74                         ComponentRepository.getAJDebugger().
75                         sourceName(loc);
76                 } catch (AbsentInformationException e) {
77                     sourceStr += "<unknown>";
78                 }
79             } else if (locLine != AJLineMapper.NO_SOURCE_LINE) {
80                 try {
81                     sourceStr =
82                         ComponentRepository.getAJDebugger().
83                         sourceName(loc);
84                 } catch (AbsentInformationException e) {
85                     sourceStr += "<unknown>";
86                 }
87                 lineNumber =
88                     ComponentRepository.getAJDebugger().
89                     lineNumber(loc);
90             } else {
91                 sourceStr += "<unknown>";
92             }
93         } catch (InvalidStackFrameException e) {
94             methodStr = "<THREAD RESUMED>";
95             sourceStr = AJLineMapper.UNKNOWN_SOURCE_STRING;
96         }
97         return new MethodAndSource(methodStr, sourceStr, lineNumber, path);
98     }
99
100     public static class MethodAndSource {
101         String JavaDoc method;
102         String JavaDoc sourceName;
103         int lineNumber = -1;
104         String JavaDoc path;
105         String JavaDoc shortSourceName;
106
107         MethodAndSource(String JavaDoc method, String JavaDoc sourceName,
108                         int lineNumber, String JavaDoc path) {
109             this.method = method;
110             this.sourceName = sourceName;
111             this.shortSourceName = shortSourceName(sourceName);
112             this.lineNumber = lineNumber;
113             this.path = path;
114         }
115
116         public String JavaDoc getProto() {
117             return getSourceName() + ":" + getLineNumber();
118         }
119
120         public String JavaDoc getSourceName() {
121             return sourceName;
122         }
123         
124         public int getLineNumber() {
125             return lineNumber;
126         }
127
128         public boolean isValid() {
129             return (lineNumber > -1);
130         }
131
132         public String JavaDoc getFullPath() {
133             String JavaDoc pfx = "";
134             if (!path.equals("") && !sourceName.startsWith(path)) {
135                 pfx = path + java.io.File.separatorChar;
136             }
137             return pfx + sourceName;
138         }
139
140         public String JavaDoc toString() {
141             return method + " (" + shortSourceName +
142                    (isValid() ? ":" + lineNumber : "") + ")";
143         }
144
145         private String JavaDoc shortSourceName(String JavaDoc sourceName) {
146             int ilastSlash = sourceName.lastIndexOf('\\');
147             int ilastBackSlash = sourceName.lastIndexOf('/');
148             int ilastFileSeparator = sourceName.lastIndexOf(File.separatorChar);
149             int ilastSep = ilastSlash;
150             if (ilastBackSlash > ilastSep) {
151                 ilastSep = ilastBackSlash;
152             }
153             if (ilastFileSeparator > ilastSep) {
154                 ilastSep = ilastFileSeparator;
155             }
156             if (ilastSep != -1) {
157                 return sourceName.substring(ilastSep+1);
158             }
159             return sourceName;
160         }
161     }
162
163     static String JavaDoc formatMethod(Method method) {
164         String JavaDoc decName = method.declaringType().name();
165         String JavaDoc methName = method.name();
166         String JavaDoc methString = method + "";
167         String JavaDoc ajFile = AJLineMapper.getSourceFilePathFromAJCClass(decName);
168         if (ajFile == null) {
169             return methName;
170         }
171
172         // Need to strip the package off the method
173
String JavaDoc classMethodString = methString;
174         int i2dot = methString.indexOf("(");
175         int numDots = 0;
176         while (i2dot > -1) {
177             if (methString.charAt(i2dot) == '.') {
178                 if ((++numDots) == 2) {
179                     break;
180                 }
181             }
182             i2dot--;
183         }
184         if (i2dot != -1) {
185             classMethodString = methString.substring(i2dot + 1);
186         }
187         int line = AJDecParser.getMethodLineNumber(ajFile, classMethodString);
188
189         // Find the base name of this method
190
int iDollarOne = methName.indexOf("$");
191         String JavaDoc baseName = null;
192         if (iDollarOne != -1) {
193             baseName = methName.substring(0, iDollarOne);
194         } else {
195             baseName = methName;
196         }
197
198         String JavaDoc ext = "";
199         String JavaDoc name = baseName;
200
201         //
202
// If this method is advice, we'll just use AJTranslator
203
// Otherwise, there are only two mapping lines, and these labels are:
204
// - call: Line that maps to a method dec. in the source.
205
// - body: Line that does map to a method dec. in the source.
206
// - int. call: Line that doesn't map to a method dec. in the source.
207
//
208
Declaration dec = AJLineMapper.sm().getDeclarationAtLine(ajFile, line);
209         if (AJTranslator.isAdvice(methName)) {
210             name = AJTranslator.translateWorkingDirMethodName(methName);
211         //XXX Work around because 214 in spacewar/Ship.java was mapping to
212
//XXX a method call, when 212 was the real method line
213
} else if (AJTranslator.isReception(methName)) {
214             name += " [reception]";
215         } else if (dec != null && dec.getKind().equals("method")) {
216             name += " [call]";
217         } else if (!AJTranslator.isNonMappingMethod(method)) {
218             name += " [body]";
219         } else {
220             name += " [intermediate call]";
221         }
222         return name;
223     }
224
225     public static String JavaDoc format(StackFrame frame,
226                                 int index,
227                                 boolean showPC) {
228         return format(frame, index, showPC, null);
229     }
230
231     public static String JavaDoc format(AJStackFrameNode node,
232                                 boolean showPC) {
233         return format(node.getStackFrame(),
234                       node.index()+1,
235                       showPC,
236                       node.getOriginalLocation());
237     }
238
239     private static String JavaDoc format(StackFrame frame,
240                                  int index,
241                                  boolean showPC,
242                                  Location originalLocation) {
243         String JavaDoc frameString = "";
244         Location loc = null;
245         int lineNum = -1;
246         String JavaDoc sourceName = "<not available>";
247         try {
248             loc = frame.location();
249             lineNum = loc.lineNumber();
250             sourceName = loc.sourceName();
251         } catch (InvalidStackFrameException e) {
252             return INVALID + ": " + e.getMessage();
253         }catch (AbsentInformationException e) {
254             //return INVALID + ": " + e.getMessage();
255
}
256
257         Method meth = loc.method();
258         frameString += " [" + index + "] ";
259         frameString += meth;
260         frameString += " (";
261
262         if (meth instanceof Method && ((Method)meth).isNative()) {
263             frameString += "native method";
264         } else {
265             if (lineNum == 2 ) { // HACK: !!!
266
frameString += "receiving";
267             }
268             else {
269                 frameString += new File JavaDoc(sourceName).getName();
270             }
271             if (lineNum != -1) {
272                 frameString += ':';
273                 frameString += lineNum; //newLineNum;
274
}
275         }
276         frameString += ')';
277         if (showPC) { //XX
278
long pc = loc.codeIndex();
279             if (pc != -1) {
280                 frameString += ", pc = " + pc;
281             }
282         }
283
284         return frameString;
285     }
286 }
287
Popular Tags