KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > jdb > LineNumberBreakpoint


1 /*
2  * LineNumberBreakpoint.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: LineNumberBreakpoint.java,v 1.3 2003/05/18 01:31:26 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.jdb;
23
24 import com.sun.jdi.Location;
25 import com.sun.jdi.ReferenceType;
26 import com.sun.jdi.request.EventRequest;
27 import com.sun.jdi.request.EventRequestManager;
28 import java.util.List JavaDoc;
29 import org.armedbear.j.Annotation;
30 import org.armedbear.j.Buffer;
31 import org.armedbear.j.Debug;
32 import org.armedbear.j.Editor;
33 import org.armedbear.j.File;
34 import org.armedbear.j.FastStringBuffer;
35 import org.armedbear.j.JavaSource;
36 import org.armedbear.j.Line;
37 import org.armedbear.j.Log;
38 import org.armedbear.j.Utilities;
39
40 public final class LineNumberBreakpoint extends ResolvableBreakpoint
41 {
42     private Buffer buffer;
43     private final int lineNumber;
44
45     public LineNumberBreakpoint(Jdb jdb, Buffer buffer, Line line)
46     {
47         super(jdb);
48         this.buffer = buffer;
49         this.line = line;
50         file = buffer.getFile();
51         final String JavaDoc fileName = file.getName();
52         String JavaDoc name = fileName.substring(0, fileName.length()-5);
53         String JavaDoc packageName = JavaSource.getPackageName(buffer);
54         if (packageName != null)
55             className = packageName.concat(".").concat(name);
56         else
57             className = name;
58         Log.debug("LineNumberBreakpoint className = |" + className + "|");
59         // Our line numbers are zero-based.
60
lineNumber = line.lineNumber() + 1;
61     }
62
63     public LineNumberBreakpoint(Jdb jdb, String JavaDoc className, File file,
64         int lineNumber)
65     {
66         super(jdb);
67         this.className = className;
68         this.file = file;
69         this.lineNumber = lineNumber;
70     }
71
72     public int getLineNumber()
73     {
74         return lineNumber;
75     }
76
77     public EventRequest resolveEventRequest(ReferenceType refType)
78         throws Exception JavaDoc
79     {
80         Log.debug("LineNumberBreakpoint.resolveEventRequest");
81         Location location = findLocation(refType, lineNumber);
82         if (location == null) {
83             Log.debug("resolveEventRequest location is null");
84             return null;
85         }
86         EventRequestManager erm =
87             refType.virtualMachine().eventRequestManager();
88         EventRequest er = erm.createBreakpointRequest(location);
89         er.setSuspendPolicy(EventRequest.SUSPEND_ALL);
90         er.enable();
91         return er;
92     }
93
94     private Location findLocation(ReferenceType refType, int lineNumber)
95         throws Exception JavaDoc
96     {
97         Location location = null;
98         List JavaDoc locations = refType.locationsOfLine(lineNumber);
99         if (locations.size() > 0) {
100             location = (Location) locations.get(0);
101             if (location.method() != null)
102                 return location;
103         }
104         return null;
105     }
106
107     public void resolved()
108     {
109         if (line != null) {
110             line.setAnnotation(new BreakpointAnnotation(this));
111         } else {
112             // This only finds existing buffers. We might want to create one
113
// if one doesn't already exist for the file in question.
114
Buffer buffer = Editor.getBufferList().findBuffer(file);
115             if (buffer != null) {
116                 if (!buffer.initialized())
117                     buffer.initialize();
118                 if (!buffer.isLoaded())
119                     buffer.load();
120                 line = buffer.getLine(lineNumber - 1);
121                 if (line != null) {
122                     line.setAnnotation(new BreakpointAnnotation(this));
123                     buffer.repaint();
124                 }
125             }
126         }
127         if (buffer != null)
128             buffer.repaint();
129         jdb.log("Breakpoint resolved: " + getLocationString());
130     }
131
132     public String JavaDoc getLocationString()
133     {
134         FastStringBuffer sb = new FastStringBuffer();
135         if (file != null) {
136             sb.append(file.getName());
137             sb.append(':');
138         }
139         sb.append(lineNumber);
140         if (!isResolved())
141             sb.append(' ');
142         return sb.toString();
143     }
144
145     public String JavaDoc toString()
146     {
147         FastStringBuffer sb = new FastStringBuffer();
148         if (file != null) {
149             sb.append(file.getName());
150             sb.append(':');
151         }
152         sb.append(lineNumber);
153         if (!isResolved()) {
154             sb.append(' ');
155             sb.append("(deferred)");
156         }
157         return sb.toString();
158     }
159
160     public String JavaDoc toXml()
161     {
162         int indent = 4;
163         final String JavaDoc separator = System.getProperty("line.separator");
164         FastStringBuffer sb = new FastStringBuffer(Utilities.spaces(indent));
165         sb.append("<breakpoint");
166         sb.append(separator);
167         if (className == null)
168             Debug.bug();
169         if (className != null) {
170             sb.append(Utilities.spaces(indent+2));
171             sb.append("className=\"");
172             sb.append(className);
173             sb.append('"');
174             sb.append(separator);
175         }
176         if (file!= null) {
177             sb.append(Utilities.spaces(indent+2));
178             sb.append("fileName=\"");
179             sb.append(file.canonicalPath());
180             sb.append('"');
181             sb.append(separator);
182         }
183         if (lineNumber > 0) {
184             sb.append(Utilities.spaces(indent+2));
185             sb.append("lineNumber=\"");
186             sb.append(String.valueOf(lineNumber));
187             sb.append('"');
188             sb.append(separator);
189         }
190         sb.append(Utilities.spaces(indent));
191         sb.append("/>");
192         sb.append(separator);
193         return sb.toString();
194     }
195 }
196
Popular Tags