private boolean shouldApplyContext(DebuggerContextImpl context) {
   SuspendContextImpl suspendContext = context.getSuspendContext();
   SuspendContextImpl currentContext = (SuspendContextImpl) getSession().getSuspendContext();
   if (suspendContext != null && !suspendContext.equals(currentContext)) return true;
   JavaExecutionStack currentExecutionStack =
       currentContext != null ? currentContext.getActiveExecutionStack() : null;
   return currentExecutionStack == null
       || !Comparing.equal(context.getThreadProxy(), currentExecutionStack.getThreadProxy());
 }
        @Override
        public void changeEvent(DebuggerContextImpl newContext, DebuggerSession.Event event) {

          final DebuggerSession session = newContext.getDebuggerSession();
          if (event == DebuggerSession.Event.PAUSE
              && myDebuggerStateManager.myDebuggerSession != session) {
            // if paused in non-active session; switch current session
            myDebuggerStateManager.setState(
                newContext,
                session != null ? session.getState() : DebuggerSession.State.DISPOSED,
                event,
                null);
            return;
          }

          if (myDebuggerStateManager.myDebuggerSession == session) {
            myDebuggerStateManager.fireStateChanged(newContext, event);
          }
          if (event == DebuggerSession.Event.ATTACHED) {
            myDispatcher.getMulticaster().sessionAttached(session);
          } else if (event == DebuggerSession.Event.DETACHED) {
            myDispatcher.getMulticaster().sessionDetached(session);
          } else if (event == DebuggerSession.Event.DISPOSE) {
            dispose(session);
            if (myDebuggerStateManager.myDebuggerSession == session) {
              myDebuggerStateManager.setState(
                  DebuggerContextImpl.EMPTY_CONTEXT,
                  DebuggerSession.State.DISPOSED,
                  DebuggerSession.Event.DISPOSE,
                  null);
            }
          }
        }
 @Override
 public void setState(
     final DebuggerContextImpl context,
     DebuggerSession.State state,
     DebuggerSession.Event event,
     String description) {
   ApplicationManager.getApplication().assertIsDispatchThread();
   myDebuggerSession = context.getDebuggerSession();
   if (myDebuggerSession != null) {
     myDebuggerSession.getContextManager().setState(context, state, event, description);
   } else {
     fireStateChanged(context, event);
   }
 }
  public DebuggerContextImpl createDebuggerContext(
      final SuspendContextImpl suspendContext, StackFrameProxyImpl stackFrame) {
    final DebuggerSession[] session = new DebuggerSession[1];

    UIUtil.invokeAndWaitIfNeeded(
        new Runnable() {
          @Override
          public void run() {
            session[0] =
                DebuggerManagerEx.getInstanceEx(myProject)
                    .getSession(suspendContext.getDebugProcess());
          }
        });

    DebuggerContextImpl debuggerContext =
        DebuggerContextImpl.createDebuggerContext(
            session[0],
            suspendContext,
            stackFrame != null ? stackFrame.threadProxy() : null,
            stackFrame);
    debuggerContext.initCaches();
    return debuggerContext;
  }
 private void unsetPausedIfNeeded(DebuggerContextImpl context) {
   SuspendContextImpl suspendContext = context.getSuspendContext();
   if (suspendContext != null && context.getThreadProxy() != suspendContext.getThread()) {
     ((XDebugSessionImpl) getSession()).unsetPaused();
   }
 }
  public void reloadClasses(final Map<String, HotSwapFile> modifiedClasses) {
    DebuggerManagerThreadImpl.assertIsManagerThread();

    if (modifiedClasses == null || modifiedClasses.size() == 0) {
      myProgress.addMessage(
          myDebuggerSession,
          MessageCategory.INFORMATION,
          DebuggerBundle.message("status.hotswap.loaded.classes.up.to.date"));
      return;
    }

    final DebugProcessImpl debugProcess = getDebugProcess();
    final VirtualMachineProxyImpl virtualMachineProxy = debugProcess.getVirtualMachineProxy();

    final Project project = debugProcess.getProject();
    final BreakpointManager breakpointManager =
        (DebuggerManagerEx.getInstanceEx(project)).getBreakpointManager();
    breakpointManager.disableBreakpoints(debugProcess);

    // virtualMachineProxy.suspend();

    try {
      RedefineProcessor redefineProcessor = new RedefineProcessor(virtualMachineProxy);

      int processedEntriesCount = 0;
      for (final Map.Entry<String, HotSwapFile> entry : modifiedClasses.entrySet()) {
        // stop if process is finished already
        if (debugProcess.isDetached() || debugProcess.isDetaching()) {
          break;
        }
        if (redefineProcessor.getProcessedClassesCount() == 0 && myProgress.isCancelled()) {
          // once at least one class has been actually reloaded, do not interrupt the whole process
          break;
        }
        processedEntriesCount++;
        final String qualifiedName = entry.getKey();
        if (qualifiedName != null) {
          myProgress.setText(qualifiedName);
          myProgress.setFraction(processedEntriesCount / (double) modifiedClasses.size());
        }
        try {
          redefineProcessor.processClass(qualifiedName, entry.getValue().file);
        } catch (IOException e) {
          reportProblem(qualifiedName, e);
        }
      }

      if (redefineProcessor.getProcessedClassesCount() == 0 && myProgress.isCancelled()) {
        // once at least one class has been actually reloaded, do not interrupt the whole process
        return;
      }

      redefineProcessor.processPending();
      myProgress.setFraction(1);

      final int partiallyRedefinedClassesCount =
          redefineProcessor.getPartiallyRedefinedClassesCount();
      if (partiallyRedefinedClassesCount == 0) {
        myProgress.addMessage(
            myDebuggerSession,
            MessageCategory.INFORMATION,
            DebuggerBundle.message(
                "status.classes.reloaded", redefineProcessor.getProcessedClassesCount()));
      } else {
        final String message =
            DebuggerBundle.message(
                "status.classes.not.all.versions.reloaded",
                partiallyRedefinedClassesCount,
                redefineProcessor.getProcessedClassesCount());
        myProgress.addMessage(myDebuggerSession, MessageCategory.WARNING, message);
      }

      LOG.debug("classes reloaded");
    } catch (Throwable e) {
      processException(e);
    }

    debugProcess.getPositionManager().clearCache();

    DebuggerContextImpl context = myDebuggerSession.getContextManager().getContext();
    SuspendContextImpl suspendContext = context.getSuspendContext();
    if (suspendContext != null) {
      XExecutionStack stack = suspendContext.getActiveExecutionStack();
      if (stack != null) {
        ((JavaExecutionStack) stack).initTopFrame();
      }
    }

    final Semaphore waitSemaphore = new Semaphore();
    waitSemaphore.down();
    //noinspection SSBasedInspection
    SwingUtilities.invokeLater(
        () -> {
          try {
            if (!project.isDisposed()) {
              breakpointManager.reloadBreakpoints();
              debugProcess.getRequestsManager().clearWarnings();
              if (LOG.isDebugEnabled()) {
                LOG.debug("requests updated");
                LOG.debug("time stamp set");
              }
              myDebuggerSession.refresh(false);

              XDebugSession session = myDebuggerSession.getXDebugSession();
              if (session != null) {
                session.rebuildViews();
              }
            }
          } catch (Throwable e) {
            LOG.error(e);
          } finally {
            waitSemaphore.up();
          }
        });

    waitSemaphore.waitFor();

    if (!project.isDisposed()) {
      try {
        breakpointManager.enableBreakpoints(debugProcess);
      } catch (Exception e) {
        processException(e);
      }
    }
  }