Package: org.codehaus.groovy.jsr223

## [Java] Class GroovyScriptEngineImpl

- org.codehaus.groovy.jsr223.GroovyScriptEngineImpl

- All Implemented Interfaces and Traits:
    Compilable, Invocable

      public class GroovyScriptEngineImpl
      extends AbstractScriptEngine
      implements Compilable, Invocable

  JSR-223 Engine implementation. Adapted from original by Mike Grogan and A. Sundararajan

- - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class AbstractScriptEngine` | `ARGV, FILENAME, ENGINE, ENGINE_VERSION, NAME, LANGUAGE, LANGUAGE_VERSION`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovyScriptEngineImpl()` Creates an engine backed by a default GroovyClassLoader.
    `GroovyScriptEngineImpl(GroovyClassLoader classLoader)` Creates an engine backed by the supplied class loader.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public CompiledScript` | `compile(String scriptSource)` Compiles Groovy source into a reusable CompiledScript.
    `` | `public CompiledScript` | `compile(Reader reader)` Reads and compiles Groovy source from the supplied reader.
    `` | `public Bindings` | `createBindings()` Creates a mutable bindings instance suitable for engine-scope variables.
    `` | `public Object` | `eval(Reader reader, ScriptContext ctx)` Reads the supplied script source and evaluates it against the given context.
    `` | `public Object` | `eval(String script, ScriptContext ctx)` Evaluates Groovy source within the supplied script context.
    `` | `public GroovyClassLoader` | `getClassLoader()` Returns the class loader currently used for script compilation.
    `` | `public ScriptEngineFactory` | `getFactory()` Returns the factory associated with this engine, creating one lazily if necessary.
    `<T>` | `public T` | `getInterface(Class<T> clazz)` Creates a dynamic proxy that dispatches interface method calls to global functions.
    `<T>` | `public T` | `getInterface(Object thiz, Class<T> clazz)` Creates a dynamic proxy that dispatches interface method calls to the supplied object.
    `` | `public Object` | `invokeFunction(String name, Object args)` Invokes a previously defined global function by name.
    `` | `public Object` | `invokeMethod(Object thiz, String name, Object args)` Invokes a method on a script object or other Groovy object.
    `` | `public void` | `setClassLoader(GroovyClassLoader classLoader)` Replaces the class loader used for future script compilation.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class AbstractScriptEngine` | `createBindings, equals, eval, eval, eval, eval, eval, eval, get, getBindings, getClass, getContext, getFactory, hashCode, notify, notifyAll, put, setBindings, setContext, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### public GroovyScriptEngineImpl()

      Creates an engine backed by a default GroovyClassLoader.

    - #### public GroovyScriptEngineImpl(GroovyClassLoader classLoader)

      Creates an engine backed by the supplied class loader.

      throws:
        IllegalArgumentException if `classLoader` is `null`
      Parameters:
        `classLoader` - the class loader used to parse and load generated script classes

  - ### Method Detail

    - #### public CompiledScript compile(String scriptSource)

      Compiles Groovy source into a reusable CompiledScript.

      throws:
        ScriptException if compilation fails
      Parameters:
        `scriptSource` - the source to compile
      Returns:
        a compiled representation of `scriptSource`

    - #### public CompiledScript compile(Reader reader)

      Reads and compiles Groovy source from the supplied reader.

      throws:
        ScriptException if the reader cannot be consumed or compilation fails
      Parameters:
        `reader` - the reader providing script source
      Returns:
        a compiled representation of the reader content

    - #### public Bindings createBindings()

      Creates a mutable bindings instance suitable for engine-scope variables.

      Returns:
        a new SimpleBindings instance

    - #### public Object eval(Reader reader, ScriptContext ctx)

      Reads the supplied script source and evaluates it against the given context.

      throws:
        ScriptException if the reader cannot be consumed or script evaluation fails
      Parameters:
        `reader` - the reader providing script source
        `ctx` - the execution context to use
      Returns:
        the script result, or the script class when the source defines a class instead of a script

    - #### public Object eval(String script, ScriptContext ctx)

      Evaluates Groovy source within the supplied script context.

      If `#jsr223.groovy.engine.keep.globals` is present in engine scope, its value controls the reference strength used for cached global closures.

      throws:
        ScriptException if compilation or execution fails
      Parameters:
        `script` - the script source to execute
        `ctx` - the execution context to use
      Returns:
        the script result, or the script class when the source defines a class instead of a script

    - #### public GroovyClassLoader getClassLoader()

      Returns the class loader currently used for script compilation.

      Returns:
        the active Groovy class loader

    - #### public ScriptEngineFactory getFactory()

      Returns the factory associated with this engine, creating one lazily if necessary.

      Returns:
        the script engine factory for this engine

    - #### <T> public T getInterface(Class<T> clazz)

      Creates a dynamic proxy that dispatches interface method calls to global functions.

      throws:
        IllegalArgumentException if `clazz` is `null` or not an interface
      Parameters:
        `clazz` - the interface to implement
      Type Parameters:
        `T` - the proxy type
      Returns:
        a proxy backed by this engine's global function namespace

    - #### <T> public T getInterface(Object thiz, Class<T> clazz)

      Creates a dynamic proxy that dispatches interface method calls to the supplied object.

      throws:
        IllegalArgumentException if `thiz` is `null`, or if `clazz` is `null` or not an interface
      Parameters:
        `thiz` - the target object that should receive method calls
        `clazz` - the interface to implement
      Type Parameters:
        `T` - the proxy type
      Returns:
        a proxy backed by `thiz`

    - #### public Object invokeFunction(String name, Object args)

      Invokes a previously defined global function by name.

      throws:
        ScriptException if invocation fails
      throws:
        NoSuchMethodException if no matching function is available
      Parameters:
        `name` - the global function name
        `args` - the arguments to pass to the function
      Returns:
        the function result

    - #### public Object invokeMethod(Object thiz, String name, Object args)

      Invokes a method on a script object or other Groovy object.

      throws:
        ScriptException if invocation fails
      throws:
        NoSuchMethodException if no matching method is available
      throws:
        IllegalArgumentException if `thiz` is `null`
      Parameters:
        `thiz` - the target object
        `name` - the method name
        `args` - the arguments to pass to the method
      Returns:
        the method result

    - #### public void setClassLoader(GroovyClassLoader classLoader)

      Replaces the class loader used for future script compilation.

      Parameters:
        `classLoader` - the class loader to use for subsequent compilations