Clover coverage report - DrJava Test Coverage (drjava-20120304-r5456)
Coverage timestamp: Sun Mar 4 2012 03:13:23 CST
file stats: LOC: 235   Methods: 6
NCLOC: 116   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PendingRequestManager.java 0% 3% 16.7% 3%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.drjava.model.debug.jpda;
 38   
 39    import com.sun.jdi.*;
 40    import com.sun.jdi.request.*;
 41    import com.sun.jdi.event.*;
 42   
 43    import java.util.HashMap;
 44    import java.util.List;
 45    import java.util.Vector;
 46    import java.util.TreeMap;
 47   
 48    import java.io.File;
 49   
 50    import edu.rice.cs.drjava.model.DrJavaFileUtils;
 51    import edu.rice.cs.drjava.model.debug.DebugException;
 52    import edu.rice.cs.drjava.model.compiler.LanguageLevelStackTraceMapper;
 53   
 54    /** Keeps track of DocumentDebugActions that are waiting to be resolved when the classes they corresponed to are
 55    * prepared. (Only DocumentDebugActions have reference types which can be prepared.)
 56    * @version $Id: PendingRequestManager.java 5442 2011-08-16 09:11:12Z rcartwright $
 57    */
 58   
 59    public class PendingRequestManager {
 60    private JPDADebugger _manager;
 61    private HashMap<String, Vector<DocumentDebugAction<?>>> _pendingActions;
 62   
 63  2198 public PendingRequestManager(JPDADebugger manager) {
 64  2198 _manager = manager;
 65  2198 _pendingActions = new HashMap<String, Vector<DocumentDebugAction<?>>>();
 66    }
 67   
 68    /** Called if a breakpoint is set before its class is prepared
 69    * @param action The DebugAction that is pending
 70    */
 71  0 public void addPendingRequest (DocumentDebugAction<?> action) {
 72  0 String className = action.getClassName();
 73  0 Vector<DocumentDebugAction<?>> actions = _pendingActions.get(className);
 74  0 if (actions == null) {
 75  0 actions = new Vector<DocumentDebugAction<?>>();
 76   
 77    // only create a ClassPrepareRequest once per class
 78  0 ClassPrepareRequest request =
 79    _manager.getEventRequestManager().createClassPrepareRequest();
 80    // Listen for events from the class, and also its inner classes
 81  0 request.addClassFilter(className + "*");
 82  0 request.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD);
 83  0 request.enable();
 84    //System.out.println("Creating prepareRequest in class " + className);
 85    }
 86  0 actions.add(action);
 87  0 _pendingActions.put(className, actions);
 88    }
 89   
 90    /** Called if a breakpoint is set and removed before its class is prepared
 91    * @param action The DebugAction that was set and removed
 92    */
 93  0 public void removePendingRequest (DocumentDebugAction<?> action) {
 94  0 String className = action.getClassName();
 95  0 Vector<DocumentDebugAction<?>> actions = _pendingActions.get(className);
 96  0 if (actions == null) {
 97  0 return;
 98    }
 99  0 actions.remove(action);
 100    // check if the vector is empty
 101  0 if (actions.size() == 0) {
 102  0 _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  0 private boolean recursiveFindLineNumber(int lineNumber, ReferenceType rt) {
 112  0 try {
 113  0 for(Location l: rt.allLineLocations()) {
 114  0 if (l.lineNumber() == lineNumber) { return true; }
 115    }
 116  0 for(ReferenceType nested: rt.nestedTypes()) {
 117  0 if (recursiveFindLineNumber(lineNumber, nested) == true) { return true; }
 118    }
 119    }
 120    catch (AbsentInformationException aie) { /* fall through and return false */ }
 121  0 return false;
 122    }
 123   
 124   
 125    /**
 126    * Method to change Language Level line numbers into their java file counterparts
 127    * @param dda the DocumentDebugAction whose line needs to be adjusted
 128    * @return the correct line number for the .java file
 129    */
 130  0 public int LLDDALineNum(DocumentDebugAction<?> dda){
 131  0 int line = dda.getLineNumber();
 132  0 File f = dda.getFile();
 133   
 134  0 if (DrJavaFileUtils.isLLFile(f)) {
 135  0 f = DrJavaFileUtils.getJavaForLLFile(f);
 136  0 TreeMap<Integer, Integer> tM = _manager.getLLSTM().readLLBlock(f);
 137  0 line = tM.get(dda.getLineNumber());
 138    }
 139  0 return line;
 140    }
 141   
 142    /** Called by the EventHandler whenever a ClassPrepareEvent occurs. This will take the event, get the class that was
 143    * prepared, lookup the Vector of DebugAction that was waiting for this class's preparation, iterate through this
 144    * Vector, and attempt to create the Breakpoints that
 145    * were pending. Since the keys to the Hashtable are the names of the
 146    * outer class, the $ and everything after it must be cropped off from the
 147    * class name in order to do the lookup. During the lookup, however, the line
 148    * number of each action is checked to see if the line number is contained
 149    * in the given event's ReferenceType. If not, we ignore that pending action
 150    * since it is not in the class that was just prepared, but may be in one of its
 151    * inner classes.
 152    * @param event The ClassPrepareEvent that just occured
 153    */
 154  0 public void classPrepared (ClassPrepareEvent event) throws DebugException {
 155  0 ReferenceType rt = event.referenceType();
 156    //DrJava.consoleOut().println("In classPrepared. rt: " + rt);
 157    //DrJava.consoleOut().println("equals getReferenceType: " +
 158    // rt.equals(_manager.getReferenceType(rt.name())));
 159  0 String className = rt.name();
 160   
 161    // crop off the $ if there is one and anything after it
 162  0 int indexOfDollar = className.indexOf('$');
 163  0 if (indexOfDollar > 1) {
 164  0 className = className.substring(0, indexOfDollar);
 165    }
 166   
 167    // Get the pending actions for this class (and inner classes)
 168  0 Vector<DocumentDebugAction<?>> actions = _pendingActions.get(className);
 169  0 Vector<DocumentDebugAction<?>> failedActions =
 170    new Vector<DocumentDebugAction<?>>();
 171    //DrJava.consoleOut().println("pending actions: " + actions);
 172  0 if (actions == null) {
 173    // Must have been a different class with a matching prefix, ignore it
 174    // since we're not interested in this class.
 175  0 return;
 176    }
 177  0 else if (actions.isEmpty()) {
 178    // any actions that were waiting for this class to be prepared have been
 179    // removed
 180  0 _manager.getEventRequestManager().deleteEventRequest(event.request());
 181  0 return;
 182    }
 183  0 for (int i = 0; i < actions.size(); i++) {
 184  0 DocumentDebugAction<?> a = actions.get(i);
 185  0 int lineNumber = LLDDALineNum(a);//a.getLineNumber();
 186  0 if (lineNumber != DebugAction.ANY_LINE) {
 187  0 try {
 188  0 List<Location> lines = rt.locationsOfLine(lineNumber);
 189  0 if (lines.size() == 0) {
 190    // Do not disable action; the line number might just be in another class in the same file
 191  0 String exactClassName = a.getExactClassName();
 192  0 if (exactClassName != null && exactClassName.equals(rt.name())) {
 193  0 _manager.printMessage(actions.get(i).toString() + " not on an executable line; disabled.");
 194  0 actions.get(i).setEnabled(false);
 195    }
 196   
 197    // Requested line number not in reference type, skip this action
 198  0 continue;
 199    }
 200    }
 201    catch (AbsentInformationException aie) {
 202    // outer class has no line number info, skip this action
 203  0 continue;
 204    }
 205    }
 206    // check if the action was successfully created
 207  0 try {
 208  0 Vector<ReferenceType> refTypes = new Vector<ReferenceType>();
 209  0 refTypes.add(rt);
 210  0 a.createRequests(refTypes); // This type warning will go away in JDK 1.5
 211    }
 212    catch (DebugException e) {
 213  0 failedActions.add(a);
 214    // DrJava.consoleOut().println("Exception preparing request!! " + e);
 215    }
 216    }
 217   
 218    // For debugging purposes
 219    /*
 220    List l = _manager.getEventRequestManager().breakpointRequests();
 221    System.out.println("list of eventrequestmanager's breakpointRequests: " +
 222    l);
 223    for (int i = 0; i < l.size(); i++) {
 224    BreakpointRequest br = (BreakpointRequest)l.get(i);
 225    System.out.println("isEnabled(): " + br.isEnabled() +
 226    " suspendPolicy(): " + br.suspendPolicy() +
 227    " location(): " + br.location());
 228    }
 229    */
 230  0 if (failedActions.size() > 0) {
 231    // need to create an exception framework
 232  0 throw new DebugException("Failed actions: " + failedActions);
 233    }
 234    }
 235    }