KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > debug > Breakpoint


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.debug;
35
36 import edu.rice.cs.drjava.model.DocumentRegion;
37 import edu.rice.cs.util.UnexpectedException;
38 import edu.rice.cs.drjava.model.OpenDefinitionsDocument;
39
40 import java.util.Vector JavaDoc;
41 import java.util.List JavaDoc;
42
43 import javax.swing.text.BadLocationException JavaDoc;
44 import javax.swing.text.Position JavaDoc;
45
46 import com.sun.jdi.*;
47 import com.sun.jdi.request.*;
48
49 import java.io.*;
50
51 /** The breakpoint object which has references to its OpenDefinitionsDocument and its BreakpointRequest. */
52 public class Breakpoint extends DocumentDebugAction<BreakpointRequest> implements DebugBreakpointData, DocumentRegion {
53
54    private volatile Position JavaDoc _startPos;
55    private volatile Position JavaDoc _endPos;
56
57   /** @throws DebugException if the document does not have a file */
58   public Breakpoint(OpenDefinitionsDocument doc, int offset, int lineNumber, boolean isEnabled, JPDADebugger manager)
59     throws DebugException {
60
61     super(manager, doc, offset);
62     _suspendPolicy = EventRequest.SUSPEND_EVENT_THREAD;
63     _lineNumber = lineNumber;
64     _isEnabled = isEnabled;
65
66     try {
67       _startPos = doc.createPosition(doc.getLineStartPos(offset));
68       _endPos = doc.createPosition(doc.getLineEndPos(offset));
69     }
70     catch (BadLocationException JavaDoc ble) {
71       throw new UnexpectedException(ble);
72     }
73
74     if ((_manager != null) && (_manager.isReady())) {
75       // the debugger is on, so initialize now
76
// otherwise breakpoint gets re-set when debugger is enabled
77
Vector JavaDoc<ReferenceType> refTypes = _manager.getReferenceTypes(_className, _lineNumber);
78       _initializeRequests(refTypes);
79       setEnabled(isEnabled);
80     }
81   }
82   
83   /** Creates appropriate EventRequests from the EventRequestManager and
84    * stores them in the _requests field.
85    * @param refTypes All (identical) ReferenceTypes to which this action
86    * applies. (There may be multiple if a custom class loader is in use.)
87    * @throws DebugException if the requests could not be created.
88    */

89   protected void _createRequests(Vector JavaDoc<ReferenceType> refTypes) throws DebugException {
90     try {
91       for (int i=0; i < refTypes.size(); i++) {
92         ReferenceType rt = refTypes.get(i);
93
94         if (!rt.isPrepared()) {
95           // Not prepared, so skip this one
96
continue;
97         }
98
99         // Get locations for the line number, use the first
100
List JavaDoc lines = rt.locationsOfLine(_lineNumber);
101         if (lines.size() == 0) {
102           // Can't find a location on this line
103
setEnabled(false);
104           throw new DebugException("Could not find line number: " + _lineNumber);
105         }
106         Location loc = (Location) lines.get(0);
107         
108         BreakpointRequest request = _manager.getEventRequestManager().createBreakpointRequest(loc);
109         request.setEnabled(_isEnabled);
110         _requests.add(request);
111       }
112     }
113     catch (AbsentInformationException aie) {
114       throw new DebugException("Could not find line number: " + aie);
115     }
116   }
117
118   /**
119    * Accessor for the offset of this breakpoint's start position
120    * @return the start offset
121    */

122   public int getStartOffset() {
123     return _startPos.getOffset();
124   }
125
126   /** Accessor for the offset of this breakpoint's end position
127    * @return the end offset
128    */

129   public int getEndOffset() {
130     return _endPos.getOffset();
131   }
132   
133   /** Enable/disable the breakpoint. */
134   public void setEnabled(boolean isEnabled) {
135     boolean old = _isEnabled;
136     super.setEnabled(isEnabled);
137     try {
138       for(BreakpointRequest bpr: _requests) {
139         bpr.setEnabled(isEnabled);
140       }
141     }
142     catch(VMDisconnectedException vmde) { /* just ignore */ }
143     if (_isEnabled!=old) _manager.notifyBreakpointChange(this);
144   }
145
146   public String JavaDoc toString() {
147     String JavaDoc cn = getClassName();
148     if (_exactClassName!=null) { cn = _exactClassName.replace('$', '.'); }
149     if (_requests.size() > 0) {
150       // All BreakpointRequests are identical-- one copy for each loaded
151
// class. So just print info from the first one, and how many there are.
152
return "Breakpoint[class: " + cn +
153         ", lineNumber: " + getLineNumber() +
154         ", method: " + _requests.get(0).location().method() +
155         ", codeIndex: " + _requests.get(0).location().codeIndex() +
156         ", numRefTypes: " + _requests.size() + "]";
157     }
158     else {
159       return "Breakpoint[class: " + cn +
160         ", lineNumber: " + getLineNumber() + "]";
161     }
162   }
163 }
164
Popular Tags