Package org.apache.groovy.groovysh.jline
Class GroovyEngine
java.lang.Object
org.apache.groovy.groovysh.jline.GroovyEngine
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionprotected static classEncapsulates access rules for code completion.static interfaceStrategy used to clone session values for completion and inspection workflows.static classCustom class loader for dynamically loaded Groovy classes.static enumSerialization/deserialization format options. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringOption key to enable completion for all classes including non-publicstatic final StringOption key to enable completion for all constructors including non-publicstatic final StringOption key to enable completion for all fields including non-publicstatic final StringOption key to enable completion for all methods including non-publicstatic final StringOption key for using canonical class names in descriptionsprotected final GroovyEngine.EngineClassLoaderClass loader for dynamically loaded Groovy classesstatic final StringEnvironment variable for Groovy color settingsstatic final StringOption key to enable identifier completionstatic final StringOption key to enable meta-method completionstatic final StringOption key for nanorc syntax file locationstatic final StringOption key for nanorc value configurationstatic final StringOption key to disable syntax checkingstatic final StringOption key for restricted completion modeprotected BindingShared binding that stores variables visible to interactive scripts.static final StringOption key to enable synthetic method completion -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidDeletes multiple variables from the binding.deserialize(String value, String formatStr) Deserializes a string value according to the specified format.Executes a Groovy script from a file.Executes a Groovy closure with the given arguments.Executes a Groovy statement.Finds variables matching the given pattern.Retrieves a variable from the binding.Returns the current executable buffer reconstructed from the recorded snippets.Returns the list of supported deserialization formats.Returns the name of this script engine.Returns the file extensions supported by this engine.Returns all import statements mapped to their source definitions.Returns the set of defined method names.Returns all defined methods mapped to their source definitions.Returns the current object cloner.org.jline.reader.CompleterReturns the completer for Groovy script completions.Returns the list of supported serialization formats.protected org.jline.builtins.SyntaxHighlighterReturns the syntax highlighter for Groovy code.getTypes()Returns all defined types mapped to their source definitions.Returns all defined variables mapped to their source definitions.protected <T> TgroovyOption(String option, T defval) Returns the value of a specific Groovy option.protected static <T> TgroovyOption(Map<String, Object> options, String option, T defval) Returns the value of a specific option from the given options map.Returns the current Groovy options map.booleanhasVariable(String name) Checks if a variable with the given name exists in the binding.voidPersists an object to a file using the default serialization format.voidPersists an object to a file using the specified serialization format.voidpurgeClassCache(String regex) Purges classes from the class cache matching the given regex.voidStores a variable in the binding.booleanrefresh()Refreshes the syntax highlighter, forcing a reload on next use.voidremoveImport(String name) Removes an import statement by name.voidremoveMethod(String name) Removes a method definition by name or signature.voidremoveType(String name) Removes a type definition by name.voidremoveVariable(String name) Removes a variable definition by name.voidreset()Resets the engine state, clearing all variables, methods, types, and imports.org.jline.console.CmdDescscriptDescription(org.jline.console.CmdLine line) Generates a description for the given command line.voidsetObjectCloner(GroovyEngine.Cloner objectCloner) Sets the object cloner to use for copying session values.Converts an object to JSON format.Converts an object to a map representation.Converts an object to Groovy string format.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jline.console.ScriptEngine
deserialize, execute, execute, execute, find
-
Field Details
-
CANONICAL_NAMES
Option key for using canonical class names in descriptions- See Also:
-
NANORC_SYNTAX
Option key for nanorc syntax file location- See Also:
-
NANORC_VALUE
Option key for nanorc value configuration- See Also:
-
GROOVY_COLORS
Environment variable for Groovy color settings- See Also:
-
NO_SYNTAX_CHECK
Option key to disable syntax checking- See Also:
-
RESTRICTED_COMPLETION
Option key for restricted completion mode- See Also:
-
ALL_FIELDS_COMPLETION
Option key to enable completion for all fields including non-public- See Also:
-
ALL_METHODS_COMPLETION
Option key to enable completion for all methods including non-public- See Also:
-
ALL_CONSTRUCTORS_COMPLETION
Option key to enable completion for all constructors including non-public- See Also:
-
ALL_CLASSES_COMPLETION
Option key to enable completion for all classes including non-public- See Also:
-
IDENTIFIERS_COMPLETION
Option key to enable identifier completion- See Also:
-
META_METHODS_COMPLETION
Option key to enable meta-method completion- See Also:
-
SYNTHETIC_METHODS_COMPLETION
Option key to enable synthetic method completion- See Also:
-
classLoader
Class loader for dynamically loaded Groovy classes
-
-
Constructor Details
-
GroovyEngine
public GroovyEngine()Constructs a new GroovyEngine with default configuration.
-
-
Method Details
-
getTypes
Returns all defined types mapped to their source definitions.- Returns:
- map of type names to their source code
-
getVariables
Returns all defined variables mapped to their source definitions.- Returns:
- map of variable names to their source code
-
getMethods
Returns all defined methods mapped to their source definitions.- Returns:
- map of method signatures to their source code
-
getMethodNames
Returns the set of defined method names.- Returns:
- unmodifiable set of method names
-
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:
getScriptCompleterin interfaceorg.jline.console.ScriptEngine- Returns:
- a completer instance
-
hasVariable
Checks if a variable with the given name exists in the binding.- Specified by:
hasVariablein interfaceorg.jline.console.ScriptEngine- Parameters:
name- the variable name to check- Returns:
trueif the variable exists,falseotherwise
-
put
Stores a variable in the binding.- Specified by:
putin interfaceorg.jline.console.ScriptEngine- Parameters:
name- the variable namevalue- the value to store
-
get
Retrieves a variable from the binding.- Specified by:
getin interfaceorg.jline.console.ScriptEngine- Parameters:
name- the variable name- Returns:
- the variable value, or
nullif not found
-
find
Finds variables matching the given pattern.- Specified by:
findin interfaceorg.jline.console.ScriptEngine- Parameters:
name- the variable name pattern, ornullto return all variables- Returns:
- a map of matching variable names to their values
-
getSerializationFormats
Returns the list of supported serialization formats.- Specified by:
getSerializationFormatsin interfaceorg.jline.console.ScriptEngine- Returns:
- list of format names
-
getDeserializationFormats
Returns the list of supported deserialization formats.- Specified by:
getDeserializationFormatsin interfaceorg.jline.console.ScriptEngine- Returns:
- list of format names
-
deserialize
Deserializes a string value according to the specified format.- Specified by:
deserializein interfaceorg.jline.console.ScriptEngine- Parameters:
value- the string to deserializeformatStr- the format name (JSON, GROOVY, NONE, or AUTO)- Returns:
- the deserialized object
-
persist
Persists an object to a file using the default serialization format.- Specified by:
persistin interfaceorg.jline.console.ScriptEngine- Parameters:
file- the target file pathobject- the object to persist
-
persist
Persists an object to a file using the specified serialization format.- Specified by:
persistin interfaceorg.jline.console.ScriptEngine- Parameters:
file- the target file pathobject- the object to persistformat- the format name to use
-
execute
Executes a Groovy script from a file.- Specified by:
executein interfaceorg.jline.console.ScriptEngine- Parameters:
script- the script file to executeargs- arguments to pass to the script- Returns:
- the result of script execution
- Throws:
Exception- if script execution fails
-
getBuffer
Returns the current executable buffer reconstructed from the recorded snippets.- Returns:
- buffer content for the current session
-
execute
Executes a Groovy statement.- Specified by:
executein interfaceorg.jline.console.ScriptEngine- Parameters:
statement- the Groovy statement to execute- Returns:
- the result of statement execution
- Throws:
Exception- if statement execution fails
-
execute
Executes a Groovy closure with the given arguments.- Specified by:
executein interfaceorg.jline.console.ScriptEngine- Parameters:
closure- the closure to executeargs- arguments to pass to the closure- Returns:
- the result of closure execution
-
getEngineName
Returns the name of this script engine.- Specified by:
getEngineNamein interfaceorg.jline.console.ScriptEngine- Returns:
- the engine name
-
getExtensions
Returns the file extensions supported by this engine.- Specified by:
getExtensionsin interfaceorg.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
Removes a method definition by name or signature.- Parameters:
name- method name or full signature to remove
-
removeImport
Removes an import statement by name.- Parameters:
name- the import identifier to remove
-
removeType
Removes a type definition by name.- Parameters:
name- the type name to remove
-
removeVariable
Removes a variable definition by name.- Parameters:
name- the variable name to remove
-
del
Deletes multiple variables from the binding.- Specified by:
delin interfaceorg.jline.console.ScriptEngine- Parameters:
vars- variable names to delete
-
toJson
Converts an object to JSON format.- Specified by:
toJsonin interfaceorg.jline.console.ScriptEngine- Parameters:
obj- the object to convert- Returns:
- JSON representation
-
toString
Converts an object to Groovy string format.- Specified by:
toStringin interfaceorg.jline.console.ScriptEngine- Parameters:
obj- the object to convert- Returns:
- Groovy string representation
-
toMap
Converts an object to a map representation.- Specified by:
toMapin interfaceorg.jline.console.ScriptEngine- Parameters:
obj- the object to convert- Returns:
- map representation
-
setObjectCloner
Sets the object cloner to use for copying session values.- Parameters:
objectCloner- the cloner implementation
-
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
Returns the current Groovy options map.- Returns:
- map of option keys to values
-
groovyOption
Returns the value of a specific Groovy option.- Type Parameters:
T- the type of the option value- Parameters:
option- the option keydefval- the default value if option not found- Returns:
- the option value or default
-
groovyOption
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 mapoption- the option keydefval- 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
Purges classes from the class cache matching the given regex.- Parameters:
regex- the pattern to match class names, or null to clear all
-