Package org.codehaus.groovy.jsr223
Class GroovyScriptEngineImpl
java.lang.Object
javax.script.AbstractScriptEngine
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl
- All Implemented Interfaces:
Compilable,Invocable,ScriptEngine
JSR-223 Engine implementation.
Adapted from original by Mike Grogan and A. Sundararajan
-
Field Summary
Fields inherited from class javax.script.AbstractScriptEngine
contextFields inherited from interface javax.script.ScriptEngine
ARGV, ENGINE, ENGINE_VERSION, FILENAME, LANGUAGE, LANGUAGE_VERSION, NAME -
Constructor Summary
ConstructorsConstructorDescriptionCreates an engine backed by a defaultGroovyClassLoader.GroovyScriptEngineImpl(GroovyClassLoader classLoader) Creates an engine backed by the supplied class loader. -
Method Summary
Modifier and TypeMethodDescriptionReads and compiles Groovy source from the supplied reader.Compiles Groovy source into a reusableCompiledScript.Creates a mutable bindings instance suitable for engine-scope variables.eval(Reader reader, ScriptContext ctx) Reads the supplied script source and evaluates it against the given context.eval(String script, ScriptContext ctx) Evaluates Groovy source within the supplied script context.Returns the class loader currently used for script compilation.Returns the factory associated with this engine, creating one lazily if necessary.<T> TgetInterface(Class<T> clazz) Creates a dynamic proxy that dispatches interface method calls to global functions.<T> TgetInterface(Object thiz, Class<T> clazz) Creates a dynamic proxy that dispatches interface method calls to the supplied object.invokeFunction(String name, Object... args) Invokes a previously defined global function by name.invokeMethod(Object thiz, String name, Object... args) Invokes a method on a script object or other Groovy object.voidsetClassLoader(GroovyClassLoader classLoader) Replaces the class loader used for future script compilation.Methods inherited from class javax.script.AbstractScriptEngine
eval, eval, eval, eval, get, getBindings, getContext, getScriptContext, put, setBindings, setContext
-
Constructor Details
-
GroovyScriptEngineImpl
public GroovyScriptEngineImpl()Creates an engine backed by a defaultGroovyClassLoader. -
GroovyScriptEngineImpl
Creates an engine backed by the supplied class loader.- Parameters:
classLoader- the class loader used to parse and load generated script classes- Throws:
IllegalArgumentException- ifclassLoaderisnull
-
-
Method Details
-
eval
Reads the supplied script source and evaluates it against the given context.- Specified by:
evalin interfaceScriptEngine- Parameters:
reader- the reader providing script sourcectx- 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
Evaluates Groovy source within the supplied script context.If
#jsr223.groovy.engine.keep.globalsis present in engine scope, its value controls the reference strength used for cached global closures.- Specified by:
evalin interfaceScriptEngine- Parameters:
script- the script source to executectx- 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
Creates a mutable bindings instance suitable for engine-scope variables.- Specified by:
createBindingsin interfaceScriptEngine- Returns:
- a new
SimpleBindingsinstance
-
getFactory
Returns the factory associated with this engine, creating one lazily if necessary.- Specified by:
getFactoryin interfaceScriptEngine- Returns:
- the script engine factory for this engine
-
compile
Compiles Groovy source into a reusableCompiledScript.- Specified by:
compilein interfaceCompilable- Parameters:
scriptSource- the source to compile- Returns:
- a compiled representation of
scriptSource - Throws:
ScriptException- if compilation fails
-
compile
Reads and compiles Groovy source from the supplied reader.- Specified by:
compilein interfaceCompilable- 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:
invokeFunctionin interfaceInvocable- Parameters:
name- the global function nameargs- the arguments to pass to the function- Returns:
- the function result
- Throws:
ScriptException- if invocation failsNoSuchMethodException- 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:
invokeMethodin interfaceInvocable- Parameters:
thiz- the target objectname- the method nameargs- the arguments to pass to the method- Returns:
- the method result
- Throws:
ScriptException- if invocation failsNoSuchMethodException- if no matching method is availableIllegalArgumentException- ifthizisnull
-
getInterface
Creates a dynamic proxy that dispatches interface method calls to global functions.- Specified by:
getInterfacein interfaceInvocable- 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- ifclazzisnullor not an interface
-
getInterface
Creates a dynamic proxy that dispatches interface method calls to the supplied object.- Specified by:
getInterfacein interfaceInvocable- Type Parameters:
T- the proxy type- Parameters:
thiz- the target object that should receive method callsclazz- the interface to implement- Returns:
- a proxy backed by
thiz - Throws:
IllegalArgumentException- ifthizisnull, or ifclazzisnullor not an interface
-
setClassLoader
Replaces the class loader used for future script compilation.- Parameters:
classLoader- the class loader to use for subsequent compilations
-
getClassLoader
Returns the class loader currently used for script compilation.- Returns:
- the active Groovy class loader
-