Top Description Fields Constructors Methods
groovy.lang

public Class GroovyShell

extends GroovyObjectSupport
Class Inheritance
Imports
groovy.security.GroovyCodeSourcePermission, groovy.ui.GroovyMain, org.apache.groovy.plugin.GroovyRunner, .GroovyRunnerRegistry, org.codehaus.groovy.control.CompilationFailedException, .CompilerConfiguration, org.codehaus.groovy.runtime.InvokerHelper, .InvokerInvocationException, java.io.File, .IOException, .Reader, java.lang.reflect.Constructor, .InvocationTargetException, java.net.URI, java.security.AccessController, .PrivilegedAction, .PrivilegedActionException, .PrivilegedExceptionAction, java.util.List, java.util.concurrent.atomic.AtomicInteger

Represents a groovy shell capable of running arbitrary groovy scripts

Field Summary

Modifier and TypeField and Description
private final CompilerConfiguration
private final Binding
private final AtomicInteger
public static final String
private static final String[]
private final GroovyClassLoader

Constructor Summary

AccessConstructor and Description
public
public
public
public
public
public
GroovyShell(ClassLoader parent, Binding binding)

public
public
GroovyShell(ClassLoader parent, Binding binding, final CompilerConfiguration config)

public
GroovyShell(GroovyShell
is the parent shell used for the variable bindings and the parent class loader
shell
)

Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parent

Method Summary

Modifier and TypeMethod and Description
public Object
evaluate(GroovyCodeSource codeSource)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(final String
the text of the script
scriptText
)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(String
the text of the script
scriptText
,
String
is the logical file name of the script (which is used to create the class name of the script)
fileName
)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(final String scriptText, final String fileName, final String codeBase)

Evaluates some script against the current Binding and returns the result.

public Object
evaluate(File
is the file of the script (which is used to create the class name of the script)
file
)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(URI
is the URI of the script (which is used to create the class name of the script)
uri
)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(Reader
the stream reading the script
in
)

Evaluates some script against the current Binding and returns the result

public Object
evaluate(Reader
the stream reading the script
in
,
String
is the logical file name of the script (which is used to create the class name of the script)
fileName
)

Evaluates some script against the current Binding and returns the result

protected String
public GroovyClassLoader
public Binding
public Object
getProperty(String
the name of the property of interest
property
)

Overrides default groovy.lang.GroovyObject.getProperty.

Retrieves a property value.
public Object
public static void
main(String[] args)

public Script

Returns:

the parsed script which is ready to be run via Script#run()
parse
(final Reader
the stream reading the script
reader
,
final String
is the logical file name of the script (which is used to create the class name of the script)
fileName
)

Parses the given script and returns it ready to be run

public Script

Returns:

ready to run script
parse
(final GroovyCodeSource codeSource)

Parses the given script and returns it ready to be run.

public Script
parse(File
is the file of the script (which is used to create the class name of the script)
file
)

Parses the given script and returns it ready to be run

public Script
parse(URI
is the URI of the script (which is used to create the class name of the script)
uri
)

Parses the given script and returns it ready to be run

public Script
parse(String
the text of the script
scriptText
)

Parses the given script and returns it ready to be run

public Script
parse(final String scriptText, final String fileName)

public Script
parse(Reader
the stream reading the script
in
)

Parses the given script and returns it ready to be run

private Class<T>
parseClass(final GroovyCodeSource codeSource)

Parses the groovy code contained in codeSource and returns a java class.

public void
public void
public Object
run(File
the file of the script to run
scriptFile
,
List<String>
the command line arguments to pass in
list
)

A helper method which runs the given script file with the given command line arguments

public Object
run(String
is the text content of the script
scriptText
,
String
is the logical file name of the script (which is used to create the class name of the script)
fileName
,
List<String>
the command line arguments to pass in
list
)

A helper method which runs the given cl script with the given command line arguments

public Object
run(final File
the file name of the script to run
scriptFile
,
String[]
the command line arguments to pass in
args
)

Runs the given script file name with the given command line arguments

public Object
run(final String
is the text content of the script
scriptText
,
final String
is the logical file name of the script (which is used to create the class name of the script)
fileName
,
String[]
the command line arguments to pass in
args
)

Runs the given script text with command line arguments

public Object
run(GroovyCodeSource
is the source content of the script
source
,
List<String>
the command line arguments to pass in
args
)

Runs the given script source with command line arguments

public Object
run(GroovyCodeSource
is the source content of the script
source
,
String[]
the command line arguments to pass in
args
)

Runs the given script source with command line arguments

