Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Additional helpers



A script can have a ScriptHelper and a ScriptLogger. However it is also possible to add additional helper classes which may be used in the script.

This can be interesting if you have existing classes in you project which you want to make available in your script without refactoring them to convert them to a single script helper.

This functionality is currently working in Groovy, and Javascript, but not Python and Ruby

Adding a additional Helper

If you want to have a script helper, you must use the ScriptWrapper.addAdditionalHelper(String, Object) method:
  • The first argument is the name of the field for the helper in the script
  • The second argument is the helper instance Object

Note that the script will have native access to all the public methods in the helper class.

Example

Suppose the following script interface:
      public interface Script {
        public int execute();
      }
And the following helper class:
      public class HelperAPI {
        public int getValue() {
          return 10;
        }
      }
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>(){
      };
      wrapper.addAdditionalHelper("api", new HelperAPI());

      File file = new File(<our script file>);
      Script script = wrapper.installScript(file);

      int value = script.execute();
with the following script:
      public int execute() {
        return api.getValue() + 1;
      }
The result will be 11.

See also


Categories: api

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