/core/src/com/bluemarsh/jswat/core/stepping/AbstractStepper.java
Java | 164 lines | 88 code | 17 blank | 59 comment | 6 complexity | 8f4cdbd4b79581b892992089c73e60d9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
1/* 2 * The contents of this file are subject to the terms of the Common Development 3 * and Distribution License (the License). You may not use this file except in 4 * compliance with the License. 5 * 6 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html 7 * or http://www.netbeans.org/cddl.txt. 8 * 9 * When distributing Covered Code, include this CDDL Header Notice in each file 10 * and include the License file at http://www.netbeans.org/cddl.txt. 11 * If applicable, add the following below the CDDL Header, with the fields 12 * enclosed by brackets [] replaced by your own identifying information: 13 * "Portions Copyrighted [year] [name of copyright owner]" 14 * 15 * The Original Software is JSwat. The Initial Developer of the Original 16 * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler 17 * are Copyright (C) 2005-2010. All Rights Reserved. 18 * 19 * Contributor(s): Nathan L. Fiedler. 20 * 21 * $Id: AbstractStepper.java 260 2010-04-20 06:21:31Z nathanfiedler $ 22 */ 23package com.bluemarsh.jswat.core.stepping; 24 25import com.bluemarsh.jswat.core.context.ContextProvider; 26import com.bluemarsh.jswat.core.context.DebuggingContext; 27import com.bluemarsh.jswat.core.event.Dispatcher; 28import com.bluemarsh.jswat.core.event.DispatcherListener; 29import com.bluemarsh.jswat.core.event.DispatcherProvider; 30import com.bluemarsh.jswat.core.session.Session; 31import com.bluemarsh.jswat.core.session.SessionEvent; 32import com.bluemarsh.jswat.core.session.SessionListener; 33import com.sun.jdi.ThreadReference; 34import com.sun.jdi.VirtualMachine; 35import com.sun.jdi.request.EventRequest; 36import com.sun.jdi.request.EventRequestManager; 37import com.sun.jdi.request.StepRequest; 38import java.util.ArrayList; 39import java.util.List; 40import org.openide.util.NbBundle; 41 42/** 43 * Class AbstractStepper provides an abstract implementation of Stepper 44 * concrete implementations to subclass. It maintains provides sensible 45 * default implementations of some of the methods. 46 * 47 * @author Nathan Fiedler 48 */ 49public abstract class AbstractStepper implements SessionListener, Stepper { 50 51 /** The Session instance we belong to. */ 52 private Session owningSession; 53 54 /** 55 * Creates a new instance of AbstractStepper. 56 */ 57 public AbstractStepper() { 58 } 59 60 /** 61 * Clear any step requests that may still be associated with the thread. 62 * Step requests that have not yet completed must be removed before any 63 * additional step requests are created. 64 * 65 * @param vm virtual machine in which to clear requests. 66 * @param thread thread on which to remove step requests. 67 */ 68 protected static void clearPreviousStep(VirtualMachine vm, ThreadReference thread) { 69 EventRequestManager erm = vm.eventRequestManager(); 70 List<EventRequest> requests = new ArrayList<EventRequest>(1); 71 List<StepRequest> steps = erm.stepRequests(); 72 for (StepRequest request : steps) { 73 if (request.thread().equals(thread)) { 74 requests.add(request); 75 } 76 } 77 erm.deleteEventRequests(requests); 78 } 79 80 @Override 81 public void closing(SessionEvent sevt) { 82 } 83 84 @Override 85 public void connected(SessionEvent sevt) { 86 } 87 88 /** 89 * Creates a step request for the Session associated with this instance. 90 * The request must be enanbled and the Session resumed. 91 * 92 * @param size how much to step (one of the StepRequest constants). 93 * @param depth how to step (one of the StepRequest constants). 94 * @return the step request. 95 * @throws SteppingException 96 * if there is a problem in creating the step request. 97 */ 98 protected StepRequest createStep(int size, int depth) throws SteppingException { 99 DebuggingContext dc = ContextProvider.getContext(owningSession); 100 ThreadReference thread = dc.getThread(); 101 if (thread != null) { 102 VirtualMachine vm = owningSession.getConnection().getVM(); 103 StepRequest req = step(vm, thread, size, depth); 104 return req; 105 } else { 106 String msg = NbBundle.getMessage(getClass(), "CTL_Stepping_NoThread"); 107 throw new SteppingException(msg); 108 } 109 } 110 111 @Override 112 public void disconnected(SessionEvent sevt) { 113 } 114 115 @Override 116 public void opened(Session session) { 117 owningSession = session; 118 } 119 120 /** 121 * Registers this stepper as a DispatchListener for the given request. 122 * In general such requests are set to fire just once, using the 123 * <code>EventRequest.addCountFilter()</code> method, so there is no 124 * corresponding "unregister" method. 125 * 126 * @param request the event request. 127 */ 128 protected void register(EventRequest request) { 129 if (this instanceof DispatcherListener) { 130 Dispatcher dispatcher = DispatcherProvider.getDispatcher(owningSession); 131 dispatcher.register((DispatcherListener) this, request); 132 } 133 } 134 135 @Override 136 public void resuming(SessionEvent sevt) { 137 } 138 139 @Override 140 public void step(int size, int depth) throws SteppingException { 141 StepRequest req = createStep(size, depth); 142 req.enable(); 143 owningSession.resumeVM(); 144 } 145 146 @Override 147 public void stepInto() throws SteppingException { 148 step(StepRequest.STEP_LINE, StepRequest.STEP_INTO); 149 } 150 151 @Override 152 public void stepOut() throws SteppingException { 153 step(StepRequest.STEP_LINE, StepRequest.STEP_OUT); 154 } 155 156 @Override 157 public void stepOver() throws SteppingException { 158 step(StepRequest.STEP_LINE, StepRequest.STEP_OVER); 159 } 160 161 @Override 162 public void suspended(SessionEvent sevt) { 163 } 164}