Package: groovy.lang

## [Java] Class GroovyShell

- groovy.lang.GroovyShell

-     public class GroovyShell
      extends GroovyObjectSupport

  Represents a groovy shell capable of running arbitrary groovy scripts

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static String` | `DEFAULT_CODE_BASE` | Default code base used for scripts evaluated by the shell.

  - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovyShell()` Creates a shell with a new Binding and the default compiler configuration.
    `GroovyShell(Binding binding)` Creates a shell with the supplied binding and the default 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 new Binding and the supplied compiler configuration.
    `GroovyShell(Binding binding, CompilerConfiguration config)` Creates a shell with the supplied binding and compiler configuration.
    `GroovyShell(ClassLoader parent, Binding binding)` Creates a shell with the supplied parent class loader and binding.
    `GroovyShell(ClassLoader parent)` Creates a shell with the supplied parent class loader, a new binding and the default compiler configuration.
    `GroovyShell(ClassLoader parent, Binding binding, CompilerConfiguration config)` Creates a shell with explicit class loader, 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 parent

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public Object` | `evaluate(GroovyCodeSource codeSource)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(String scriptText)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(String scriptText, String fileName)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(String scriptText, String fileName, String codeBase)` Evaluates some script against the current Binding and returns the result.
    `` | `public Object` | `evaluate(File file)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(URI uri)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(Reader in)` Evaluates some script against the current Binding and returns the result
    `` | `public Object` | `evaluate(Reader in, String fileName)` Evaluates some script against the current Binding and returns the result
    `` | `protected String` | `generateScriptName()` Generates a unique logical script name for anonymous shell evaluations.
    `` | `public GroovyClassLoader` | `getClassLoader()` Returns the class loader used to parse and load scripts.
    `` | `public Binding` | `getContext()` Returns the binding used as this shell's execution context.
    `` | `public Object` | `getProperty(String property)` Retrieves a property value. Parameters: `propertyName` - the name of the property of interest Returns: the given property
    `` | `public Object` | `getVariable(String name)` Returns the value of a variable from the shell binding.
    `` | `public static void` | `main(String[] args)` Runs the standard `groovysh`-style command-line entry point.
    `` | `public Script` | `parse(Reader reader, String fileName)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(Reader reader, String fileName, Binding binding)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(GroovyCodeSource codeSource, Binding binding)` Parses the given script and returns it ready to be run.
    `` | `public Script` | `parse(GroovyCodeSource codeSource)` Parses the given script and returns it ready to be run.
    `` | `public Script` | `parse(File file)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(URI uri)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(String scriptText, Binding binding)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(String scriptText)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(String scriptText, String fileName, Binding binding)` Parses script text using an explicit logical name and binding.
    `` | `public Script` | `parse(String scriptText, String fileName)` Parses script text using an explicit logical name and this shell's binding.
    `` | `public Script` | `parse(Reader in)` Parses the given script and returns it ready to be run
    `` | `public Script` | `parse(Reader in, Binding binding)` Parses the given script and returns it ready to be run
    `` | `public void` | `removeVariable(String name)` Removes a variable from the shell binding.
    `` | `public void` | `resetLoadedClasses()` Clears classes previously loaded by this shell class loader.
    `` | `public Object` | `run(File scriptFile, List<String> list)` A helper method which runs the given script file with the given command line arguments
    `` | `public Object` | `run(String scriptText, String fileName, List<String> list)` A helper method which runs the given cl script with the given command line arguments
    `` | `public Object` | `run(File scriptFile, String[] args)` Runs the given script file name with the given command line arguments
    `` | `public Object` | `run(String scriptText, String fileName, String[] args)` Runs the given script text with command line arguments
    `` | `public Object` | `run(GroovyCodeSource source, List<String> args)` Runs the given script source with command line arguments
    `` | `public Object` | `run(GroovyCodeSource source, String[] args)` Runs the given script source with command line arguments
    `` | `public Object` | `run(URI source, List<String> args)` Runs the given script source with command line arguments
    `` | `public Object` | `run(URI source, String[] args)` Runs the given script source with command line arguments
    `` | `public Object` | `run(Reader in, String fileName, List<String> list)` Runs the given script with command line arguments
    `` | `public Object` | `run(Reader in, String fileName, String[] args)` Runs the given script with command line arguments
    `` | `public void` | `setProperty(String property, Object newValue)` Sets the given property to the new value. Parameters: `propertyName` - the name of the property of interest `newValue` - the new value for the property
    `` | `public void` | `setVariable(String name, Object value)` Stores a variable in the shell binding.
    `` | `public static GroovyShell` | `withConfig(Closure<Void> spec)` Since: 4.0.3

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class GroovyObjectSupport` | `getMetaClass, setMetaClass`