public Object
run(URI
is the source content of the script
source
,
List<String>
the command line arguments to pass in
args
)

Runs the given script source with command line arguments

public Object
run(URI
is the source content of the script
source
,
String[]
the command line arguments to pass in
args
)

Runs the given script source with command line arguments

public Object
run(final Reader
the stream reading the script
in
,
final String
is the logical file name of the script (which is used to create the class name of the script)
fileName
,
List<String>
the command line arguments to pass in
list
)

Runs the given script with command line arguments

public Object
run(final Reader
the stream reading the script
in
,
final String
is the logical file name of the script (which is used to create the class name of the script)
fileName
,
String[]
the command line arguments to pass in
args
)

Runs the given script with command line arguments

private static Object
runRunnable(Class<T> scriptClass, String[] args)

private Object
runScriptOrMainOrTestOrRunnable(Class<T> scriptClass, String[] args)

if (theClass is a Script) { run it like a script } else if (theClass has a main method) { run the main method } else if (theClass instanceof GroovyTestCase) { use the test runner to run it } else if (theClass implements Runnable) { if (theClass has a constructor with String[] params) instantiate theClass with this constructor and run else if (theClass has a no-args constructor) instantiate theClass with the no-args constructor and run }

public void
setProperty(String
the name of the property of interest
property
,
Object
the new value for the property
newValue
)

Overrides default groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.
public void
setVariable(String name, Object value)

Inherited from groovy.lang.GroovyObjectSupport:
getMetaClasssetMetaClass

Field Detail

configback to summary
private final CompilerConfiguration config
contextback to summary
private final Binding context
counterback to summary
private final AtomicInteger counter
DEFAULT_CODE_BASEback to summary
public static final String DEFAULT_CODE_BASE
EMPTY_STRING_ARRAYback to summary
private static final String[] EMPTY_STRING_ARRAY
loaderback to summary
private final GroovyClassLoader loader

Constructor Detail

GroovyShellback to summary
public GroovyShell()
GroovyShellback to summary
public GroovyShell(Binding binding)
GroovyShellback to summary
public GroovyShell(ClassLoader parent, CompilerConfiguration config)
GroovyShellback to summary
public GroovyShell(CompilerConfiguration config)
GroovyShellback to summary
public GroovyShell(Binding binding, CompilerConfiguration config)
GroovyShellback to summary
public GroovyShell(ClassLoader parent, Binding binding)
GroovyShellback to summary
public GroovyShell(ClassLoader parent)
GroovyShellback to summary
public GroovyShell(ClassLoader parent, Binding binding, final CompilerConfiguration config)
GroovyShellback to summary
public GroovyShell(GroovyShell shell)

Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parent

Parameters
shell:GroovyShell

is the parent shell used for the variable bindings and the parent class loader

Method Detail

evaluateback to summary
public Object evaluate(GroovyCodeSource codeSource) throws CompilationFailedException

Evaluates some script against the current Binding and returns the result

evaluateback to summary
public Object evaluate(final String scriptText) throws CompilationFailedException

Evaluates some script against the current Binding and returns the result

Parameters
scriptText:String

the text of the script

evaluateback to summary
public Object evaluate(String scriptText, String fileName) throws CompilationFailedException

Evaluates some script against the current Binding and returns the result

Parameters
scriptText:String

the text of the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

