groovy.lang
Class GroovyShell

java.lang.Object
  extended bygroovy.lang.GroovyObjectSupport
      extended bygroovy.lang.GroovyShell
All Implemented Interfaces:
GroovyObject

public class GroovyShell
extends GroovyObjectSupport

Represents a groovy shell capable of running arbitrary groovy scripts

Version:
$Revision: 1.41 $
Author:
James Strachan, Guillaume Laforge

Field Summary
private  Binding context
           
private  int counter
           
static java.lang.String[] EMPTY_ARGS
           
private  GroovyClassLoader loader
           
 
Fields inherited from class groovy.lang.GroovyObjectSupport
 
Constructor Summary
GroovyShell()
           
GroovyShell(Binding binding)
           
GroovyShell(Binding binding, CompilerConfiguration config)
           
GroovyShell(java.lang.ClassLoader parent)
           
GroovyShell(java.lang.ClassLoader parent, Binding binding)
           
GroovyShell(java.lang.ClassLoader parent, Binding binding, CompilerConfiguration config)
           
GroovyShell(CompilerConfiguration config)
           
GroovyShell(GroovyShell shell)
          Creates a child shell using a new ClassLoader which uses the parent shell's class loader as its parent
 
Method Summary
 java.lang.Object evaluate(java.io.File file)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(GroovyCodeSource codeSource)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(java.io.InputStream in)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(java.io.InputStream in, java.lang.String fileName)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(java.lang.String scriptText)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(java.lang.String scriptText, java.lang.String fileName)
          Evaluates some script against the current Binding and returns the result
 java.lang.Object evaluate(java.lang.String scriptText, java.lang.String fileName, java.lang.String codeBase)
          Evaluates some script against the current Binding and returns the result.
protected  java.lang.String generateScriptName()
           
 Binding getContext()
           
 java.lang.Object getProperty(java.lang.String property)
           
 java.lang.Object getVariable(java.lang.String name)
           
private  boolean isUnitTestCase(java.lang.Class scriptClass)
          Utility method to check through reflection if the parsed class extends GroovyTestCase.
static void main(java.lang.String[] args)
           
 Script parse(java.io.File file)
          Parses the given script and returns it ready to be run
 Script parse(GroovyCodeSource codeSource)
          Parses the given script and returns it ready to be run.
 Script parse(java.io.InputStream in)
          Parses the given script and returns it ready to be run
 Script parse(java.io.InputStream in, java.lang.String fileName)
          Parses the given script and returns it ready to be run
 Script parse(java.lang.String scriptText)
          Parses the given script and returns it ready to be run
 Script parse(java.lang.String scriptText, java.lang.String fileName)
           
private  java.lang.Class parseClass(GroovyCodeSource codeSource)
          Parses the groovy code contained in codeSource and returns a java class.
 void run(java.io.File scriptFile, java.util.List list)
          A helper method which runs the given script file with the given command line arguments
 void run(java.io.File scriptFile, java.lang.String[] args)
          Runs the given script file name with the given command line arguments
 java.lang.Object run(java.io.InputStream in, java.lang.String fileName, java.lang.String[] args)
          Runs the given script with command line arguments
 void run(java.lang.String scriptText, java.lang.String fileName, java.util.List list)
          A helper method which runs the given cl script with the given command line arguments
 void run(java.lang.String scriptText, java.lang.String fileName, java.lang.String[] args)
          Runs the given script text with command line arguments
private  void runMainOrTestOrRunnable(java.lang.Class scriptClass, java.lang.String[] args)
          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) instanciate theClass with this constructor and run else if (theClass has a no-args constructor) instanciate theClass with the no-args constructor and run }
private  void runTest(java.lang.Class scriptClass)
          Run the specified class extending GroovyTestCase as a unit test.
 void setProperty(java.lang.String property, java.lang.Object newValue)
          Sets the given property to the new value
 void setVariable(java.lang.String name, java.lang.Object value)
           
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, invokeMethod, setMetaClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ARGS

public static final java.lang.String[] EMPTY_ARGS

loader

private GroovyClassLoader loader

context

private Binding context

counter

private int counter
Constructor Detail

GroovyShell

public GroovyShell()

GroovyShell

public GroovyShell(Binding binding)

GroovyShell

public GroovyShell(CompilerConfiguration config)

GroovyShell

public GroovyShell(Binding binding,
                   CompilerConfiguration config)

GroovyShell

public GroovyShell(java.lang.ClassLoader parent,
                   Binding binding)

GroovyShell

public GroovyShell(java.lang.ClassLoader parent)

GroovyShell

public GroovyShell(java.lang.ClassLoader parent,
                   Binding binding,
                   CompilerConfiguration config)

GroovyShell

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 - is the parent shell used for the variable bindings and the parent class loader
Method Detail

main

public static void main(java.lang.String[] args)

getContext

public Binding getContext()

getProperty

public java.lang.Object getProperty(java.lang.String property)
Specified by:
getProperty in interface GroovyObject
Overrides:
getProperty in class GroovyObjectSupport

setProperty

public void setProperty(java.lang.String property,
                        java.lang.Object newValue)
Description copied from interface: GroovyObject
Sets the given property to the new value

Specified by:
setProperty in interface GroovyObject
Overrides:
setProperty in class GroovyObjectSupport

run

