Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Method invocation



Method invocation is one way of using the API. Remind that the API will consider a script as a class implementing a Java interface (see basic usage.

Use case

The basic usage of the API allow only to call method which are declared in the interface. Directly incoking a method allows to call any method which is defined by the script, including those which do not implement the interface.

API concepts

Main Article: Running a script

See API concepts for how to create the ScriptWrapper. For example, suppose that we have the following script interface:
      public interface Script {
        public int computeResult(int value);
      }
We could perform:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };      
            
      File file = new File(<our script file>);
      wrapper.installScript(file);

Invoking a method


Invoking directly any method 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


To know which methods can be directly invoked, you can use the ScriptMethodInvoker.getInvocableMethods() and ScriptMethodInvoker.getInvocableMethodsByNames() methods which will return the map of methods available in the script.

Example

For the following script interface:
      public interface Script {
        public int computeResult(int value);
      }
Suppose the following script:
      public int computeResult(int value) {
      return value;
      }
      
      public int anotherMethod(int value) {
      return value * 2;
      }      
We can perform:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };            
      File file = new File(<our script file>);
      wrapper.installScript(file);
      wrapper.logExceptions(true);
      
      Object result = wrapper.invokeMethod("anotherMethod", 1);
      // result is the Integer 1

See also


Categories: api | general

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