evaluateback to summary
public Object evaluate(final String scriptText, final String fileName, final 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

evaluateback to summary
public Object evaluate(File file) throws CompilationFailedException, IOException

Evaluates some script against the current Binding and returns the result

Parameters
file:File

is the file of the script (which is used to create the class name of the script)

evaluateback to summary
public Object evaluate(URI uri) throws CompilationFailedException, IOException

Evaluates some script against the current Binding and returns the result

Parameters
uri:URI

is the URI of the script (which is used to create the class name of the script)

evaluateback to summary
public Object evaluate(Reader in) throws CompilationFailedException

Evaluates some script against the current Binding and returns the result

Parameters
in:Reader

the stream reading the script

evaluateback to summary
public Object evaluate(Reader in, String fileName) throws CompilationFailedException

Evaluates some script against the current Binding and returns the result

Parameters
in:Reader

the stream reading the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

generateScriptNameback to summary
protected String generateScriptName()
getClassLoaderback to summary
public GroovyClassLoader getClassLoader()
getContextback to summary
public Binding getContext()
getPropertyback to summary
public Object getProperty(String property)

Overrides default groovy.lang.GroovyObject.getProperty.

Doc from groovy.lang.GroovyObject.getProperty.

Retrieves a property value.

Parameters
property:String

the name of the property of interest

Returns:Object

the given property

getVariableback to summary
public Object getVariable(String name)
mainback to summary
public static void main(String[] args)
parseback to summary
public Script parse(final Reader reader, final String fileName) throws CompilationFailedException

Parses the given script and returns it ready to be run

Parameters
reader:Reader

the stream reading the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

Returns:Script

the parsed script which is ready to be run via Script#run()

parseback to summary
public Script parse(final GroovyCodeSource codeSource) throws CompilationFailedException

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.

Returns:Script

ready to run script

parseback to summary
public Script parse(File file) throws CompilationFailedException, IOException

Parses the given script and returns it ready to be run

Parameters
file:File

is the file of the script (which is used to create the class name of the script)

parseback to summary
public Script parse(URI uri) throws CompilationFailedException, IOException

Parses the given script and returns it ready to be run

Parameters
uri:URI

is the URI of the script (which is used to create the class name of the script)

parseback to summary
public Script parse(String scriptText) throws CompilationFailedException

Parses the given script and returns it ready to be run

Parameters
scriptText:String

the text of the script

parseback to summary
public Script parse(final String scriptText, final String fileName) throws CompilationFailedException
parseback to summary
public Script parse(Reader in) throws CompilationFailedException

Parses the given script and returns it ready to be run

Parameters
in:Reader

the stream reading the script

parseClassback to summary
private Class<T> parseClass(final GroovyCodeSource codeSource) throws CompilationFailedException

Parses the groovy code contained in codeSource and returns a java class.

removeVariableback to summary
public void removeVariable(String name)
resetLoadedClassesback to summary
public void resetLoadedClasses()
runback to summary
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:File

the file of the script to run

list:List<String>

the command line arguments to pass in

runback to summary
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:String

is the text content of the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

list:List<String>

the command line arguments to pass in

runback to summary
public Object run(final File scriptFile, String[] args) throws CompilationFailedException, IOException

Runs the given script file name with the given command line arguments

Parameters
scriptFile:File

the file name of the script to run

args:String[]

the command line arguments to pass in

runback to summary
public Object run(final String scriptText, final String fileName, String[] args) throws CompilationFailedException

Runs the given script text with command line arguments

Parameters
scriptText:String

is the text content of the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

args:String[]

the command line arguments to pass in

runback to summary
public Object run(GroovyCodeSource source, List<String> args) throws CompilationFailedException

Runs the given script source with command line arguments

Parameters
source:GroovyCodeSource

is the source content of the script

args:List<String>

the command line arguments to pass in

runback to summary
public Object run(GroovyCodeSource source, String[] args) throws CompilationFailedException

Runs the given script source with command line arguments

Parameters
source:GroovyCodeSource

is the source content of the script

args:String[]

the command line arguments to pass in

runback to summary
public Object run(URI source, List<String> args) throws CompilationFailedException, IOException

Runs the given script source with command line arguments

Parameters
source:URI

is the source content of the script

args:List<String>

the command line arguments to pass in

runback to summary
public Object run(URI source, String[] args) throws CompilationFailedException, IOException

Runs the given script source with command line arguments

Parameters
source:URI

is the source content of the script

args:String[]

the command line arguments to pass in

runback to summary
public Object run(final Reader in, final String fileName, List<String> list) throws CompilationFailedException

Runs the given script with command line arguments

Parameters
in:Reader

the stream reading the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

list:List<String>

the command line arguments to pass in

runback to summary
public Object run(final Reader in, final String fileName, String[] args) throws CompilationFailedException

Runs the given script with command line arguments

Parameters
in:Reader

the stream reading the script

fileName:String

is the logical file name of the script (which is used to create the class name of the script)

args:String[]

the command line arguments to pass in

runRunnableback to summary
private static Object runRunnable(Class<T> scriptClass, String[] args)
runScriptOrMainOrTestOrRunnableback to summary
private Object runScriptOrMainOrTestOrRunnable(Class<T> scriptClass, String[] args)

if (theClass is a Script) { run it like a script } else if (theClass has a main method) { run the main method } else if (theClass instanceof GroovyTestCase) { use the test runner to run it } else if (theClass implements Runnable) { if (theClass has a constructor with String[] params) instantiate theClass with this constructor and run else if (theClass has a no-args constructor) instantiate theClass with the no-args constructor and run }

setPropertyback to summary
public void setProperty(String property, Object newValue)

Overrides default groovy.lang.GroovyObject.setProperty.

Doc from groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.

Parameters
property:String

the name of the property of interest

newValue:Object

the new value for the property

setVariableback to summary
public void setVariable(String name, Object value)