- - ### Field Detail

    - #### public static final String DEFAULT_CODE_BASE

      Default code base used for scripts evaluated by the shell.

  - ### Constructor Detail

    - #### public GroovyShell()

      Creates a shell with a new Binding and the default compiler configuration.

    - #### public GroovyShell(Binding binding)

      Creates a shell with the supplied binding and the default compiler configuration.

      Parameters:
        `binding` - the binding used as the shell context

    - #### public GroovyShell(ClassLoader parent, CompilerConfiguration config)

      Creates a shell with the supplied parent class loader and compiler configuration.

      Parameters:
        `parent` - the parent class loader
        `config` - the compiler configuration to use

    - #### public GroovyShell(CompilerConfiguration config)

      Creates a shell with a new Binding and the supplied compiler configuration.

      Parameters:
        `config` - the compiler configuration to use

    - #### public GroovyShell(Binding binding, CompilerConfiguration config)

      Creates a shell with the supplied binding and compiler configuration.

      Parameters:
        `binding` - the binding used as the shell context
        `config` - the compiler configuration to use

    - #### public GroovyShell(ClassLoader parent, Binding binding)

      Creates a shell with the supplied parent class loader and binding.

      Parameters:
        `parent` - the parent class loader
        `binding` - the binding used as the shell context

    - #### public GroovyShell(ClassLoader parent)

      Creates a shell with the supplied parent class loader, a new binding and the default compiler configuration.

      Parameters:
        `parent` - the parent class loader

    - #### public GroovyShell(ClassLoader parent, Binding binding, CompilerConfiguration config)

      Creates a shell with explicit class loader, binding and compiler configuration.

      Parameters:
        `parent` - the parent class loader, or `null` to use the shell class loader
        `binding` - the binding used as the shell context
        `config` - the compiler configuration to use

    - #### 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

    - #### public Object evaluate(GroovyCodeSource codeSource)

      Evaluates some script against the current Binding and returns the result

      throws:
        CompilationFailedException
      Parameters:
        codeSource

    - #### public Object evaluate(String scriptText)

      Evaluates some script against the current Binding and returns the result

      Parameters:
        `scriptText` - the text of the script

    - #### public Object evaluate(String scriptText, String fileName)

      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)

    - #### public Object evaluate(String scriptText, String fileName, String codeBase)

      Evaluates some script against the current Binding and returns the result. The .class file created from the script is given the supplied codeBase

    - #### public Object evaluate(File file)

      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)

    - #### public Object evaluate(URI uri)

      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)

    - #### public Object evaluate(Reader in)

      Evaluates some script against the current Binding and returns the result

      Parameters:
        `in` - the stream reading the script

    - #### public Object evaluate(Reader in, String fileName)

      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)

    - #### protected String generateScriptName()

      Generates a unique logical script name for anonymous shell evaluations.

      Returns:
        a unique script name ending in `.groovy`

    - #### public GroovyClassLoader getClassLoader()

      Returns the class loader used to parse and load scripts.

      Returns:
        the shell class loader

    - #### public Binding getContext()

      Returns the binding used as this shell's execution context.

      Returns:
        the current binding

    - #### public Object getProperty(String property)

      Retrieves a property value.

      Parameters:
        `propertyName` - the name of the property of interest
      Returns:
        the given property

    - #### public Object getVariable(String name)

      Returns the value of a variable from the shell binding.

      Parameters:
        `name` - the variable name
      Returns:
        the current variable value, or `null` if it is not bound

    - #### public static void main(String[] args)

      Runs the standard `groovysh`-style command-line entry point.

      Parameters:
        `args` - command-line arguments passed to GroovyMain

    - #### public Script parse(Reader reader, String fileName)

      Parses the given script and returns it ready to be run

      Parameters:
        `reader` - 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 Script.run

    - #### public Script parse(Reader reader, String fileName, Binding binding)

      Parses the given script and returns it ready to be run

      Parameters:
        `reader` - 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)
        `binding` - the context eval the script
      Returns:
        the parsed script which is ready to be run via Script.run

    - #### public Script parse(GroovyCodeSource codeSource, Binding binding)

      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

    - #### public Script parse(GroovyCodeSource codeSource)

      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

    - #### public Script parse(File file)

      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)

    - #### public Script parse(URI uri)

      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)

    - #### public Script parse(String scriptText, Binding binding)

      Parses the given script and returns it ready to be run

      Parameters:
        `scriptText` - the text of the script
        `binding` - the context eval the script

    - #### public Script parse(String scriptText)

      Parses the given script and returns it ready to be run

      Parameters:
        `scriptText` - the text of the script

    - #### public Script parse(String scriptText, String fileName, Binding binding)

      Parses script text using an explicit logical name and binding.

      throws:
        CompilationFailedException if compilation fails
      Parameters:
        `scriptText` - the Groovy source text
        `fileName` - the logical script name
        `binding` - the binding to associate with the parsed script
      Returns:
        the parsed script instance

    - #### public Script parse(String scriptText, String fileName)

      Parses script text using an explicit logical name and this shell's binding.

      throws:
        CompilationFailedException if compilation fails
      Parameters:
        `scriptText` - the Groovy source text
        `fileName` - the logical script name
      Returns:
        the parsed script instance

    - #### public Script parse(Reader in)

      Parses the given script and returns it ready to be run

      Parameters:
        `in` - the stream reading the script

    - #### public Script parse(Reader in, Binding binding)

      Parses the given script and returns it ready to be run

      Parameters:
        `in` - the stream reading the script
        `binding` - the context eval the script

    - #### public void removeVariable(String name)

      Removes a variable from the shell binding.

      Parameters:
        `name` - the variable name

    - #### public void resetLoadedClasses()

      Clears classes previously loaded by this shell class loader.

    - #### public Object run(File scriptFile, List<String> list)

      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

    - #### public Object run(String scriptText, String fileName, List<String> list)

      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

    - #### public Object run(File scriptFile, String[] args)

      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

    - #### public Object run(String scriptText, String fileName, String[] args)

      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

    - #### public Object run(GroovyCodeSource source, List<String> args)

      Runs the given script source with command line arguments

      Parameters:
        `source` - is the source content of the script
        `args` - the command line arguments to pass in

    - #### public Object run(GroovyCodeSource source, String[] args)

      Runs the given script source with command line arguments

      Parameters:
        `source` - is the source content of the script
        `args` - the command line arguments to pass in

    - #### public Object run(URI source, List<String> args)

      Runs the given script source with command line arguments

      Parameters:
        `source` - is the source content of the script
        `args` - the command line arguments to pass in

    - #### public Object run(URI source, String[] args)

      Runs the given script source with command line arguments

      Parameters:
        `source` - is the source content of the script
        `args` - the command line arguments to pass in

    - #### public Object run(Reader in, String fileName, List<String> list)

      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)
        `list` - the command line arguments to pass in

    - #### public Object run(Reader in, String fileName, String[] args)

      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

    - #### public void setProperty(String property, Object newValue)

      Sets the given property to the new value.

      Parameters:
        `propertyName` - the name of the property of interest
        `newValue` - the new value for the property

    - #### public void setVariable(String name, Object value)

      Stores a variable in the shell binding.

      Parameters:
        `name` - the variable name
        `value` - the value to bind

    - #### public static GroovyShell withConfig(@DelegatesTo(type = CONFIGURATION_CUSTOMIZER)
      Closure<Void> spec)

      Since:
        4.0.3