Package groovy.lang
Class GroovyShell
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.lang.GroovyShell
- All Implemented Interfaces:
GroovyObject
Represents a groovy shell capable of running arbitrary groovy scripts
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault code base used for scripts evaluated by the shell. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a shell with a newBindingand the default compiler configuration.GroovyShell(Binding binding) Creates a shell with the supplied binding and the default compiler configuration.GroovyShell(Binding binding, CompilerConfiguration config) Creates a shell with the supplied binding and compiler configuration.GroovyShell(GroovyShell shell) Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parentGroovyShell(ClassLoader parent) Creates a shell with the supplied parent class loader, a new binding and the default compiler configuration.GroovyShell(ClassLoader parent, Binding binding) Creates a shell with the supplied parent class loader and binding.GroovyShell(ClassLoader parent, Binding binding, CompilerConfiguration config) Creates a shell with explicit class loader, binding and compiler configuration.GroovyShell(ClassLoader parent, CompilerConfiguration config) Creates a shell with the supplied parent class loader and compiler configuration.GroovyShell(CompilerConfiguration config) Creates a shell with a newBindingand the supplied compiler configuration. -
Method Summary
Modifier and TypeMethodDescriptionevaluate(GroovyCodeSource codeSource) Evaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the resultEvaluates some script against the current Binding and returns the result.Evaluates some script against the current Binding and returns the resultprotected StringGenerates a unique logical script name for anonymous shell evaluations.Returns the class loader used to parse and load scripts.Returns the binding used as this shell's execution context.getProperty(String property) Retrieves a property value.getVariable(String name) Returns the value of a variable from the shell binding.static voidRuns the standardgroovysh-style command-line entry point.parse(GroovyCodeSource codeSource) Parses the given script and returns it ready to be run.parse(GroovyCodeSource codeSource, Binding binding) Parses the given script and returns it ready to be run.Parses the given script and returns it ready to be runParses the given script and returns it ready to be runParses the given script and returns it ready to be runParses the given script and returns it ready to be runParses the given script and returns it ready to be runParses the given script and returns it ready to be runParses the given script and returns it ready to be runParses script text using an explicit logical name and this shell's binding.Parses script text using an explicit logical name and binding.Parses the given script and returns it ready to be runvoidremoveVariable(String name) Removes a variable from the shell binding.voidClears classes previously loaded by this shell class loader.run(GroovyCodeSource source, String[] args) Runs the given script source with command line argumentsrun(GroovyCodeSource source, List<String> args) Runs the given script source with command line argumentsRuns the given script file name with the given command line argumentsA helper method which runs the given script file with the given command line argumentsRuns the given script with command line argumentsRuns the given script with command line argumentsRuns the given script text with command line argumentsA helper method which runs the given cl script with the given command line argumentsRuns the given script source with command line argumentsRuns the given script source with command line argumentsvoidsetProperty(String property, Object newValue) Sets the given property to the new value.voidsetVariable(String name, Object value) Stores a variable in the shell binding.static GroovyShellwithConfig(Closure<Void> spec) Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface groovy.lang.GroovyObject
invokeMethod
-
Field Details
-
DEFAULT_CODE_BASE
Default code base used for scripts evaluated by the shell.- See Also:
-
-
Constructor Details
-
GroovyShell
public GroovyShell()Creates a shell with a newBindingand the default compiler configuration. -
GroovyShell
Creates a shell with the supplied binding and the default compiler configuration.- Parameters:
binding- the binding used as the shell context
-
GroovyShell
Creates a shell with the supplied parent class loader and compiler configuration.- Parameters:
parent- the parent class loaderconfig- the compiler configuration to use
-
GroovyShell
Creates a shell with a newBindingand the supplied compiler configuration.- Parameters:
config- the compiler configuration to use
-
GroovyShell
Creates a shell with the supplied binding and compiler configuration.- Parameters:
binding- the binding used as the shell contextconfig- the compiler configuration to use
-
GroovyShell
Creates a shell with the supplied parent class loader and binding.- Parameters:
parent- the parent class loaderbinding- the binding used as the shell context
-
GroovyShell
Creates a shell with the supplied parent class loader, a new binding and the default compiler configuration.- Parameters:
parent- the parent class loader
-
GroovyShell
Creates a shell with explicit class loader, binding and compiler configuration.- Parameters:
parent- the parent class loader, ornullto use the shell class loaderbinding- the binding used as the shell contextconfig- the compiler configuration to use
-
GroovyShell
Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parent- Parameters:
shell- is the parent shell used for the variable bindings and the parent class loader
-
-
Method Details
-
main
Runs the standardgroovysh-style command-line entry point.- Parameters:
args- command-line arguments passed toGroovyMain
-
withConfig
public static GroovyShell withConfig(@DelegatesTo(type="org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder") Closure<Void> spec) - Since:
- 4.0.3
-
resetLoadedClasses
public void resetLoadedClasses()Clears classes previously loaded by this shell class loader. -
getContext
Returns the binding used as this shell's execution context.- Returns:
- the current binding
-
getClassLoader
Returns the class loader used to parse and load scripts.- Returns:
- the shell class loader
-
getProperty
Retrieves a property value.- Parameters:
property- the name of the property of interest- Returns:
- the given property
-
setProperty
Sets the given property to the new value.- Parameters:
property- the name of the property of interestnewValue- the new value for the property
-
run
public Object run(File scriptFile, List<String> list) throws CompilationFailedException, IOException A helper method which runs the given script file with the given command line arguments- Parameters:
scriptFile- the file of the script to runlist- the command line arguments to pass in- Throws:
CompilationFailedExceptionIOException
-
run
public Object run(String scriptText, String fileName, List<String> list) throws CompilationFailedException A helper method which runs the given cl script with the given command line arguments- Parameters:
scriptText- is the text content of the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)list- the command line arguments to pass in- Throws:
CompilationFailedException
-
run
Runs the given script file name with the given command line arguments- Parameters:
scriptFile- the file name of the script to runargs- the command line arguments to pass in- Throws:
CompilationFailedExceptionIOException
-
run
public Object run(String scriptText, String fileName, String[] args) throws CompilationFailedException Runs the given script text with command line arguments- Parameters:
scriptText- is the text content of the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)args- the command line arguments to pass in- Throws:
CompilationFailedException
-
run
Runs the given script source with command line arguments- Parameters:
source- is the source content of the scriptargs- the command line arguments to pass in- Throws:
CompilationFailedException
-
run
Runs the given script source with command line arguments- Parameters:
source- is the source content of the scriptargs- the command line arguments to pass in- Throws:
CompilationFailedException
-
run
Runs the given script source with command line arguments- Parameters:
source- is the source content of the scriptargs- the command line arguments to pass in- Throws:
CompilationFailedExceptionIOException
-
run
Runs the given script source with command line arguments- Parameters:
source- is the source content of the scriptargs- the command line arguments to pass in- Throws:
CompilationFailedExceptionIOException
-
run
Runs the given script with command line arguments- Parameters:
in- the stream reading the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)list- the command line arguments to pass in- Throws:
CompilationFailedException
-
run
Runs the given script with command line arguments- Parameters:
in- the stream reading the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)args- the command line arguments to pass in- Throws:
CompilationFailedException
-
getVariable
Returns the value of a variable from the shell binding.- Parameters:
name- the variable name- Returns:
- the current variable value, or
nullif it is not bound
-
setVariable
Stores a variable in the shell binding.- Parameters:
name- the variable namevalue- the value to bind
-
removeVariable
Removes a variable from the shell binding.- Parameters:
name- the variable name
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
codeSource-- Throws:
CompilationFailedException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
scriptText- the text of the script- Throws:
CompilationFailedException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
scriptText- the text of the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)- Throws:
CompilationFailedException
-
evaluate
public Object evaluate(String scriptText, String fileName, String codeBase) throws CompilationFailedException Evaluates some script against the current Binding and returns the result. The .class file created from the script is given the supplied codeBase- Throws:
CompilationFailedException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
file- is the file of the script (which is used to create the class name of the script)- Throws:
CompilationFailedExceptionIOException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
uri- is the URI of the script (which is used to create the class name of the script)- Throws:
CompilationFailedExceptionIOException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
in- the stream reading the script- Throws:
CompilationFailedException
-
evaluate
Evaluates some script against the current Binding and returns the result- Parameters:
in- the stream reading the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)- Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run- Parameters:
reader- the stream reading the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)- Returns:
- the parsed script which is ready to be run via
Script.run() - Throws:
CompilationFailedException
-
parse
public Script parse(Reader reader, String fileName, Binding binding) throws CompilationFailedException Parses the given script and returns it ready to be run- Parameters:
reader- the stream reading the scriptfileName- is the logical file name of the script (which is used to create the class name of the script)binding- the context eval the script- Returns:
- the parsed script which is ready to be run via
Script.run() - Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run. When running in a secure environment (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be given to the script.- Parameters:
codeSource-binding-- Returns:
- ready to run script
- Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run. When running in a secure environment (-Djava.security.manager) codeSource.getCodeSource() determines what policy grants should be given to the script.- Parameters:
codeSource-- Returns:
- ready to run script
- Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run- Parameters:
file- is the file of the script (which is used to create the class name of the script)- Throws:
CompilationFailedExceptionIOException
-
parse
Parses the given script and returns it ready to be run- Parameters:
uri- is the URI of the script (which is used to create the class name of the script)- Throws:
CompilationFailedExceptionIOException
-
parse
Parses the given script and returns it ready to be run- Parameters:
scriptText- the text of the scriptbinding- the context eval the script- Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run- Parameters:
scriptText- the text of the script- Throws:
CompilationFailedException
-
parse
public Script parse(String scriptText, String fileName, Binding binding) throws CompilationFailedException Parses script text using an explicit logical name and binding.- Parameters:
scriptText- the Groovy source textfileName- the logical script namebinding- the binding to associate with the parsed script- Returns:
- the parsed script instance
- Throws:
CompilationFailedException- if compilation fails
-
parse
Parses script text using an explicit logical name and this shell's binding.- Parameters:
scriptText- the Groovy source textfileName- the logical script name- Returns:
- the parsed script instance
- Throws:
CompilationFailedException- if compilation fails
-
parse
Parses the given script and returns it ready to be run- Parameters:
in- the stream reading the script- Throws:
CompilationFailedException
-
parse
Parses the given script and returns it ready to be run- Parameters:
in- the stream reading the scriptbinding- the context eval the script- Throws:
CompilationFailedException
-
generateScriptName
Generates a unique logical script name for anonymous shell evaluations.- Returns:
- a unique script name ending in
.groovy
-