Interface Script

All Known Implementing Classes:
SimpleScript

public interface Script
Represents a script written in a scripting language such as Lua, Ruby, Python or JavaScript, or any others that implements the JSR-233 scripting interfaces, including Invocable and Compilable.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    convertToInterface(Object obj, Class<T> expected)
    Converts an object returned by a script to the given Java interface.
    Fetches the attribute with the specified name, or null if not found.
    invoke(Object instance, String name, Object... args)
    Invokes a method on the given object, which should be a return value or scripting object.
    invoke(String name, Object... args)
    Invokes a root-level method using the method name and args and returns the result.
    void
    setAttribute(String name, Object value)
    Sets the attribute with the given name and value.
  • Method Details

    • convertToInterface

      <T> T convertToInterface(Object obj, Class<T> expected)
      Converts an object returned by a script to the given Java interface. The class should be an interface, as although abstract classes are accepted by the Rhino engine, this is not standard behaviour.
      Parameters:
      obj - The object to convert
      expected - The expected interface
      Returns:
      The converted class
    • getAttribute

      Object getAttribute(String name)
      Fetches the attribute with the specified name, or null if not found. The returned attribute can be user-stored or a script variable, such as a function. Script objects should be accessed through invoke(String, Object...)
      Parameters:
      name - The key of the attribute
      Returns:
      The attribute
    • invoke

      Object invoke(Object instance, String name, Object... args)
      Invokes a method on the given object, which should be a return value or scripting object.
      Parameters:
      instance - The object instance to invoke the method on
      name - The name of the method
      args - The method arguments
      Returns:
      The result of the method, if any
      Throws:
      NoSuchMethodException - If no such method was found on the object
    • invoke

      Object invoke(String name, Object... args)
      Invokes a root-level method using the method name and args and returns the result.
      Parameters:
      name - The method name
      args - The method arguments
      Returns:
      The result of the method call, or null if there was none
    • setAttribute

      void setAttribute(String name, Object value)
      Sets the attribute with the given name and value.