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 com.sun.jdi.*;
040: import com.sun.jdi.request.*;
041: import com.sun.jdi.event.*;
042:
043: import java.util.Hashtable;
044: import java.util.List;
045: import java.util.Vector;
046: import java.io.*;
047:
048: import edu.rice.cs.drjava.model.debug.DebugException;
049:
050: /** Keeps track of DocumentDebugActions that are waiting to be resolved when the classes they corresponed to are
051: * prepared. (Only DocumentDebugActions have reference types which can be prepared.)
052: * @version $Id: PendingRequestManager.java 4255 2007-08-28 19:17:37Z mgricken $
053: */
054:
055: public class PendingRequestManager {
056: private JPDADebugger _manager;
057: private Hashtable<String, Vector<DocumentDebugAction<?>>> _pendingActions;
058:
059: public PendingRequestManager(JPDADebugger manager) {
060: _manager = manager;
061: _pendingActions = new Hashtable<String, Vector<DocumentDebugAction<?>>>();
062: }
063:
064: /** Called if a breakpoint is set before its class is prepared
065: * @param action The DebugAction that is pending
066: */
067: public void addPendingRequest(DocumentDebugAction<?> action) {
068: String className = action.getClassName();
069: Vector<DocumentDebugAction<?>> actions = _pendingActions
070: .get(className);
071: if (actions == null) {
072: actions = new Vector<DocumentDebugAction<?>>();
073:
074: // only create a ClassPrepareRequest once per class
075: ClassPrepareRequest request = _manager
076: .getEventRequestManager()
077: .createClassPrepareRequest();
078: // Listen for events from the class, and also its inner classes
079: request.addClassFilter(className + "*");
080: request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
081: request.enable();
082: //System.out.println("Creating prepareRequest in class " + className);
083: }
084: actions.add(action);
085: _pendingActions.put(className, actions);
086: }
087:
088: /**
089: * Called if a breakpoint is set and removed before its class is prepared
090: * @param action The DebugAction that was set and removed
091: */
092: public void removePendingRequest(DocumentDebugAction<?> action) {
093: String className = action.getClassName();
094: Vector<DocumentDebugAction<?>> actions = _pendingActions
095: .get(className);
096: if (actions == null) {
097: return;
098: }
099: actions.remove(action);
100: // check if the vector is empty
101: if (actions.size() == 0) {
102: _pendingActions.remove(className);
103: }
104: }
105:
106: /** Recursively look through all nested types to see if the line number exists.
107: * @param lineNumber line number to look for
108: * @param rt reference type to start at
109: * @return true if line number is found
110: */
111: private boolean recursiveFindLineNumber(int lineNumber,
112: ReferenceType rt) {
113: try {
114: for (Location l : rt.allLineLocations()) {
115: if (l.lineNumber() == lineNumber) {
116: return true;
117: }
118: }
119: for (ReferenceType nested : rt.nestedTypes()) {
120: if (recursiveFindLineNumber(lineNumber, nested) == true) {
121: return true;
122: }
123: }
124: } catch (AbsentInformationException aie) {
125: // ignore, return false
126: }
127:
128: return false;
129: }
130:
131: /**
132: * Called by the EventHandler whenever a ClassPrepareEvent occurs.
133: * This will take the event, get the class that was prepared, lookup
134: * the Vector of DebugAction that was waiting for this class's preparation,
135: * iterate through this Vector, and attempt to create the Breakpoints that
136: * were pending. Since the keys to the HashTable are the names of the
137: * outer class, the $ and everything after it must be cropped off from the
138: * class name in order to do the lookup. During the lookup, however, the line
139: * number of each action is checked to see if the line number is contained
140: * in the given event's ReferenceType. If not, we ignore that pending action
141: * since it is not in the class that was just prepared, but may be in one of its
142: * inner classes.
143: * @param event The ClassPrepareEvent that just occured
144: */
145: public void classPrepared(ClassPrepareEvent event)
146: throws DebugException {
147: ReferenceType rt = event.referenceType();
148: //DrJava.consoleOut().println("In classPrepared. rt: " + rt);
149: //DrJava.consoleOut().println("equals getReferenceType: " +
150: // rt.equals(_manager.getReferenceType(rt.name())));
151: String className = rt.name();
152:
153: // crop off the $ if there is one and anything after it
154: int indexOfDollar = className.indexOf('$');
155: if (indexOfDollar > 1) {
156: className = className.substring(0, indexOfDollar);
157: }
158:
159: // Get the pending actions for this class (and inner classes)
160: Vector<DocumentDebugAction<?>> actions = _pendingActions
161: .get(className);
162: Vector<DocumentDebugAction<?>> failedActions = new Vector<DocumentDebugAction<?>>();
163: //DrJava.consoleOut().println("pending actions: " + actions);
164: if (actions == null) {
165: // Must have been a different class with a matching prefix, ignore it
166: // since we're not interested in this class.
167: return;
168: } else if (actions.isEmpty()) {
169: // any actions that were waiting for this class to be prepared have been
170: // removed
171: _manager.getEventRequestManager().deleteEventRequest(
172: event.request());
173: return;
174: }
175: for (int i = 0; i < actions.size(); i++) {
176: DocumentDebugAction<?> a = actions.get(i);
177: int lineNumber = a.getLineNumber();
178: if (lineNumber != DebugAction.ANY_LINE) {
179: try {
180: List lines = rt.locationsOfLine(lineNumber);
181: if (lines.size() == 0) {
182: // Do not disable action; the line number might just be in another class in the same file
183: String exactClassName = a.getExactClassName();
184: if (exactClassName != null
185: && exactClassName.equals(rt.name())) {
186: _manager
187: .printMessage(actions.get(i)
188: .toString()
189: + " not on an executable line; disabled.");
190: actions.get(i).setEnabled(false);
191: }
192:
193: // Requested line number not in reference type, skip this action
194: continue;
195: }
196: } catch (AbsentInformationException aie) {
197: // outer class has no line number info, skip this action
198: continue;
199: }
200: }
201: // check if the action was successfully created
202: try {
203: Vector<ReferenceType> refTypes = new Vector<ReferenceType>();
204: refTypes.add(rt);
205: a.createRequests(refTypes); // This type warning will go away in JDK 1.5
206: } catch (DebugException e) {
207: failedActions.add(a);
208: // DrJava.consoleOut().println("Exception preparing request!! " + e);
209: }
210: }
211:
212: // For debugging purposes
213: /*
214: List l = _manager.getEventRequestManager().breakpointRequests();
215: System.out.println("list of eventrequestmanager's breakpointRequests: " +
216: l);
217: for (int i = 0; i < l.size(); i++) {
218: BreakpointRequest br = (BreakpointRequest)l.get(i);
219: System.out.println("isEnabled(): " + br.isEnabled() +
220: " suspendPolicy(): " + br.suspendPolicy() +
221: " location(): " + br.location());
222: }
223: */
224: if (failedActions.size() > 0) {
225: // need to create an exception framework
226: throw new DebugException("Failed actions: " + failedActions);
227: }
228: }
229: }
|