KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import com.sun.jdi.*;
28 import java.util.*;
29 import org.aspectj.tools.ide.*;
30
31 public class AJTranslator {
32
33     final static String JavaDoc AJC = "$ajc";
34     final static String JavaDoc BEFORE_AJC = "before" + AJC;
35     final static String JavaDoc AFTER_AJC = "after" + AJC;
36     final static String JavaDoc AROUND_AJC = "around" + AJC;
37
38     final static String JavaDoc EXECUTION = "$execution";
39     final static String JavaDoc RECEPTION = "$reception";
40     final static String JavaDoc CALL = "$call";
41     final static String JavaDoc ASPECT_OF = "aspectOf";
42     final static String JavaDoc HAS_ASPECT = "hasAspect";
43
44     final static String JavaDoc AROUND = "$around";
45     final static String JavaDoc AROUND_EXECUTION = AROUND + EXECUTION;
46     final static String JavaDoc AROUND_RECEPTION = AROUND + RECEPTION;
47     final static String JavaDoc AROUND_CALL = AROUND + CALL;
48
49     final static String JavaDoc[] SPECIAL_STRINGS = {
50         EXECUTION,
51         RECEPTION,
52         CALL,
53         ASPECT_OF,
54         HAS_ASPECT,
55     };
56
57     final static String JavaDoc[] ADVICE_STRINGS = {
58         BEFORE_AJC,
59         AFTER_AJC,
60         AROUND_AJC
61     };
62
63     /**
64      * Takes a FULLY qualified method name and returns the method name that
65      * really maps from an output file to a sourcefile.
66      */

67     public static String JavaDoc getMappingMethodName(String JavaDoc method) {
68         String JavaDoc clazz = AJUtil.getTypeFromMethod(method);
69
70         // If this class was compiled with AJC, it will jave a ajwd file
71
String JavaDoc file = AJLineMapper.getSourceFilePathFromAJCClass(clazz);
72         if (file == null) {
73             return method;
74         }
75
76         // Get the line of the method
77
int line = AJDecParser.getMethodLineNumber(file, method);
78         if (line == -1) {
79             return method;
80         }
81
82         // See if it is even advised
83
Declaration dec = AJLineMapper.symbolManager.getDeclarationAtLine(file, line);
84         if (dec == null ||
85             dec.getPointedToBy() == null ||
86             dec.getPointedToBy().length == 0) {
87             return method;
88         }
89
90         // If this method has advice, it's name will *not* map, so find all
91
// methods that start with <basename>$<clazz>, and see if these methods
92
// map back to source
93
String JavaDoc baseName = AJUtil.stripParens(method);
94         //ReferenceType refType = Env.getReferenceTypeFromToken(clazz);
95
ReferenceType refType = null;
96         try {
97           refType = ComponentRepository.getAJDebugger().getReferenceTypeFromToken(clazz);
98         } catch (DebuggerException de) {
99         }
100         if (refType == null) {
101             return method;
102         }
103         Iterator iter = refType.methods().iterator();
104         String JavaDoc uScoreClass = clazz.replace('.', '_');
105         String JavaDoc comp = AJUtil.stripTypeFromMethod(baseName) + "$" + uScoreClass;
106         while (iter.hasNext()) {
107             Method meth = (Method) iter.next();
108             if (!meth.name().startsWith(comp) || isNonMappingMethod(meth)) {
109                 continue;
110             }
111             return meth + "";
112         }
113         return method;
114     }
115
116     public static String JavaDoc translateFullWorkingDirMethodName(String JavaDoc meth) {
117         String JavaDoc type = AJUtil.getTypeFromMethod(meth);
118         String JavaDoc name = translateWorkingDirMethodName(
119                             AJUtil.getNameFromMethod(meth));
120         String JavaDoc params = AJUtil.getParamsFromMethod(meth);
121         return type + "." + name + "(" + params + ")";
122     }
123
124     /**
125      * @param method a fully-qualified method with type, name, and params
126      */

127     public static boolean isNonMappingMethod(Method method) {
128         Location loc = method.location();
129         if (loc == null) {
130             return false;
131         }
132         int line = AJLineMapper.getCorrespondingLine(loc).line;
133         if (line == AJLineMapper.NON_MAPPING_SOURCE_LINE) {
134             return true;
135         }
136         return false;
137     }
138
139     public static boolean isNonMappingMethod(String JavaDoc clazz, int oldLine) {
140         String JavaDoc ajFile = AJLineMapper.getSourceFilePathFromAJCClass(clazz);
141         if (ajFile == null) {
142             return false;
143         }
144         int line = AJLineMapper.getCorrespondingLine(clazz, oldLine).line;
145         if (line == AJLineMapper.NON_MAPPING_SOURCE_LINE) {
146             return true;
147         }
148         return false;
149     }
150
151     public static String JavaDoc getBaseName(String JavaDoc method) {
152         int end = method.indexOf("(");
153         if (end == -1) {
154             end = method.length();
155         }
156         int i = end - 1;
157         int idollar = -1;
158         while (i >= 0) {
159             char c = method.charAt(i);
160             if (c == '.') {
161                 break;
162             } else if (c == '$') {
163                 idollar = i;
164             }
165             i--;
166         }
167         return (idollar != -1) ? method.substring(0, idollar) : method;
168     }
169
170     public static String JavaDoc getBaseNameWithParams(String JavaDoc method) {
171         int ip = method.indexOf("(");
172         String JavaDoc params = "";
173         String JavaDoc nameAndMaybeType = method;
174         if (ip != -1) {
175             params = method.substring(ip);
176             nameAndMaybeType = method.substring(0, ip);
177         }
178         return getBaseName(nameAndMaybeType) + params;
179     }
180
181     public static String JavaDoc translateWorkingDirMethodName_(String JavaDoc methodName) {
182         int firstDollar = methodName.indexOf('$');
183
184         if (firstDollar == -1) return methodName;
185
186         String JavaDoc baseName = methodName.substring(0, firstDollar);
187
188         int iajc;
189         int iaroundExecution;
190         int iaroundReception;
191         int iaroundCall;
192         int iexecution;
193         int ireception;
194         int icall;
195
196         if ((iajc = methodName.indexOf(AJC)) != -1) {
197             String JavaDoc numStr = methodName.substring(iajc + AJC.length());
198             return baseName + "<" + "advice-#" + numStr + ">";
199         } else if ((iaroundExecution = methodName.indexOf(AROUND_EXECUTION)) != -1) {
200             return baseName + "<" + "around-execution>";
201         } else if ((iaroundReception = methodName.indexOf(AROUND_RECEPTION)) != -1) {
202             return baseName + "<" + "around-reception>";
203         } else if ((iaroundCall = methodName.indexOf(AROUND_CALL)) != -1) {
204             String JavaDoc numStr = methodName.substring(iaroundCall + AROUND_CALL.length());
205             return baseName + "<" + "around-call-#" + numStr + ">";
206         } else if ((iexecution = methodName.indexOf(EXECUTION)) != -1) {
207             return baseName + "<" + "execution>";
208         } else if ((ireception = methodName.indexOf(RECEPTION)) != -1) {
209             return baseName + "<" + "reception>";
210         } else if ((icall = methodName.indexOf(CALL))!= -1) {
211             String JavaDoc numStr = methodName.substring(icall + CALL.length());
212             return baseName + "<" + "call-#" + numStr + ">";
213         }
214         //return baseName;
215
return methodName;
216     }
217
218     public static String JavaDoc translateWorkingDirMethod(String JavaDoc clazz, String JavaDoc methodName) {
219         if (isAdvice(methodName)) {
220             return translateWorkingDirMethodName(methodName);
221         }
222         return getMappingMethodName(clazz + "." + methodName);
223     }
224
225     public static String JavaDoc translateWorkingDirMethodName(String JavaDoc methodName) {
226         int firstDollar = methodName.indexOf('$');
227         if (firstDollar == -1) {
228             return methodName;
229         }
230
231         String JavaDoc baseName = methodName.substring(0, firstDollar);
232         String JavaDoc noDollars = methodName.substring(firstDollar + 1).replace('$', '-');
233         return baseName + "<" + noDollars + ">";
234     }
235
236     public static boolean isAdvice(String JavaDoc methodName) {
237         if (methodName.indexOf(AJC) != -1) {
238             return true;
239         }
240         return false;
241     }
242
243     public static boolean isReception(String JavaDoc methodName) {
244         if (methodName.indexOf(RECEPTION) != -1) {
245             return true;
246         }
247         return false;
248     }
249
250     public static boolean isSpecialMethod(String JavaDoc methodName) {
251         for (int i = 0; i < SPECIAL_STRINGS.length; i++) {
252             if (methodName.indexOf(SPECIAL_STRINGS[i]) != -1) {
253                 return true;
254             }
255         }
256         return false;
257     }
258
259     public static String JavaDoc translateSourceMethodNameWithParams(String JavaDoc methodName) {
260         String JavaDoc params = AJUtil.getParamsFromMethod(methodName);
261         String JavaDoc rest = AJUtil.stripParens(methodName);
262         String JavaDoc result = translateSourceMethodName(rest);
263 // if (result.indexOf("-advice-#") == -1) {
264
return result + "(" + params + ")";
265 // }
266
// return result;
267

268     }
269
270     /**
271      * @param methodName A method with or w/o params, but <b>NO TYPE</b>.
272      */

273     public static String JavaDoc translateSourceMethodName(String JavaDoc methodName) {
274
275         // Keep <init>, <clinit>, and any name without <'s
276
int firstLT = methodName.indexOf("<");
277         if (firstLT < 1) {
278             return methodName;
279         }
280
281         String JavaDoc baseName = methodName.substring(0, firstLT);
282         String JavaDoc dollars = methodName.substring(firstLT + 1, methodName.length() - 1).replace('-', '$');
283         return baseName + "$" + dollars;
284     }
285
286     public static String JavaDoc translateSourceMethodName_(String JavaDoc methodName) {
287         if (methodName.equals("<init>") ||
288             methodName.equals("<clinit>")) {
289             return methodName;
290         } else if (methodName.startsWith("<") && methodName.endsWith(">")) {
291             //
292
// Advice
293
//
294
int iAdvice = methodName.indexOf("-advice-#");
295             if (iAdvice == -1) {
296                 return methodName;
297             }
298             String JavaDoc adviceType = methodName.substring(1, iAdvice);
299             int iNum = iAdvice + 9;
300             String JavaDoc numStr = methodName.substring(iNum, methodName.length()-1);
301             return adviceType + "$ajc" + numStr;
302
303         } else {
304             //
305
// Other stuff
306
//
307

308         }
309         return methodName;
310     }
311
312     public static String JavaDoc translateWorkingDirMethodNameWithParams(String JavaDoc methodName) {
313         String JavaDoc params = AJUtil.getParamsFromMethod(methodName);
314         String JavaDoc rest = AJUtil.stripParens(methodName);
315         String JavaDoc result = translateWorkingDirMethodName(rest);
316 // if (result.indexOf("-advice-#") == -1) {
317
return result + "(" + params + ")";
318 // }
319
// return result;
320
}
321
322     /**
323      * Returns the <b>real</b> name of this method.
324      * @param <code>classID</code> The full name of the class.
325      * @param <code>meth</code> The name of the method with params.
326      */

327     public static String JavaDoc getTranslatedMethodName(String JavaDoc classID, String JavaDoc meth) {
328         return "";
329 // String filePath = AJLineMapper.getSourceFilePathFromAJCClass(classID);
330
// if (filePath == null) {
331
// return meth;
332
// }
333
//
334
// //
335
// // Search all the declarations util we find a method with name 'meth'
336
// //
337
// int line = AJDecParser.getMethodLineNumber(filePath, classID + "." + meth);
338
// Util.msg("LINE", line+"", java.awt.Color.darkGray);
339
// SourceLine sl = getCorrespondingOutputLine(classID, line);
340
// if (sl == null) {
341
// return meth;
342
// }
343
// int outLine = sl.line;
344
// try {
345
// ReferenceType refType = Env.getReferenceTypeFromToken(classID);
346
// Iterator iter = refType.methods().iterator();
347
// while (iter.hasNext()) {
348
// Method method = (Method) iter.next();
349
// Location loc = method.location();
350
// int locLine = loc.lineNumber();
351
// if (locLine == outLine) {
352
// Util.debug("GOT ONE: " + loc);
353
// return method.name();
354
// }
355
// }
356
// } catch (VMNotConnectedException e) {
357
// return meth;
358
// }
359
// return meth;
360
}
361 }
362
Popular Tags