KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.jdi.*;
37 import com.sun.jdi.request.*;
38
39 import java.util.Vector JavaDoc;
40 import java.util.List JavaDoc;
41 import java.io.File JavaDoc;
42
43 import edu.rice.cs.drjava.model.*;
44 import edu.rice.cs.drjava.model.definitions.ClassNameNotFoundException;
45 import javax.swing.text.BadLocationException JavaDoc;
46
47 /** Superclasses all DebugActions that are associated with specific OpenDefinitionsDocuments.
48  * @version $Id: DocumentDebugAction.java 4011 2006-09-22 15:31:42Z rcartwright $
49  */

50 public abstract class DocumentDebugAction<T extends EventRequest> extends DebugAction<T> {
51   
52   protected volatile String JavaDoc _className;
53   protected volatile String JavaDoc _exactClassName;
54   protected volatile File JavaDoc _file;
55   protected volatile OpenDefinitionsDocument _doc;
56   protected int _offset;
57   
58   
59   /** Creates a new DocumentDebugAction. Automatically tries to create the EventRequest if a ReferenceType can be
60     * found, or else adds this object to the PendingRequestManager. Any subclass should automatically call
61     * _initializeRequest in its constructor.
62     * @param manager JPDADebugger in charge
63     * @param doc Document this action corresponds to
64     * @param offset Offset into the document that the action affects
65     */

66   public DocumentDebugAction (JPDADebugger manager, OpenDefinitionsDocument doc, int offset) throws DebugException {
67     super(manager);
68     _exactClassName = null;
69     try {
70       if (offset >= 0) {
71         if (doc.getNumberOfLines() < 500) {
72           // only do this on short files
73
// in long files, getEnclosingClassName might take too long
74
_exactClassName = doc.getEnclosingClassName(offset, true);
75         }
76       }
77     }
78     catch(ClassNameNotFoundException cnnfe) { /* ignore, we don't need the exact class name */ }
79     catch(BadLocationException JavaDoc ble) { /* ignore, we don't need the exact class name */ }
80     try {
81       if (offset >= 0) {
82         _className = doc.getQualifiedClassName(offset);
83       }
84     }
85     catch (ClassNameNotFoundException cnnfe) {
86       // Still couldn't find a class name, use ""
87
_className = "";
88     }
89     // System.out.println("Breakpoint added: "+_className+", exact="+_exactClassName);
90

91     try {
92       _file = doc.getFile();
93       if (_file == null) throw new DebugException("This document has no source file.");
94     }
95     catch (FileMovedException fme) {
96       throw new DebugException("This document's file no longer exists: " + fme.getMessage());
97     }
98     _doc = doc;
99     _offset = offset;
100   }
101   
102   /** Returns the class name this DebugAction occurs in. */
103   public String JavaDoc getClassName() { return _className; }
104   
105   /** Returns the file this DebugAction occurs in. */
106   public File JavaDoc getFile() { return _file; }
107   
108   /** Returns the document this DebugAction occurs in. */
109   public OpenDefinitionsDocument getDocument() { return _doc; }
110   
111   /** @return offset of this debug action. */
112   public int getOffset() { return _offset; }
113   
114   /** @return exact class name, or null if not available. */
115   public String JavaDoc getExactClassName() { return _exactClassName; }
116   
117   /** Creates EventRequests corresponding to this DebugAction, using the given ReferenceTypes. This is called either
118    * from the DebugAction constructor or the PendingRequestManager, depending on when the ReferenceTypes become
119    * available. (There may be multiple reference types for the same class if a custom class loader is used.)
120    * @return true if the EventRequest is successfully created
121    */

122   public boolean createRequests(Vector JavaDoc<ReferenceType> refTypes) throws DebugException {
123     _createRequests(refTypes);
124     if (_requests.size() > 0) {
125       _prepareRequests(_requests);
126       return true;
127     }
128     else return false;
129   }
130   
131   /** This should always be called from the constructor of the subclass. Attempts to create EventRequests on the
132    * given ReferenceTypes, and also adds this action to the pending request manager (so identical classes loaded
133    * in the future will also have this action).
134    */

135   protected void _initializeRequests(Vector JavaDoc<ReferenceType> refTypes) throws DebugException {
136     if (refTypes.size() > 0) createRequests(refTypes);
137     else {
138       if (_exactClassName!=null) {
139         List JavaDoc<ReferenceType> referenceTypes = _manager.getVM().classesByName(_exactClassName);
140         if (referenceTypes.size()>0) {
141           // class has been loaded, but couldn't find this line number
142
throw new LineNotExecutableException("Cannot set breakpoint, line "+getLineNumber()+" is not an executable line.");
143         }
144       }
145     }
146     //if (_request == null) {
147
// couldn't create the request yet, add to the pending request manager
148

149     // Experiment: always add to pending request, to deal with multpile class loads
150
_manager.getPendingRequestManager().addPendingRequest(this);
151     //}
152
}
153   
154   /** Creates appropriate EventRequests from the EventRequestManager and stores them in the _requests field.
155    * @param refTypes All (identical) ReferenceTypes to which this action applies. (There may be multiple if a custom
156    * class loader is in use.)
157    * @throws DebugException if the requests could not be created.
158    */

159   protected abstract void _createRequests(Vector JavaDoc<ReferenceType> refTypes) throws DebugException;
160   
161   /** Prepares this EventRequest with the current stored values.
162    * @param request the EventRequest to prepare
163    */

164   protected void _prepareRequest(T request) {
165     super._prepareRequest(request);
166     request.putProperty("document", _doc);
167   }
168 }
169
Popular Tags