/core/src/com/bluemarsh/jswat/core/stepping/AbstractStepper.java

http://jswat.googlecode.com/ · Java · 164 lines · 88 code · 17 blank · 59 comment · 6 complexity · 8f4cdbd4b79581b892992089c73e60d9 MD5 · raw file

  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. */
  23. package com.bluemarsh.jswat.core.stepping;
  24. import com.bluemarsh.jswat.core.context.ContextProvider;
  25. import com.bluemarsh.jswat.core.context.DebuggingContext;
  26. import com.bluemarsh.jswat.core.event.Dispatcher;
  27. import com.bluemarsh.jswat.core.event.DispatcherListener;
  28. import com.bluemarsh.jswat.core.event.DispatcherProvider;
  29. import com.bluemarsh.jswat.core.session.Session;
  30. import com.bluemarsh.jswat.core.session.SessionEvent;
  31. import com.bluemarsh.jswat.core.session.SessionListener;
  32. import com.sun.jdi.ThreadReference;
  33. import com.sun.jdi.VirtualMachine;
  34. import com.sun.jdi.request.EventRequest;
  35. import com.sun.jdi.request.EventRequestManager;
  36. import com.sun.jdi.request.StepRequest;
  37. import java.util.ArrayList;
  38. import java.util.List;
  39. import org.openide.util.NbBundle;
  40. /**
  41. * Class AbstractStepper provides an abstract implementation of Stepper
  42. * concrete implementations to subclass. It maintains provides sensible
  43. * default implementations of some of the methods.
  44. *
  45. * @author Nathan Fiedler
  46. */
  47. public abstract class AbstractStepper implements SessionListener, Stepper {
  48. /** The Session instance we belong to. */
  49. private Session owningSession;
  50. /**
  51. * Creates a new instance of AbstractStepper.
  52. */
  53. public AbstractStepper() {
  54. }
  55. /**
  56. * Clear any step requests that may still be associated with the thread.
  57. * Step requests that have not yet completed must be removed before any
  58. * additional step requests are created.
  59. *
  60. * @param vm virtual machine in which to clear requests.
  61. * @param thread thread on which to remove step requests.
  62. */
  63. protected static void clearPreviousStep(VirtualMachine vm, ThreadReference thread) {
  64. EventRequestManager erm = vm.eventRequestManager();
  65. List<EventRequest> requests = new ArrayList<EventRequest>(1);
  66. List<StepRequest> steps = erm.stepRequests();
  67. for (StepRequest request : steps) {
  68. if (request.thread().equals(thread)) {
  69. requests.add(request);
  70. }
  71. }
  72. erm.deleteEventRequests(requests);
  73. }
  74. @Override
  75. public void closing(SessionEvent sevt) {
  76. }
  77. @Override
  78. public void connected(SessionEvent sevt) {
  79. }
  80. /**
  81. * Creates a step request for the Session associated with this instance.
  82. * The request must be enanbled and the Session resumed.
  83. *
  84. * @param size how much to step (one of the StepRequest constants).
  85. * @param depth how to step (one of the StepRequest constants).
  86. * @return the step request.
  87. * @throws SteppingException
  88. * if there is a problem in creating the step request.
  89. */
  90. protected StepRequest createStep(int size, int depth) throws SteppingException {
  91. DebuggingContext dc = ContextProvider.getContext(owningSession);
  92. ThreadReference thread = dc.getThread();
  93. if (thread != null) {
  94. VirtualMachine vm = owningSession.getConnection().getVM();
  95. StepRequest req = step(vm, thread, size, depth);
  96. return req;
  97. } else {
  98. String msg = NbBundle.getMessage(getClass(), "CTL_Stepping_NoThread");
  99. throw new SteppingException(msg);
  100. }
  101. }
  102. @Override
  103. public void disconnected(SessionEvent sevt) {
  104. }
  105. @Override
  106. public void opened(Session session) {
  107. owningSession = session;
  108. }
  109. /**
  110. * Registers this stepper as a DispatchListener for the given request.
  111. * In general such requests are set to fire just once, using the
  112. * <code>EventRequest.addCountFilter()</code> method, so there is no
  113. * corresponding "unregister" method.
  114. *
  115. * @param request the event request.
  116. */
  117. protected void register(EventRequest request) {
  118. if (this instanceof DispatcherListener) {
  119. Dispatcher dispatcher = DispatcherProvider.getDispatcher(owningSession);
  120. dispatcher.register((DispatcherListener) this, request);
  121. }
  122. }
  123. @Override
  124. public void resuming(SessionEvent sevt) {
  125. }
  126. @Override
  127. public void step(int size, int depth) throws SteppingException {
  128. StepRequest req = createStep(size, depth);
  129. req.enable();
  130. owningSession.resumeVM();
  131. }
  132. @Override
  133. public void stepInto() throws SteppingException {
  134. step(StepRequest.STEP_LINE, StepRequest.STEP_INTO);
  135. }
  136. @Override
  137. public void stepOut() throws SteppingException {
  138. step(StepRequest.STEP_LINE, StepRequest.STEP_OUT);
  139. }
  140. @Override
  141. public void stepOver() throws SteppingException {
  142. step(StepRequest.STEP_LINE, StepRequest.STEP_OVER);
  143. }
  144. @Override
  145. public void suspended(SessionEvent sevt) {
  146. }
  147. }