Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Direct method invocation



Invoking directly any method (and not only those defined in the interface) in the script can be performed by calling one of the following methods: If Exceptions handling is enabled, exception will be logged rather to be thrown.

Getting the script methods

It is possible to get the map of methods defined in the script by ScriptMethodInvoker.getInvocableMethods() and ScriptMethodInvoker.getInvocableMethodsByNames().

These two methods will return the methods available in the installed script (not limited to the ones define in the Script interface). See Method invocation to see how to use them.

Note that depending on the language, this Map can contain more methods than only those defined in the Script itself[1]
However they will still be available to be invoked
.

For example, suppose the following interface:
      public interface Script {
        public int computeResult(int value) {
        }
      }
And the script:
      public int computeResult(int value) {
         return value * 10;
      }

      public void doSomething() {
         context.echo("Hello!);
      }            
The getInvocableMethods() method will return at least two results:
  • One for the computeResult(int value) method
  • One for the doSomething() method

Example

Suppose that we have the following script interface:
      public interface Script {
        public int computeResult(int value);
      }
And the creation of the ScriptWrapper:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };      
            
      File file = new File(<our script file>);
      wrapper.installScript(file);
The basic way to use the wrapper is by calling the computeResult(value) method of the interface such as:
      Script script = wrapper.getScriptProxy();
      int result = script.computeResult(10);
Bu we can also call any method of the script, even those which don't be part of the interface signature. For example, suppose the following Groovy script:
      public int computeResult(int value) {
        return value * 10;
      }
      
      public int myFunction(int value) {
        return value * 100;
      }      
We could perform in our java code:
      int result = wrapper.invokeMethod("myFunction", 10);

Notes

  1. ^ However they will still be available to be invoked

See also


Categories: api | general

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