001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.debug.jpda;
038:
039: import edu.rice.cs.drjava.model.DocumentRegion;
040: import edu.rice.cs.util.UnexpectedException;
041: import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
042: import edu.rice.cs.drjava.model.debug.Breakpoint;
043: import edu.rice.cs.drjava.model.debug.DebugException;
044:
045: import java.util.Vector;
046: import java.util.List;
047: import javax.swing.text.BadLocationException;
048: import javax.swing.text.Position;
049: import java.io.*;
050:
051: import com.sun.jdi.*;
052: import com.sun.jdi.request.*;
053:
054: /** The breakpoint object which has references to its OpenDefinitionsDocument and its BreakpointRequest. */
055: public class JPDABreakpoint extends
056: DocumentDebugAction<BreakpointRequest> implements Breakpoint {
057:
058: private volatile Position _startPos;
059: private volatile Position _endPos;
060:
061: /** @throws DebugException if the document does not have a file */
062: public JPDABreakpoint(OpenDefinitionsDocument doc, int offset,
063: int lineNumber, boolean isEnabled, JPDADebugger manager)
064: throws DebugException {
065:
066: super (manager, doc, offset);
067: _suspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
068: _lineNumber = lineNumber;
069: _isEnabled = isEnabled;
070:
071: try {
072: _startPos = doc.createPosition(doc.getLineStartPos(offset));
073: _endPos = doc.createPosition(doc.getLineEndPos(offset));
074: } catch (BadLocationException ble) {
075: throw new UnexpectedException(ble);
076: }
077:
078: if ((_manager != null) && (_manager.isReady())) {
079: // the debugger is on, so initialize now
080: // otherwise breakpoint gets re-set when debugger is enabled
081: Vector<ReferenceType> refTypes = _manager
082: .getReferenceTypes(_className, _lineNumber);
083: _initializeRequests(refTypes);
084: setEnabled(isEnabled);
085: }
086: }
087:
088: /** Creates appropriate EventRequests from the EventRequestManager and
089: * stores them in the _requests field.
090: * @param refTypes All (identical) ReferenceTypes to which this action
091: * applies. (There may be multiple if a custom class loader is in use.)
092: * @throws DebugException if the requests could not be created.
093: */
094: protected void _createRequests(Vector<ReferenceType> refTypes)
095: throws DebugException {
096: try {
097: for (int i = 0; i < refTypes.size(); i++) {
098: ReferenceType rt = refTypes.get(i);
099:
100: if (!rt.isPrepared()) {
101: // Not prepared, so skip this one
102: continue;
103: }
104:
105: // Get locations for the line number, use the first
106: List lines = rt.locationsOfLine(_lineNumber);
107: if (lines.size() == 0) {
108: // Can't find a location on this line
109: setEnabled(false);
110: throw new DebugException(
111: "Could not find line number: "
112: + _lineNumber);
113: }
114: Location loc = (Location) lines.get(0);
115:
116: BreakpointRequest request = _manager
117: .getEventRequestManager()
118: .createBreakpointRequest(loc);
119: request.setEnabled(_isEnabled);
120: _requests.add(request);
121: }
122: } catch (AbsentInformationException aie) {
123: throw new DebugException("Could not find line number: "
124: + aie);
125: }
126: }
127:
128: /**
129: * Accessor for the offset of this breakpoint's start position
130: * @return the start offset
131: */
132: public int getStartOffset() {
133: return _startPos.getOffset();
134: }
135:
136: /** Accessor for the offset of this breakpoint's end position
137: * @return the end offset
138: */
139: public int getEndOffset() {
140: return _endPos.getOffset();
141: }
142:
143: /** Enable/disable the breakpoint. */
144: public void setEnabled(boolean isEnabled) {
145: boolean old = _isEnabled;
146: super .setEnabled(isEnabled);
147: try {
148: for (BreakpointRequest bpr : _requests) {
149: bpr.setEnabled(isEnabled);
150: }
151: } catch (VMDisconnectedException vmde) { /* just ignore */
152: }
153: if (_isEnabled != old)
154: _manager.notifyBreakpointChange(this );
155: }
156:
157: public String toString() {
158: String cn = getClassName();
159: if (_exactClassName != null) {
160: cn = _exactClassName.replace('$', '.');
161: }
162: if (_requests.size() > 0) {
163: // All BreakpointRequests are identical-- one copy for each loaded
164: // class. So just print info from the first one, and how many there are.
165: return "Breakpoint[class: " + cn + ", lineNumber: "
166: + getLineNumber() + ", method: "
167: + _requests.get(0).location().method()
168: + ", codeIndex: "
169: + _requests.get(0).location().codeIndex()
170: + ", numRefTypes: " + _requests.size() + "]";
171: } else {
172: return "Breakpoint[class: " + cn + ", lineNumber: "
173: + getLineNumber() + "]";
174: }
175: }
176: }
|