Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Session Hook



The SessionHook is the interface which executes the code after a Debug session is asked to start the script by DebugSession.startScript().

The DebugSession.setSessionHook(SessionHook) will specify the code which will be executed after the start of the session. The most simple code to execute is just calling the code of one of the Script interface method.

Example

For example for the following script interface:
      public interface Script {
        public int compute() {
        }
      }
We can perform:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      DebugSession<Script> session = wrapper.createDebugSession();
      SessionHook<Script> hook = new SessionHook<> {
        public Object start(Script script) {
          return script.compute();
        }
      }

      session.setSessionHook(hook);
      
      session.startSession();
      session.startScript(); // here the script.compute() method is effectively executed         

Using a script method without a return value in the SessionHook

To use a script method without a return value, just execute the method and return null. For example:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      DebugSession<Script> session = wrapper.createDebugSession();
      SessionHook<Script> hook = new SessionHook<> {
        public Object start(Script script) {
          script.compute();
          return null;
        }
      }

      session.setSessionHook(hook);

      session.startSession();
      session.startScript(); // here the script.compute() method is effectively executed                  

Using a script method with arguments in the SessionHook

To use a script method with arguments, just add these arguments in the hook call. For example:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      DebugSession<Script> session = wrapper.createDebugSession();
      SessionHook<Script> hook = new SessionHook<> {
        public Object start(Script script, int arg) {
          return script.compute(arg);
        }
      }

      session.setSessionHook(hook);
      
      session.startSession();
      session.startScript(10); // here the script.compute() method is effectively executed

Executing complex code in the SessionHook

It is not mandatory to have only one method in the SessionHook. For example, for the following script interface:
      public interface Script {
        public int compute(int value) {
        }
      }
It is valid to have the following code for the SessionHook:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      DebugSession<Script> session = wrapper.createDebugSession();
      SessionHook<Script> hook = new SessionHook<> {
        public Object start(Script script) {
          int result = 0;
          for (int i = 0; i < 100; i++) {
            result += script.compute(i);
          }
          return result;
        }
      }

      session.setSessionHook(hook);
      
      session.startSession();
      session.startScript();

Categories: api | debugging

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence