KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.File JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import org.aspectj.tools.ide.Declaration;
30
31 public class AJDecParser {
32
33     public final static int BAD = -1;
34
35     public static int getMethodLineNumber(String JavaDoc fileName,
36                                           String JavaDoc methodName) {
37         if (fileName == null) {
38             return BAD;
39         }
40         Util.debug("Looking for methodName=" + methodName + " in " + fileName);
41         String JavaDoc fullName = new File JavaDoc(fileName).getAbsolutePath();
42         Declaration[] decs =
43             AJLineMapper.symbolManager.
44             getDeclarations(fullName);
45         if (decs == null) {
46             return BAD;
47         }
48         MethodComparator c = new MethodComparator();
49         for (int i = 0; i < decs.length; i++) {
50             int result = getBeginLine(decs[i], methodName, c);
51             if (result != BAD) {
52                 return result;
53             }
54         }
55         return BAD;
56   }
57
58     public static int getBeginLine(Declaration dec,
59                                    String JavaDoc s,
60                                    DeclarationComparator c) {
61         if (c.compare(dec, s)) {
62             return dec.getBeginLine();
63         }
64         Declaration[] decs = dec.getDeclarations();
65         int result = BAD;
66         for (int i = 0; i < decs.length; i++) {
67             if ((result = getBeginLine(decs[i], s, c)) != BAD) {
68                 return result;
69             }
70         }
71         return BAD;
72     }
73 }
74
75 interface DeclarationComparator {
76   boolean compare(Declaration d, String JavaDoc s);
77 }
78
79 class MethodComparator implements DeclarationComparator {
80     public boolean compare(Declaration d1, String JavaDoc s) {
81         String JavaDoc method1 = getMethodPrototype(d1);
82         String JavaDoc method2 = s;
83         return (!method1.equals("") &&
84                 !method2.equals("") &&
85                 method1.equals(method2));
86     }
87
88     public static String JavaDoc getMethodPrototype(Declaration dec) {
89         if (dec == null ||!(dec.getKind().equals("method") ||
90                             dec.getKind().equals("constructor"))) {
91             return "";
92         }
93         String JavaDoc fullSig = dec.getFullSignature();
94         String JavaDoc sig = dec.getSignature();
95         String JavaDoc methodName = sig.substring(0, sig.indexOf("("));
96         if (methodName.equals("new")) {
97             methodName = dec.getDeclaringType();
98         }
99         String JavaDoc name = dec.getDeclaringType() + "." + methodName;
100         String JavaDoc args = fullSig.substring(fullSig.indexOf("(")+1,
101                                         fullSig.indexOf(")"));
102         String JavaDoc str = name + "(";
103         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(args, ",");
104         if (!tok.hasMoreTokens()) {
105         str += ")";
106         }
107         while (tok.hasMoreTokens()) {
108             String JavaDoc arg = tok.nextToken().trim();
109             String JavaDoc type = arg.substring(0, arg.indexOf(" "));
110             str += arg.substring(0, arg.indexOf(" ")) +
111                    (tok.hasMoreTokens() ? "," : ")");
112         }
113         return str;
114   }
115 }
116
Popular Tags