Class GroovyEngine
- All Implemented Interfaces:
org.jline.console.ScriptEngine
-
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
ConstructorsConstructorDescriptionConstructs a new GroovyEngine with default configuration.GroovyEngine(ClassLoader parent, Binding binding, CompilerConfiguration configuration) Constructs a GroovyEngine with an explicit parent class loader, binding, and compiler configuration.GroovyEngine(CompilerConfiguration configuration) Constructs a GroovyEngine that evaluates with the given compiler configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected GroovyShellcreateShell(ClassLoader classLoader, Binding binding, CompilerConfiguration configuration) Creates theGroovyShellused to evaluate statements.voidDeletes 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 class loader used to parse and load scripts for this engine.Returns the compiler configuration this engine was constructed with.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. -
GroovyEngine
Constructs a GroovyEngine that evaluates with the given compiler configuration.Embedders that previously installed an
ImportCustomizerorASTTransformationCustomizer(for exampleThreadInterrupt) on groovysh'sCompilerConfigurationshould pass that configuration here.CompilerConfiguration.DEFAULTis immutable; start fromnew CompilerConfiguration()when adding customizers.- Parameters:
configuration- compiler configuration, ornullforCompilerConfiguration.DEFAULT- Since:
- 7.0.0
-
GroovyEngine
@Incubating public GroovyEngine(ClassLoader parent, Binding binding, CompilerConfiguration configuration) Constructs a GroovyEngine with an explicit parent class loader, binding, and compiler configuration.As with
GroovyShell, anGroovyEngine.EngineClassLoaderwhose configuration is the same instance asconfigurationbecomes the engine class loader itself rather than its parent; any other loader is wrapped. Two engines sharing one loader that way also share its class cache, so a cache purge from either —/resetand the type-redefinition paths do this — discards the other's classes, and their script classes occupy one namespace. Pass a plainClassLoader, or a configuration this loader does not already hold, to get a child loader instead.- Parameters:
parent- class loader to use as parent, or to adopt as described above;nulluses the thread context class loaderbinding- shared binding, ornullto create a new oneconfiguration- compiler configuration, ornullforCompilerConfiguration.DEFAULT- Since:
- 7.0.0
-
-
Method Details
-
createShell
@Incubating protected GroovyShell createShell(ClassLoader classLoader, Binding binding, CompilerConfiguration configuration) Creates theGroovyShellused to evaluate statements.Called from the constructor, so overrides must not rely on subclass fields that are assigned after
super(...)returns. Primitive fields and static state are safe; instance initializers of the subclass have not run yet. The returned shell must useclassLoaderas its loader (the default implementation does this viaGroovyClassLoader.hasCompatibleConfiguration(CompilerConfiguration)), otherwise engine internals that cast toGroovyEngine.EngineClassLoaderwill fail.- Parameters:
classLoader- the engine class loaderbinding- the shared bindingconfiguration- the compiler configuration, nevernull- Returns:
- a shell that uses
classLoaderas its loader - Since:
- 7.0.0
-
getClassLoader
Returns the class loader used to parse and load scripts for this engine.- Returns:
- the engine class loader
- Since:
- 7.0.0
-
getCompilerConfiguration
Returns the compiler configuration this engine was constructed with.- Returns:
- the compiler configuration, never
null - Since:
- 7.0.0
-
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).AUTOparses only: it recognises JSON and otherwise returns the text unchanged, and never evaluates the value as Groovy. UseGROOVYto evaluate.- 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
-