KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.jdi.event.*;
39
40 import java.util.Hashtable JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 import java.io.*;
45
46 /** Keeps track of DocumentDebugActions that are waiting to be resolved when the classes they corresponed to are
47  * prepared. (Only DocumentDebugActions have reference types which can be prepared.)
48  * @version $Id: PendingRequestManager.java 3856 2006-05-24 06:18:08Z rcartwright $
49  */

50
51 public class PendingRequestManager {
52   private JPDADebugger _manager;
53   private Hashtable JavaDoc<String JavaDoc, Vector JavaDoc<DocumentDebugAction<?>>> _pendingActions;
54
55   public PendingRequestManager(JPDADebugger manager) {
56     _manager = manager;
57     _pendingActions = new Hashtable JavaDoc<String JavaDoc, Vector JavaDoc<DocumentDebugAction<?>>>();
58   }
59
60   /** Called if a breakpoint is set before its class is prepared
61    * @param action The DebugAction that is pending
62    */

63   public void addPendingRequest (DocumentDebugAction<?> action) {
64     String JavaDoc className = action.getClassName();
65     Vector JavaDoc<DocumentDebugAction<?>> actions = _pendingActions.get(className);
66     if (actions == null) {
67       actions = new Vector JavaDoc<DocumentDebugAction<?>>();
68
69       // only create a ClassPrepareRequest once per class
70
ClassPrepareRequest request =
71         _manager.getEventRequestManager().createClassPrepareRequest();
72       // Listen for events from the class, and also its inner classes
73
request.addClassFilter(className + "*");
74       request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
75       request.enable();
76       //System.out.println("Creating prepareRequest in class " + className);
77
}
78     actions.add(action);
79     _pendingActions.put(className, actions);
80   }
81
82   /**
83    * Called if a breakpoint is set and removed before its class is prepared
84    * @param action The DebugAction that was set and removed
85    */

86   public void removePendingRequest (DocumentDebugAction<?> action) {
87     String JavaDoc className = action.getClassName();
88     Vector JavaDoc<DocumentDebugAction<?>> actions = _pendingActions.get(className);
89     if (actions == null) {
90       return;
91     }
92     actions.remove(action);
93     // check if the vector is empty
94
if (actions.size() == 0) {
95       _pendingActions.remove(className);
96     }
97   }
98
99   /** Recursively look through all nested types to see if the line number exists.
100    * @param lineNumber line number to look for
101    * @param rt reference type to start at
102    * @return true if line number is found
103    */

104   private boolean recursiveFindLineNumber(int lineNumber, ReferenceType rt) {
105     try {
106       for(Location l: rt.allLineLocations()) {
107         if (l.lineNumber()==lineNumber) { return true; }
108       }
109       for(ReferenceType nested: rt.nestedTypes()) {
110         if (recursiveFindLineNumber(lineNumber, nested)==true) { return true; }
111       }
112     }
113     catch (AbsentInformationException aie) {
114       // ignore, return false
115
}
116     
117     return false;
118   }
119   
120   /**
121    * Called by the EventHandler whenever a ClassPrepareEvent occurs.
122    * This will take the event, get the class that was prepared, lookup
123    * the Vector of DebugAction that was waiting for this class's preparation,
124    * iterate through this Vector, and attempt to create the Breakpoints that
125    * were pending. Since the keys to the HashTable are the names of the
126    * outer class, the $ and everything after it must be cropped off from the
127    * class name in order to do the lookup. During the lookup, however, the line
128    * number of each action is checked to see if the line number is contained
129    * in the given event's ReferenceType. If not, we ignore that pending action
130    * since it is not in the class that was just prepared, but may be in one of its
131    * inner classes.
132    * @param event The ClassPrepareEvent that just occured
133    */

134   public void classPrepared (ClassPrepareEvent event) throws DebugException {
135     ReferenceType rt = event.referenceType();
136     //DrJava.consoleOut().println("In classPrepared. rt: " + rt);
137
//DrJava.consoleOut().println("equals getReferenceType: " +
138
// rt.equals(_manager.getReferenceType(rt.name())));
139
String JavaDoc className = rt.name();
140
141     // crop off the $ if there is one and anything after it
142
int indexOfDollar = className.indexOf('$');
143     if (indexOfDollar > 1) {
144       className = className.substring(0, indexOfDollar);
145     }
146
147     // Get the pending actions for this class (and inner classes)
148
Vector JavaDoc<DocumentDebugAction<?>> actions = _pendingActions.get(className);
149     Vector JavaDoc<DocumentDebugAction<?>> failedActions =
150       new Vector JavaDoc<DocumentDebugAction<?>>();
151     //DrJava.consoleOut().println("pending actions: " + actions);
152
if (actions == null) {
153       // Must have been a different class with a matching prefix, ignore it
154
// since we're not interested in this class.
155
return;
156     }
157     else if (actions.isEmpty()) {
158       // any actions that were waiting for this class to be prepared have been
159
// removed
160
_manager.getEventRequestManager().deleteEventRequest(event.request());
161       return;
162     }
163     for (int i = 0; i < actions.size(); i++) {
164       DocumentDebugAction<?> a = actions.get(i);
165       int lineNumber = a.getLineNumber();
166       if (lineNumber != DebugAction.ANY_LINE) {
167         try {
168           List JavaDoc lines = rt.locationsOfLine(lineNumber);
169           if (lines.size() == 0) {
170             // Do not disable action; the line number might just be in another class in the same file
171
String JavaDoc exactClassName = a.getExactClassName();
172             if ((exactClassName!=null) && (exactClassName.equals(rt.name()))) {
173               _manager.printMessage(actions.get(i).toString()+" not on an executable line; disabled.");
174               actions.get(i).setEnabled(false);
175             }
176
177             // Requested line number not in reference type, skip this action
178
continue;
179           }
180         }
181         catch (AbsentInformationException aie) {
182           // outer class has no line number info, skip this action
183
continue;
184         }
185       }
186       // check if the action was successfully created
187
try {
188         Vector JavaDoc<ReferenceType> refTypes = new Vector JavaDoc<ReferenceType>();
189         refTypes.add(rt);
190         a.createRequests(refTypes); // This type warning will go away in JDK 1.5
191
}
192       catch (DebugException e) {
193         failedActions.add(a);
194         // DrJava.consoleOut().println("Exception preparing request!! " + e);
195
}
196     }
197
198     // For debugging purposes
199
/*
200     List l = _manager.getEventRequestManager().breakpointRequests();
201     System.out.println("list of eventrequestmanager's breakpointRequests: " +
202                        l);
203     for (int i = 0; i < l.size(); i++) {
204       BreakpointRequest br = (BreakpointRequest)l.get(i);
205       System.out.println("isEnabled(): " + br.isEnabled() +
206                          " suspendPolicy(): " + br.suspendPolicy() +
207                          " location(): " + br.location());
208     }
209     */

210     if (failedActions.size() > 0) {
211       // need to create an exception framework
212
throw new DebugException("Failed actions: " + failedActions);
213     }
214   }
215 }
216
Popular Tags