public void run(java.io.File scriptFile,
                java.util.List list)
         throws CompilationFailedException,
                java.io.IOException
A helper method which runs the given script file with the given command line arguments

Parameters:
scriptFile - the file of the script to run
list - the command line arguments to pass in
Throws:
CompilationFailedException
java.io.IOException

run

public void run(java.lang.String scriptText,
                java.lang.String fileName,
                java.util.List 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 script
fileName - 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

public void run(java.io.File scriptFile,
                java.lang.String[] args)
         throws CompilationFailedException,
                java.io.IOException
Runs the given script file name with the given command line arguments

Parameters:
scriptFile - the file name of the script to run
args - the command line arguments to pass in
Throws:
CompilationFailedException
java.io.IOException

runMainOrTestOrRunnable

private void runMainOrTestOrRunnable(java.lang.Class scriptClass,
                                     java.lang.String[] args)
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) instanciate theClass with this constructor and run else if (theClass has a no-args constructor) instanciate theClass with the no-args constructor and run }


runTest

private void runTest(java.lang.Class scriptClass)
Run the specified class extending GroovyTestCase as a unit test. This is done through reflection, to avoid adding a dependency to the JUnit framework. Otherwise, developers embedding Groovy and using GroovyShell to load/parse/compile groovy scripts and classes would have to add another dependency on their classpath.

Parameters:
scriptClass - the class to be run as a unit test

isUnitTestCase

private boolean isUnitTestCase(java.lang.Class scriptClass)
Utility method to check through reflection if the parsed class extends GroovyTestCase.

Parameters:
scriptClass - the class we want to know if it extends GroovyTestCase
Returns:
true if the class extends groovy.util.GroovyTestCase

run

public void run(java.lang.String scriptText,
                java.lang.String fileName,
                java.lang.String[] args)
         throws CompilationFailedException
Runs the given script text with command line arguments

Parameters:
scriptText - is the text content of the script
fileName - 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

public java.lang.Object run(java.io.InputStream in,
                            java.lang.String fileName,
                            java.lang.String[] args)
                     throws CompilationFailedException
Runs the given script with command line arguments

Parameters:
in - the stream reading the script
fileName - 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

public java.lang.Object getVariable(java.lang.String name)

setVariable

public void setVariable(java.lang.String name,
                        java.lang.Object value)

evaluate

public java.lang.Object evaluate(GroovyCodeSource codeSource)
                          throws CompilationFailedException
Evaluates some script against the current Binding and returns the result

Parameters:
codeSource -
Returns:
Throws:
CompilationFailedException
java.io.IOException

evaluate

public java.lang.Object evaluate(java.lang.String scriptText,
                                 java.lang.String fileName)
                          throws CompilationFailedException
Evaluates some script against the current Binding and returns the result

Parameters:
scriptText - the text of the script
fileName - is the logical file name of the script (which is used to create the class name of the script)
Throws:
CompilationFailedException

evaluate

public java.lang.Object evaluate(java.lang.String scriptText,
                                 java.lang.String fileName,
                                 java.lang.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

public java.lang.Object evaluate(java.io.File file)
                          throws CompilationFailedException,
                                 java.io.IOException
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:
CompilationFailedException
java.io.IOException

evaluate

public java.lang.Object evaluate(java.lang.String scriptText)
                          throws CompilationFailedException
Evaluates some script against the current Binding and returns the result

Parameters:
scriptText - the text of the script
Throws:
CompilationFailedException

evaluate

public java.lang.Object evaluate(java.io.InputStream in)
                          throws CompilationFailedException
Evaluates some script against the current Binding and returns the result

Parameters:
in - the stream reading the script
Throws:
CompilationFailedException

evaluate

public java.lang.Object evaluate(java.io.InputStream in,
                                 java.lang.String fileName)
                          throws CompilationFailedException
Evaluates some script against the current Binding and returns the result

Parameters:
in - the stream reading the script
fileName - is the logical file name of the script (which is used to create the class name of the script)
Throws:
CompilationFailedException

parse

public Script parse(java.io.InputStream in,
                    java.lang.String fileName)
             throws CompilationFailedException
Parses the given script and returns it ready to be run

Parameters:
in - the stream reading the script
fileName - 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 @link Script.run()
Throws:
CompilationFailedException

parseClass

private java.lang.Class parseClass(GroovyCodeSource codeSource)
                            throws CompilationFailedException
Parses the groovy code contained in codeSource and returns a java class.

Throws:
CompilationFailedException

parse

public Script parse(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.

Parameters:
codeSource -
Returns:
Throws:
CompilationFailedException

parse

public Script parse(java.io.File file)
             throws CompilationFailedException,
                    java.io.IOException
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:
CompilationFailedException
java.io.IOException

parse

public Script parse(java.lang.String scriptText)
             throws CompilationFailedException
Parses the given script and returns it ready to be run

Parameters:
scriptText - the text of the script
Throws:
CompilationFailedException

parse

public Script parse(java.lang.String scriptText,
                    java.lang.String fileName)
             throws CompilationFailedException
Throws:
CompilationFailedException

parse

public Script parse(java.io.InputStream in)
             throws CompilationFailedException
Parses the given script and returns it ready to be run

Parameters:
in - the stream reading the script
Throws:
CompilationFailedException

generateScriptName

protected java.lang.String generateScriptName()


Copyright © 2003-2005 The Codehaus. All Rights Reserved.