001: /*
002: * LineNumberBreakpoint.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: LineNumberBreakpoint.java,v 1.3 2003/05/18 01:31:26 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.jdb;
023:
024: import com.sun.jdi.Location;
025: import com.sun.jdi.ReferenceType;
026: import com.sun.jdi.request.EventRequest;
027: import com.sun.jdi.request.EventRequestManager;
028: import java.util.List;
029: import org.armedbear.j.Annotation;
030: import org.armedbear.j.Buffer;
031: import org.armedbear.j.Debug;
032: import org.armedbear.j.Editor;
033: import org.armedbear.j.File;
034: import org.armedbear.j.FastStringBuffer;
035: import org.armedbear.j.JavaSource;
036: import org.armedbear.j.Line;
037: import org.armedbear.j.Log;
038: import org.armedbear.j.Utilities;
039:
040: public final class LineNumberBreakpoint extends ResolvableBreakpoint {
041: private Buffer buffer;
042: private final int lineNumber;
043:
044: public LineNumberBreakpoint(Jdb jdb, Buffer buffer, Line line) {
045: super (jdb);
046: this .buffer = buffer;
047: this .line = line;
048: file = buffer.getFile();
049: final String fileName = file.getName();
050: String name = fileName.substring(0, fileName.length() - 5);
051: String packageName = JavaSource.getPackageName(buffer);
052: if (packageName != null)
053: className = packageName.concat(".").concat(name);
054: else
055: className = name;
056: Log.debug("LineNumberBreakpoint className = |" + className
057: + "|");
058: // Our line numbers are zero-based.
059: lineNumber = line.lineNumber() + 1;
060: }
061:
062: public LineNumberBreakpoint(Jdb jdb, String className, File file,
063: int lineNumber) {
064: super (jdb);
065: this .className = className;
066: this .file = file;
067: this .lineNumber = lineNumber;
068: }
069:
070: public int getLineNumber() {
071: return lineNumber;
072: }
073:
074: public EventRequest resolveEventRequest(ReferenceType refType)
075: throws Exception {
076: Log.debug("LineNumberBreakpoint.resolveEventRequest");
077: Location location = findLocation(refType, lineNumber);
078: if (location == null) {
079: Log.debug("resolveEventRequest location is null");
080: return null;
081: }
082: EventRequestManager erm = refType.virtualMachine()
083: .eventRequestManager();
084: EventRequest er = erm.createBreakpointRequest(location);
085: er.setSuspendPolicy(EventRequest.SUSPEND_ALL);
086: er.enable();
087: return er;
088: }
089:
090: private Location findLocation(ReferenceType refType, int lineNumber)
091: throws Exception {
092: Location location = null;
093: List locations = refType.locationsOfLine(lineNumber);
094: if (locations.size() > 0) {
095: location = (Location) locations.get(0);
096: if (location.method() != null)
097: return location;
098: }
099: return null;
100: }
101:
102: public void resolved() {
103: if (line != null) {
104: line.setAnnotation(new BreakpointAnnotation(this ));
105: } else {
106: // This only finds existing buffers. We might want to create one
107: // if one doesn't already exist for the file in question.
108: Buffer buffer = Editor.getBufferList().findBuffer(file);
109: if (buffer != null) {
110: if (!buffer.initialized())
111: buffer.initialize();
112: if (!buffer.isLoaded())
113: buffer.load();
114: line = buffer.getLine(lineNumber - 1);
115: if (line != null) {
116: line.setAnnotation(new BreakpointAnnotation(this ));
117: buffer.repaint();
118: }
119: }
120: }
121: if (buffer != null)
122: buffer.repaint();
123: jdb.log("Breakpoint resolved: " + getLocationString());
124: }
125:
126: public String getLocationString() {
127: FastStringBuffer sb = new FastStringBuffer();
128: if (file != null) {
129: sb.append(file.getName());
130: sb.append(':');
131: }
132: sb.append(lineNumber);
133: if (!isResolved())
134: sb.append(' ');
135: return sb.toString();
136: }
137:
138: public String toString() {
139: FastStringBuffer sb = new FastStringBuffer();
140: if (file != null) {
141: sb.append(file.getName());
142: sb.append(':');
143: }
144: sb.append(lineNumber);
145: if (!isResolved()) {
146: sb.append(' ');
147: sb.append("(deferred)");
148: }
149: return sb.toString();
150: }
151:
152: public String toXml() {
153: int indent = 4;
154: final String separator = System.getProperty("line.separator");
155: FastStringBuffer sb = new FastStringBuffer(Utilities
156: .spaces(indent));
157: sb.append("<breakpoint");
158: sb.append(separator);
159: if (className == null)
160: Debug.bug();
161: if (className != null) {
162: sb.append(Utilities.spaces(indent + 2));
163: sb.append("className=\"");
164: sb.append(className);
165: sb.append('"');
166: sb.append(separator);
167: }
168: if (file != null) {
169: sb.append(Utilities.spaces(indent + 2));
170: sb.append("fileName=\"");
171: sb.append(file.canonicalPath());
172: sb.append('"');
173: sb.append(separator);
174: }
175: if (lineNumber > 0) {
176: sb.append(Utilities.spaces(indent + 2));
177: sb.append("lineNumber=\"");
178: sb.append(String.valueOf(lineNumber));
179: sb.append('"');
180: sb.append(separator);
181: }
182: sb.append(Utilities.spaces(indent));
183: sb.append("/>");
184: sb.append(separator);
185: return sb.toString();
186: }
187: }
|