Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Script helper



It is possible to use a script helper in scripts. A script helper allows to define helper methods associated with your application. Note that these methods can also be defined in the script context, but it is clearer to define a specific class for them.

It there is a script helper, it is available through the helper field in your script. For example:
      public void apply() {
         helper.doSomeComputation();
      }

Adding a script Helper

If you want to have a script helper, you must use a custom script context and implement the ScriptContext.getHelper() method.

For example for the CustomContext class:
      public class CustomContext extends DefaultScriptContext {
        private final CustomHelper helper;

        public CustomContext() {
          helper = new CustomHelper(this);
        }

        public ScriptHelper getHelper() {
          return helper;
        }
      }
And for the CustomHelper class:
      public class CustomHelper implements ScriptHelper {
        private final ScriptContext context;

        public CustomHelper(ScriptContext context) {
          this.context = context;
        }

        public double doSomeComputation(double angle) {
          return Math.sin(angle);
        }
We can perform in a Script:
      public void apply() {
        double result = helper.doSomeComputation(1.2d);
        context.echo(result);
      }

Default script Helper

The DefaultScriptHelper class is a basic default script helper which implements the ScriptContext.getHelper() method.

The DefaultScriptHelper class is a generic class which allows to specify the type of the ScriptContext class.


For example for the previous example:
      public class CustomHelper extends DefaultScriptHelper {
        public double doSomeComputation(double angle) {
          return Math.sin(angle);
        }
      }

Helper API

By default the helper have one method:

public class ScriptHelper


Modifier and Type Method and Description
ScriptContext getContext()
Return the script context

See also


Categories: api

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