Package org.apache.groovy.groovysh.jline

# Class GroovyEngine

java.lang.Object
org.apache.groovy.groovysh.jline.GroovyEngine

All Implemented Interfaces:
  `org.jline.console.ScriptEngine`

public class GroovyEngine extends Object implements org.jline.console.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
  Modifier and Type
  Class
  Description
  `protected static class`
  `GroovyEngine.AccessRules`
  Encapsulates access rules for code completion.
  `static 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.
  `static enum`
  `GroovyEngine.Format`
  Serialization/deserialization format options.

- ## Field Summary

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

- ## Constructor Summary

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

- ## Method Summary

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

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.jline.console.ScriptEngine

  `deserialize, execute, execute, execute, find`

- ## Field Details

  - ### CANONICAL_NAMES

    public static final String CANONICAL_NAMES
    Option key for using canonical class names in descriptions
    See Also:
      - Constant Field Values

  - ### NANORC_SYNTAX

    public static final String NANORC_SYNTAX
    Option key for nanorc syntax file location
    See Also:
      - Constant Field Values

  - ### NANORC_VALUE

    public static final String NANORC_VALUE
    Option key for nanorc value configuration
    See Also:
      - Constant Field Values

  - ### GROOVY_COLORS

    public static final String GROOVY_COLORS
    Environment variable for Groovy color settings
    See Also:
      - Constant Field Values

  - ### NO_SYNTAX_CHECK

    public static final String NO_SYNTAX_CHECK
    Option key to disable syntax checking
    See Also:
      - Constant Field Values

  - ### RESTRICTED_COMPLETION

    public static final String RESTRICTED_COMPLETION
    Option key for restricted completion mode
    See Also:
      - Constant Field Values

  - ### ALL_FIELDS_COMPLETION

    public static final String ALL_FIELDS_COMPLETION
    Option key to enable completion for all fields including non-public
    See Also:
      - Constant Field Values

  - ### ALL_METHODS_COMPLETION

    public static final String ALL_METHODS_COMPLETION
    Option key to enable completion for all methods including non-public
    See Also:
      - Constant Field Values

  - ### ALL_CONSTRUCTORS_COMPLETION

    public static final String ALL_CONSTRUCTORS_COMPLETION
    Option key to enable completion for all constructors including non-public
    See Also:
      - Constant Field Values

  - ### ALL_CLASSES_COMPLETION

    public static final String ALL_CLASSES_COMPLETION
    Option key to enable completion for all classes including non-public
    See Also:
      - Constant Field Values

  - ### IDENTIFIERS_COMPLETION

    public static final String IDENTIFIERS_COMPLETION
    Option key to enable identifier completion
    See Also:
      - Constant Field Values

  - ### META_METHODS_COMPLETION

    public static final String META_METHODS_COMPLETION
    Option key to enable meta-method completion
    See Also:
      - Constant Field Values

  - ### SYNTHETIC_METHODS_COMPLETION

    public static final String SYNTHETIC_METHODS_COMPLETION
    Option key to enable synthetic method completion
    See Also:
      - Constant Field Values

  - ### sharedData

    protected Binding sharedData
    Shared binding that stores variables visible to interactive scripts.

  - ### classLoader

    protected final GroovyEngine.EngineClassLoader classLoader
    Class loader for dynamically loaded Groovy classes

- ## Constructor Details

  - ### GroovyEngine

    public GroovyEngine()
    Constructs a new GroovyEngine with default configuration.

- ## Method Details

  - ### getTypes

    public Map<String,String> getTypes()
    Returns all defined types mapped to their source definitions.
    Returns:
      map of type names to their source code

  - ### getVariables

    public Map<String,String> getVariables()
    Returns all defined variables mapped to their source definitions.
    Returns:
      map of variable names to their source code

  - ### getMethods

    public Map<String,String> getMethods()
    Returns all defined methods mapped to their source definitions.
    Returns:
      map of method signatures to their source code

  - ### getMethodNames

    public Set<String> getMethodNames()
    Returns the set of defined method names.
    Returns:
      unmodifiable set of method names

  - ### getImports

    public Map<String,String> getImports()
    Returns all import statements mapped to their source definitions.
    Returns:
      map of import identifiers to their source code

  - ### getScriptCompleter

    public org.jline.reader.Completer getScriptCompleter()
    Returns the completer for Groovy script completions.
    Specified by:
      `getScriptCompleter` in interface `org.jline.console.ScriptEngine`
    Returns:
      a completer instance

  - ### hasVariable

    public boolean hasVariable(String name)
    Checks if a variable with the given name exists in the binding.
    Specified by:
      `hasVariable` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `name` - the variable name to check
    Returns:
      `true` if the variable exists, `false` otherwise

  - ### put

    public void put(String name, Object value)
    Stores a variable in the binding.
    Specified by:
      `put` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `name` - the variable name
      `value` - the value to store

  - ### get

    public Object get(String name)
    Retrieves a variable from the binding.
    Specified by:
      `get` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `name` - the variable name
    Returns:
      the variable value, or `null` if not found

  - ### find

    public Map<String,Object> find(String name)
    Finds variables matching the given pattern.
    Specified by:
      `find` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `name` - the variable name pattern, or `null` to return all variables
    Returns:
      a map of matching variable names to their values

  - ### getSerializationFormats

    public List<String> getSerializationFormats()
    Returns the list of supported serialization formats.
    Specified by:
      `getSerializationFormats` in interface `org.jline.console.ScriptEngine`
    Returns:
      list of format names

  - ### getDeserializationFormats

    public List<String> getDeserializationFormats()
    Returns the list of supported deserialization formats.
    Specified by:
      `getDeserializationFormats` in interface `org.jline.console.ScriptEngine`
    Returns:
      list of format names

  - ### deserialize

    public Object deserialize(String value, String formatStr)
    Deserializes a string value according to the specified format.
    Specified by:
      `deserialize` in interface `org.jline.console.ScriptEngine`
    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

  - ### persist

    public void persist(Path file, Object object)
    Persists an object to a file using the default serialization format.
    Specified by:
      `persist` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `file` - the target file path
      `object` - the object to persist

  - ### persist

    public void persist(Path file, Object object, String format)
    Persists an object to a file using the specified serialization format.
    Specified by:
      `persist` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `file` - the target file path
      `object` - the object to persist
      `format` - the format name to use

  - ### execute

    public Object execute(File script, Object[] args) throws Exception
    Executes a Groovy script from a file.
    Specified by:
      `execute` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `script` - the script file to execute
      `args` - arguments to pass to the script
    Returns:
      the result of script execution
    Throws:
      `Exception` - if script execution fails

  - ### getBuffer

    public String getBuffer()
    Returns the current executable buffer reconstructed from the recorded snippets.
    Returns:
      buffer content for the current session

  - ### execute

    public Object execute(String statement) throws Exception
    Executes a Groovy statement.
    Specified by:
      `execute` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `statement` - the Groovy statement to execute
    Returns:
      the result of statement execution
    Throws:
      `Exception` - if statement execution fails

  - ### execute

    public Object execute(Object closure, Object... args)
    Executes a Groovy closure with the given arguments.
    Specified by:
      `execute` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `closure` - the closure to execute
      `args` - arguments to pass to the closure
    Returns:
      the result of closure execution

  - ### getEngineName

    public String getEngineName()
    Returns the name of this script engine.
    Specified by:
      `getEngineName` in interface `org.jline.console.ScriptEngine`
    Returns:
      the engine name

  - ### getExtensions

    public List<String> getExtensions()
    Returns the file extensions supported by this engine.
    Specified by:
      `getExtensions` in interface `org.jline.console.ScriptEngine`
    Returns:
      list of supported file extensions

  - ### reset

    public void reset()
    Resets the engine state, clearing all variables, methods, types, and imports.

  - ### removeMethod

    public void removeMethod(String name)
    Removes a method definition by name or signature.
    Parameters:
      `name` - method name or full signature to remove

  - ### removeImport

    public void removeImport(String name)
    Removes an import statement by name.
    Parameters:
      `name` - the import identifier to remove

  - ### removeType

    public void removeType(String name)
    Removes a type definition by name.
    Parameters:
      `name` - the type name to remove

  - ### removeVariable

    public void removeVariable(String name)
    Removes a variable definition by name.
    Parameters:
      `name` - the variable name to remove

  - ### del

    public void del(String... vars)
    Deletes multiple variables from the binding.
    Specified by:
      `del` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `vars` - variable names to delete

  - ### toJson

    public String toJson(Object obj)
    Converts an object to JSON format.
    Specified by:
      `toJson` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `obj` - the object to convert
    Returns:
      JSON representation

  - ### toString

    public String toString(Object obj)
    Converts an object to Groovy string format.
    Specified by:
      `toString` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `obj` - the object to convert
    Returns:
      Groovy string representation

  - ### toMap

    public Map<String,Object> toMap(Object obj)
    Converts an object to a map representation.
    Specified by:
      `toMap` in interface `org.jline.console.ScriptEngine`
    Parameters:
      `obj` - the object to convert
    Returns:
      map representation

  - ### setObjectCloner

    public void setObjectCloner(GroovyEngine.Cloner objectCloner)
    Sets the object cloner to use for copying session values.
    Parameters:
      `objectCloner` - the cloner implementation

  - ### getObjectCloner

    public GroovyEngine.Cloner getObjectCloner()
    Returns the current object cloner.
    Returns:
      the cloner implementation

  - ### scriptDescription

    public org.jline.console.CmdDesc scriptDescription(org.jline.console.CmdLine line)
    Generates a description for the given command line.
    Parameters:
      `line` - the command line to describe
    Returns:
      the command description

  - ### groovyOptions

    protected Map<String,Object> groovyOptions()
    Returns the current Groovy options map.
    Returns:
      map of option keys to values

  - ### groovyOption

    protected <T> T groovyOption(String option, T defval)
    Returns the value of a specific Groovy option.
    Type Parameters:
      `T` - the type of the option value
    Parameters:
      `option` - the option key
      `defval` - the default value if option not found
    Returns:
      the option value or default

  - ### groovyOption

    protected static <T> T groovyOption(Map<String,Object> options, String option, T defval)
    Returns the value of a specific option from the given options map.
    Type Parameters:
      `T` - the type of the option value
    Parameters:
      `options` - the options map
      `option` - the option key
      `defval` - the default value if option not found
    Returns:
      the option value or default

  - ### refresh

    public boolean refresh()
    Refreshes the syntax highlighter, forcing a reload on next use.
    Returns:
      true if refresh was successful

  - ### getSyntaxHighlighter

    protected org.jline.builtins.SyntaxHighlighter getSyntaxHighlighter()
    Returns the syntax highlighter for Groovy code.
    Returns:
      the syntax highlighter instance

  - ### purgeClassCache

    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