Package: org.apache.groovy.groovysh.jline

## [Java] Class GroovyEngine

- org.apache.groovy.groovysh.jline.GroovyEngine

- All Implemented Interfaces and Traits:
    ScriptEngine

      public class GroovyEngine
      extends Object
      implements ScriptEngine

  Implements Groovy ScriptEngine. You must be very careful when using GroovyEngine in a multithreaded environment. The Binding instance is not thread safe, and it is shared by all scripts.

- - ### Nested Class Summary

    Nested classes
    Modifiers | Name | Description
    --- | --- | ---
    `protected static class` | `GroovyEngine.AccessRules` | Encapsulates access rules for code completion.
    `interface` | `GroovyEngine.Cloner` | Strategy used to clone session values for completion and inspection workflows.
    `static class` | `GroovyEngine.EngineClassLoader` | Custom class loader for dynamically loaded Groovy classes.
    `enum` | `GroovyEngine.Format` | Serialization/deserialization format options.

  - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static String` | `ALL_CLASSES_COMPLETION` | Option key to enable completion for all classes including non-public
    `static String` | `ALL_CONSTRUCTORS_COMPLETION` | Option key to enable completion for all constructors including non-public
    `static String` | `ALL_FIELDS_COMPLETION` | Option key to enable completion for all fields including non-public
    `static String` | `ALL_METHODS_COMPLETION` | Option key to enable completion for all methods including non-public
    `static String` | `CANONICAL_NAMES` | Option key for using canonical class names in descriptions
    `static String` | `GROOVY_COLORS` | Environment variable for Groovy color settings
    `static String` | `IDENTIFIERS_COMPLETION` | Option key to enable identifier completion
    `static String` | `META_METHODS_COMPLETION` | Option key to enable meta-method completion
    `static String` | `NANORC_SYNTAX` | Option key for nanorc syntax file location
    `static String` | `NANORC_VALUE` | Option key for nanorc value configuration
    `static String` | `NO_SYNTAX_CHECK` | Option key to disable syntax checking
    `static String` | `RESTRICTED_COMPLETION` | Option key for restricted completion mode
    `static String` | `SYNTHETIC_METHODS_COMPLETION` | Option key to enable synthetic method completion
    `protected GroovyEngine.EngineClassLoader` | `classLoader` | Class loader for dynamically loaded Groovy classes
    `protected Binding` | `sharedData` | Shared binding that stores variables visible to interactive scripts.

  - ### Constructor Summary

    Constructors
    Constructor and description
    `GroovyEngine()` Constructs a new GroovyEngine with default configuration.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `del(String vars)` Deletes multiple variables from the binding.
    `` | `public Object` | `deserialize(String value, String formatStr)` Deserializes a string value according to the specified format.
    `` | `public Object` | `execute(File script, Object[] args)` Executes a Groovy script from a file.
    `` | `public Object` | `execute(String statement)` Executes a Groovy statement.
    `` | `public Object` | `execute(Object closure, Object args)` Executes a Groovy closure with the given arguments.
    `` | `public Map<String, Object>` | `find(String name)` Finds variables matching the given pattern.
    `` | `public Object` | `get(String name)` Retrieves a variable from the binding.
    `` | `public String` | `getBuffer()` Returns the current executable buffer reconstructed from the recorded snippets.
    `` | `public List<String>` | `getDeserializationFormats()` Returns the list of supported deserialization formats.
    `` | `public String` | `getEngineName()` Returns the name of this script engine.
    `` | `public List<String>` | `getExtensions()` Returns the file extensions supported by this engine.
    `` | `public Map<String, String>` | `getImports()` Returns all import statements mapped to their source definitions.
    `` | `public Set<String>` | `getMethodNames()` Returns the set of defined method names.
    `` | `public Map<String, String>` | `getMethods()` Returns all defined methods mapped to their source definitions.
    `` | `public GroovyEngine.Cloner` | `getObjectCloner()` Returns the current object cloner.
    `` | `public Completer` | `getScriptCompleter()` Returns the completer for Groovy script completions.
    `` | `public List<String>` | `getSerializationFormats()` Returns the list of supported serialization formats.
    `` | `protected SyntaxHighlighter` | `getSyntaxHighlighter()` Returns the syntax highlighter for Groovy code.
    `` | `public Map<String, String>` | `getTypes()` Returns all defined types mapped to their source definitions.
    `` | `public Map<String, String>` | `getVariables()` Returns all defined variables mapped to their source definitions.
    `<T>` | `protected T` | `groovyOption(String option, T defval)` Returns the value of a specific Groovy option.
    `<T>` | `protected static T` | `groovyOption(Map<String, Object> options, String option, T defval)` Returns the value of a specific option from the given options map.
    `` | `protected Map<String, Object>` | `groovyOptions()` Returns the current Groovy options map.
    `` | `public boolean` | `hasVariable(String name)` Checks if a variable with the given name exists in the binding.
    `` | `public void` | `persist(Path file, Object object)` Persists an object to a file using the default serialization format.
    `` | `public void` | `persist(Path file, Object object, String format)` Persists an object to a file using the specified serialization format.
    `` | `public void` | `purgeClassCache(String regex)` Purges classes from the class cache matching the given regex.
    `` | `public void` | `put(String name, Object value)` Stores a variable in the binding.
    `` | `public boolean` | `refresh()` Refreshes the syntax highlighter, forcing a reload on next use.
    `` | `public void` | `removeImport(String name)` Removes an import statement by name.
    `` | `public void` | `removeMethod(String name)` Removes a method definition by name or signature.
    `` | `public void` | `removeType(String name)` Removes a type definition by name.
    `` | `public void` | `removeVariable(String name)` Removes a variable definition by name.
    `` | `public void` | `reset()` Resets the engine state, clearing all variables, methods, types, and imports.
    `` | `public CmdDesc` | `scriptDescription(CmdLine line)` Generates a description for the given command line.
    `` | `public void` | `setObjectCloner(GroovyEngine.Cloner objectCloner)` Sets the object cloner to use for copying session values.
    `` | `public String` | `toJson(Object obj)` Converts an object to JSON format.
    `` | `public Map<String, Object>` | `toMap(Object obj)` Converts an object to a map representation.
    `` | `public String` | `toString(Object obj)` Converts an object to Groovy string format.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### public static final String ALL_CLASSES_COMPLETION

      Option key to enable completion for all classes including non-public

    - #### public static final String ALL_CONSTRUCTORS_COMPLETION

      Option key to enable completion for all constructors including non-public

    - #### public static final String ALL_FIELDS_COMPLETION

      Option key to enable completion for all fields including non-public

    - #### public static final String ALL_METHODS_COMPLETION

      Option key to enable completion for all methods including non-public

    - #### public static final String CANONICAL_NAMES

      Option key for using canonical class names in descriptions

    - #### public static final String GROOVY_COLORS

      Environment variable for Groovy color settings

    - #### public static final String IDENTIFIERS_COMPLETION

      Option key to enable identifier completion

    - #### public static final String META_METHODS_COMPLETION

      Option key to enable meta-method completion

    - #### public static final String NANORC_SYNTAX

      Option key for nanorc syntax file location

    - #### public static final String NANORC_VALUE

      Option key for nanorc value configuration

    - #### public static final String NO_SYNTAX_CHECK

      Option key to disable syntax checking

    - #### public static final String RESTRICTED_COMPLETION

      Option key for restricted completion mode

    - #### public static final String SYNTHETIC_METHODS_COMPLETION

      Option key to enable synthetic method completion

    - #### protected final GroovyEngine.EngineClassLoader classLoader

      Class loader for dynamically loaded Groovy classes

    - #### protected Binding sharedData

      Shared binding that stores variables visible to interactive scripts.

  - ### Constructor Detail

    - #### public GroovyEngine()

      Constructs a new GroovyEngine with default configuration.

  - ### Method Detail

    - #### public void del(String vars)

      Deletes multiple variables from the binding.

      Parameters:
        `vars` - variable names to delete

    - #### public Object deserialize(String value, String formatStr)

      Deserializes a string value according to the specified format.

      Parameters:
        `value` - the string to deserialize
        `formatStr` - the format name (JSON, GROOVY, NONE, or AUTO). `AUTO` parses only: it recognises JSON and otherwise returns the text unchanged, and never evaluates the value as Groovy. Use `GROOVY` to evaluate.
      Returns:
        the deserialized object

    - #### public Object execute(File script, Object[] args)

      Executes a Groovy script from a file.

      throws:
        Exception if script execution fails
      Parameters:
        `script` - the script file to execute
        `args` - arguments to pass to the script
      Returns:
        the result of script execution

    - #### public Object execute(String statement)

      Executes a Groovy statement.

      throws:
        Exception if statement execution fails
      Parameters:
        `statement` - the Groovy statement to execute
      Returns:
        the result of statement execution

    - #### public Object execute(Object closure, Object args)

      Executes a Groovy closure with the given arguments.

      Parameters:
        `closure` - the closure to execute
        `args` - arguments to pass to the closure
      Returns:
        the result of closure execution

    - #### public Map<String, Object> find(String name)

      Finds variables matching the given pattern.

      Parameters:
        `name` - the variable name pattern, or `null` to return all variables
      Returns:
        a map of matching variable names to their values

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

      Retrieves a variable from the binding.

      Parameters:
        `name` - the variable name
      Returns:
        the variable value, or `null` if not found

    - #### public String getBuffer()

      Returns the current executable buffer reconstructed from the recorded snippets.

      Returns:
        buffer content for the current session

    - #### public List<String> getDeserializationFormats()

      Returns the list of supported deserialization formats.

      Returns:
        list of format names

    - #### public String getEngineName()

      Returns the name of this script engine.

      Returns:
        the engine name

    - #### public List<String> getExtensions()

      Returns the file extensions supported by this engine.

      Returns:
        list of supported file extensions

    - #### public Map<String, String> getImports()

      Returns all import statements mapped to their source definitions.

      Returns:
        map of import identifiers to their source code

    - #### public Set<String> getMethodNames()

      Returns the set of defined method names.

      Returns:
        unmodifiable set of method names

    - #### public Map<String, String> getMethods()

      Returns all defined methods mapped to their source definitions.

      Returns:
        map of method signatures to their source code

    - #### public GroovyEngine.Cloner getObjectCloner()

      Returns the current object cloner.

      Returns:
        the cloner implementation

    - #### public Completer getScriptCompleter()

      Returns the completer for Groovy script completions.

      Returns:
        a completer instance

    - #### public List<String> getSerializationFormats()

      Returns the list of supported serialization formats.

      Returns:
        list of format names

    - #### protected SyntaxHighlighter getSyntaxHighlighter()

      Returns the syntax highlighter for Groovy code.

      Returns:
        the syntax highlighter instance

    - #### public Map<String, String> getTypes()

      Returns all defined types mapped to their source definitions.

      Returns:
        map of type names to their source code

    - #### public Map<String, String> getVariables()

      Returns all defined variables mapped to their source definitions.

      Returns:
        map of variable names to their source code

    - #### <T> protected T groovyOption(String option, T defval)

      Returns the value of a specific Groovy option.

      Parameters:
        `option` - the option key
        `defval` - the default value if option not found
      Type Parameters:
        `T` - the type of the option value
      Returns:
        the option value or default

    - #### <T> protected static T groovyOption(Map<String, Object> options, String option, T defval)

      Returns the value of a specific option from the given options map.

      Parameters:
        `options` - the options map
        `option` - the option key
        `defval` - the default value if option not found
      Type Parameters:
        `T` - the type of the option value
      Returns:
        the option value or default

    - #### protected Map<String, Object> groovyOptions()

      Returns the current Groovy options map.

      Returns:
        map of option keys to values

    - #### public boolean hasVariable(String name)

      Checks if a variable with the given name exists in the binding.

      Parameters:
        `name` - the variable name to check
      Returns:
        `true` if the variable exists, `false` otherwise

    - #### public void persist(Path file, Object object)

      Persists an object to a file using the default serialization format.

      Parameters:
        `file` - the target file path
        `object` - the object to persist

    - #### public void persist(Path file, Object object, String format)

      Persists an object to a file using the specified serialization format.

      Parameters:
        `file` - the target file path
        `object` - the object to persist
        `format` - the format name to use

    - #### public void purgeClassCache(String regex)

      Purges classes from the class cache matching the given regex.

      Parameters:
        `regex` - the pattern to match class names, or null to clear all

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

      Stores a variable in the binding.

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

    - #### public boolean refresh()

      Refreshes the syntax highlighter, forcing a reload on next use.

      Returns:
        true if refresh was successful

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

      Removes an import statement by name.

      Parameters:
        `name` - the import identifier to remove

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

      Removes a method definition by name or signature.

      Parameters:
        `name` - method name or full signature to remove

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

      Removes a type definition by name.

      Parameters:
        `name` - the type name to remove

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

      Removes a variable definition by name.

      Parameters:
        `name` - the variable name to remove

    - #### public void reset()

      Resets the engine state, clearing all variables, methods, types, and imports.

    - #### public CmdDesc scriptDescription(CmdLine line)

      Generates a description for the given command line.

      Parameters:
        `line` - the command line to describe
      Returns:
        the command description

    - #### public void setObjectCloner(GroovyEngine.Cloner objectCloner)

      Sets the object cloner to use for copying session values.

      Parameters:
        `objectCloner` - the cloner implementation

    - #### public String toJson(Object obj)

      Converts an object to JSON format.

      Parameters:
        `obj` - the object to convert
      Returns:
        JSON representation

    - #### public Map<String, Object> toMap(Object obj)

      Converts an object to a map representation.

      Parameters:
        `obj` - the object to convert
      Returns:
        map representation

    - #### public String toString(Object obj)

      Converts an object to Groovy string format.

      Parameters:
        `obj` - the object to convert
      Returns:
        Groovy string representation