KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > request > SourceLineBreakpointRequestAction


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.request;
24
25 import org.aspectj.debugger.base.*;
26
27 import com.sun.jdi.*;
28 import com.sun.jdi.request.*;
29 import com.sun.jdi.event.*;
30 import org.aspectj.tools.ide.SourceLine;
31 import java.io.*;
32 import java.util.*;
33
34 /**
35  * SourceLineBreakpointRequestAction.java
36  *
37  *
38  * Created: Tue Aug 29 10:50:47 2000
39  *
40  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
41  */

42
43 public abstract class SourceLineBreakpointRequestAction
44     extends BreakpointRequestAction {
45
46     protected String JavaDoc sourceName;
47     protected int line;
48
49     public SourceLineBreakpointRequestAction(Debugger debugger,
50                                              String JavaDoc sourceName, int line) {
51         super(debugger);
52         System.err.println(">> source line request: " + sourceName + " : " + line);
53         this.sourceName = ensureDriveIsUpperCase(sourceName);
54         this.line = line;
55     }
56
57     EventRequest resolve(ReferenceType refType) throws MultipleLocationsException,
58                                                         UnableToSetRequestException {
59         BreakpointRequest request = null;
60         try {
61             String JavaDoc refTypeSourceName = refType.sourceName();
62             String JavaDoc strippedName = ajdbg().strip(getSourceName());
63             if (refTypeSourceName.equals(strippedName)) {
64                 EventRequestManager em = vm().eventRequestManager();
65                 Location location = findLocation(refType);
66                 if (location == null) {
67                     return null;
68                 }
69                 request = request(location);
70             }
71         } catch (NoVMException e) {
72         } catch (AbsentInformationException aie) {
73         }
74         return request;
75     }
76
77     /**
78      * Exceptions were being thrown if c:\... was used as a file path instead of
79      * C:\... This method makes sure sourceName has an upper case drive letter.
80      */

81     private String JavaDoc ensureDriveIsUpperCase(String JavaDoc sourceName) {
82         try {
83             if (Character.isLetter(sourceName.charAt(0)) &&
84                 sourceName.charAt(1) == ':' &&
85                 isFileSeparator(sourceName.charAt(2))) {
86                 return Character.toUpperCase(sourceName.charAt(0)) +
87                        sourceName.substring(1);
88             }
89         } catch (ArrayIndexOutOfBoundsException JavaDoc aoobe) {
90         }
91         return sourceName;
92     }
93
94     private boolean isFileSeparator(char c) {
95         return c == File.separatorChar || c == '/' || c == '\\';
96     }
97
98     private Location findLocation(ReferenceType refType)
99         throws MultipleLocationsException,
100                UnableToSetRequestException {
101         Location location = null;
102         try {
103             List lines = refType.locationsOfLine(line);
104             if (lines == null || lines.size() == 0) {
105
106                 //TODO
107
//Need a better way to find if a class matches the
108
//source desired.
109

110                 //throw new UnableToSetRequestException(this);
111
return null;
112             }
113             location = (Location) lines.get(0);
114         } catch (AbsentInformationException aie) {
115             ajdbg().exception("Unable to set " + this + " : No code at line " +
116                               line + " in " + refType.name(), aie);
117         } catch (ClassNotPreparedException cpe) {
118             ajdbg().classNotPreparedException(cpe, refType.name());
119         } catch (ObjectCollectedException oce) {
120             ajdbg().objectCollectedException(oce, refType.name());
121         } catch (InvalidLineNumberException ilne){
122             ajdbg().invalidLineNumberException(ilne, refType.name(), line);
123         }
124         return location;
125     }
126
127     public int getLine() {
128         return line;
129     }
130
131     public String JavaDoc getSourceName() {
132         return sourceName;
133     }
134
135     public int getRealLine() {
136         SourceLine sl = sourceLine();
137         if (sl == null || sl.line < 0) {
138             return super.getRealLine();
139         }
140         return sl.line;
141     }
142
143     public String JavaDoc getRealSourceName() {
144         SourceLine sl = sourceLine();
145         if (sl == null) {
146             return super.getRealSourceName();
147         }
148         return sl.filename;
149     }
150
151     public SourceLine sourceLine() {
152         return ajdbg().getSourceLineFromSource(getSourceName(), getLine());
153     }
154
155     public String JavaDoc getProto() {
156         SourceLine sl = ajdbg().getSourceLineFromSource(getSourceName(), getLine());
157         String JavaDoc newSource = "<source-unkown>";
158         if (sl != null) {
159             newSource = ajdbg().strip(ajdbg().removeFullWorkingDir(sl.filename));
160         } else {
161             System.out.println("sl is NULL: " + sourceName + ":" + getLine());
162         }
163         return newSource + ":" + sl.line;
164     }
165
166     public boolean equals(Object JavaDoc other) {
167         if (other == null || !(other instanceof SourceLineBreakpointRequestAction)) {
168             return super.equals(other);
169         }
170         SourceLineBreakpointRequestAction ba = (SourceLineBreakpointRequestAction) other;
171         return (getSourceName().equals(ba.getSourceName()) &&
172                 getLine() == ba.getLine());
173 // return (getSourceName() != null && ba.getSourceName() != null &&
174
// getSourceName().equals(ba.getSourceName()) &&
175
// getLine() == ba.getLine());
176
}
177
178     public String JavaDoc getErrorMessage() {
179         SourceLine sl = ajdbg().getSourceLineFromSource(getSourceName(), getLine());
180         String JavaDoc newSourceName = getSourceName();
181         int newLine = getLine();
182         if (sl != null) {
183             newSourceName = ajdbg().removeSourcePath(sl.filename);
184             newLine = sl.line;
185         }
186         return "No code at line " + newLine + " in file " + newSourceName;
187     }
188 }
189
Popular Tags