Class GroovyScriptEngineImpl

java.lang.Object
javax.script.AbstractScriptEngine
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl
All Implemented Interfaces:
Compilable, Invocable, ScriptEngine

public class GroovyScriptEngineImpl extends AbstractScriptEngine implements Compilable, Invocable
JSR-223 Engine implementation. Adapted from original by Mike Grogan and A. Sundararajan
  • Constructor Details

    • GroovyScriptEngineImpl

      public GroovyScriptEngineImpl()
      Creates an engine backed by a default GroovyClassLoader.
    • GroovyScriptEngineImpl

      public GroovyScriptEngineImpl(GroovyClassLoader classLoader)
      Creates an engine backed by the supplied class loader.
      Parameters:
      classLoader - the class loader used to parse and load generated script classes
      Throws:
      IllegalArgumentException - if classLoader is null
  • Method Details

    • eval

      public Object eval(Reader reader, ScriptContext ctx) throws ScriptException
      Reads the supplied script source and evaluates it against the given context.
      Specified by:
      eval in interface ScriptEngine
      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
      Throws:
      ScriptException - if the reader cannot be consumed or script evaluation fails
    • eval

      public Object eval(String script, ScriptContext ctx) throws ScriptException
      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.

      Specified by:
      eval in interface ScriptEngine
      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
      Throws:
      ScriptException - if compilation or execution fails
    • createBindings

      public Bindings createBindings()
      Creates a mutable bindings instance suitable for engine-scope variables.
      Specified by:
      createBindings in interface ScriptEngine
      Returns:
      a new SimpleBindings instance
    • getFactory

      public ScriptEngineFactory getFactory()
      Returns the factory associated with this engine, creating one lazily if necessary.
      Specified by:
      getFactory in interface ScriptEngine
      Returns:
      the script engine factory for this engine
    • compile

      public CompiledScript compile(String scriptSource) throws ScriptException
      Compiles Groovy source into a reusable CompiledScript.
      Specified by:
      compile in interface Compilable
      Parameters:
      scriptSource - the source to compile
      Returns:
      a compiled representation of scriptSource
      Throws:
      ScriptException - if compilation fails
    • compile

      public CompiledScript compile(Reader reader) throws ScriptException
      Reads and compiles Groovy source from the supplied reader.
      Specified by:
      compile in interface Compilable
      Parameters:
      reader - the reader providing script source
      Returns:
      a compiled representation of the reader content
      Throws:
      ScriptException - if the reader cannot be consumed or compilation fails
    • invokeFunction

      public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException
      Invokes a previously defined global function by name.
      Specified by:
      invokeFunction in interface Invocable
      Parameters:
      name - the global function name
      args - the arguments to pass to the function
      Returns:
      the function result
      Throws:
      ScriptException - if invocation fails
      NoSuchMethodException - if no matching function is available
    • invokeMethod

      public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException
      Invokes a method on a script object or other Groovy object.
      Specified by:
      invokeMethod in interface Invocable
      Parameters:
      thiz - the target object
      name - the method name
      args - the arguments to pass to the method
      Returns:
      the method result
      Throws:
      ScriptException - if invocation fails
      NoSuchMethodException - if no matching method is available
      IllegalArgumentException - if thiz is null
    • getInterface

      public <T> T getInterface(Class<T> clazz)
      Creates a dynamic proxy that dispatches interface method calls to global functions.
      Specified by:
      getInterface in interface Invocable
      Type Parameters:
      T - the proxy type
      Parameters:
      clazz - the interface to implement
      Returns:
      a proxy backed by this engine's global function namespace
      Throws:
      IllegalArgumentException - if clazz is null or not an interface
    • getInterface

      public <T> T getInterface(Object thiz, Class<T> clazz)
      Creates a dynamic proxy that dispatches interface method calls to the supplied object.
      Specified by:
      getInterface in interface Invocable
      Type Parameters:
      T - the proxy type
      Parameters:
      thiz - the target object that should receive method calls
      clazz - the interface to implement
      Returns:
      a proxy backed by thiz
      Throws:
      IllegalArgumentException - if thiz is null, or if clazz is null or not an interface
    • setClassLoader

      public void setClassLoader(GroovyClassLoader classLoader)
      Replaces the class loader used for future script compilation.
      Parameters:
      classLoader - the class loader to use for subsequent compilations
    • getClassLoader

      public GroovyClassLoader getClassLoader()
      Returns the class loader currently used for script compilation.
      Returns:
      the active Groovy class loader