Interactive command-line shell (REPL) for Apache Groovy with scripting engine and rich command system
—
Core engine for executing Groovy code programmatically, managing variables and imports, providing code completion, and handling state persistence. The GroovyEngine serves as the primary programmatic interface for embedding Groovy REPL functionality in applications.
Execute Groovy statements, scripts, and closures with full error handling and result capture.
/**
* Execute a Groovy code statement
* @param statement Groovy code as string
* @return Execution result object
*/
Object execute(String statement)
/**
* Execute a Groovy script file with arguments
* @param script File containing Groovy script
* @param args Arguments to pass to script
* @return Execution result object
*/
Object execute(File script, Object[] args)
/**
* Execute a Groovy closure with arguments
* @param closure Groovy closure object
* @param args Arguments to pass to closure
* @return Closure execution result
*/
Object execute(Object closure, Object... args)Usage Examples:
import org.apache.groovy.groovysh.jline.GroovyEngine
GroovyEngine engine = new GroovyEngine()
// Execute simple expressions
Object result = engine.execute("2 + 2")
println result // 4
// Execute complex code
engine.execute("""
def factorial(n) {
n <= 1 ? 1 : n * factorial(n - 1)
}
factorial(5)
""")
// Execute script file
File script = new File("calculator.groovy")
Object result = engine.execute(script, [] as Object[])Manage variables in the engine's execution context with full lifecycle control.
/**
* Set a variable in the engine scope
* @param name Variable name
* @param value Variable value
*/
void put(String name, Object value)
/**
* Get variable value from engine scope
* @param name Variable name
* @return Variable value or null if not found
*/
Object get(String name)
/**
* Check if variable exists in engine scope
* @param name Variable name
* @return true if variable exists, false otherwise
*/
boolean hasVariable(String name)
/**
* Find variables matching a pattern
* @param name Pattern to match variable names (null returns all variables)
* @return Map of variable names to values
*/
Map<String, Object> find(String name)
/**
* Delete specified variables from engine scope
* @param vars Variable names to delete
*/
void del(String... vars)Usage Examples:
GroovyEngine engine = new GroovyEngine()
// Set variables
engine.put("username", "alice")
engine.put("age", 25)
engine.put("active", true)
// Use variables in code
Object greeting = engine.execute("'Hello, ' + username")
// Check and retrieve variables
if (engine.hasVariable("age")) {
Object userAge = engine.get("age")
println "User age: $userAge"
}
// Find variables by pattern
Map<String, Object> userVars = engine.find("user*")
// Delete variables
engine.del("username", "age")Control the engine's execution state including buffer, imports, methods, and types.
/**
* Clear all state (imports, variables, methods, types)
*/
void reset()
/**
* Get current script buffer content
* @return Current buffer as string
*/
String getBuffer()
/**
* Get all imports in the engine
* @return Map of import names to definitions
*/
Map<String, String> getImports()
/**
* Get all variables in the engine
* @return Map of variable names to definitions
*/
Map<String, String> getVariables()
/**
* Get all method definitions in the engine
* @return Map of method signatures to definitions
*/
Map<String, String> getMethods()
/**
* Get all type definitions in the engine
* @return Map of type names to definitions
*/
Map<String, String> getTypes()
/**
* Remove specific import statement
* @param importStatement Import to remove
*/
void removeImport(String importStatement)
/**
* Remove specific variable
* @param variableName Variable to remove
*/
void removeVariable(String variableName)
/**
* Remove specific method
* @param methodName Method to remove
*/
void removeMethod(String methodName)
/**
* Remove specific type
* @param typeName Type to remove
*/
void removeType(String typeName)Usage Examples:
GroovyEngine engine = new GroovyEngine()
// Add some state
engine.execute("import java.util.Date")
engine.put("startTime", new Date())
engine.execute("def helper() { 'utility function' }")
// Inspect state
Map<String, String> imports = engine.getImports()
Map<String, String> variables = engine.getVariables()
Map<String, String> methods = engine.getMethods()
// Selective cleanup
engine.removeVariable("startTime")
engine.removeMethod("helper")
// Complete reset
engine.reset()Handle data conversion and persistence with support for multiple formats.
/**
* Deserialize data from string representation
* @param value Serialized data string
* @param format Deserialization format (JSON, GROOVY, NONE)
* @return Deserialized object
*/
Object deserialize(String value, String format)
/**
* Persist object to file
* @param file Target file path
* @param object Object to persist
*/
void persist(Path file, Object object)
/**
* Convert object to JSON string
* @param obj Object to serialize
* @return JSON representation
*/
String toJson(Object obj)
/**
* Convert object to string representation
* @param obj Object to convert
* @return String representation
*/
String toString(Object obj)
/**
* Convert object to map representation
* @param obj Object to convert
* @return Map representation
*/
Map toMap(Object obj)
/**
* Get supported serialization formats
* @return List of supported formats
*/
List<String> getSerializationFormats()
/**
* Get supported deserialization formats
* @return List of supported formats
*/
List<String> getDeserializationFormats()Usage Examples:
GroovyEngine engine = new GroovyEngine()
// Data conversion
def user = [name: "Alice", age: 25, active: true]
String json = engine.toJson(user)
Map userMap = engine.toMap(user)
// Deserialization
Object userData = engine.deserialize('{"name":"Bob","age":30}', "JSON")
// Persistence
Path dataFile = Paths.get("user-data.json")
engine.persist(dataFile, user)
// Check supported formats
List<String> serFormats = engine.getSerializationFormats()
List<String> deserFormats = engine.getDeserializationFormats()Advanced features for IDE-like functionality including auto-completion and code analysis.
/**
* Get script completer for auto-completion
* @return Completer instance for code completion
*/
Completer getScriptCompleter()
/**
* Get description for script or method
* @param line Command line to describe
* @return Description string
*/
String scriptDescription(CmdLine line)
/**
* Get engine name identifier
* @return Engine name ("GroovyEngine")
*/
String getEngineName()
/**
* Get supported file extensions
* @return List of extensions (["groovy"])
*/
List<String> getExtensions()
/**
* Get method names defined in the engine
* @return Set of method names
*/
Set<String> getMethodNames()
/**
* Purge class cache with optional regex filter
* @param regex Regular expression to match class names (null clears all)
*/
void purgeClassCache(String regex)
/**
* Refresh engine state and syntax highlighting
* @return true if refresh succeeded
*/
boolean refresh()
/**
* Set object cloner for variable copying
* @param cloner Cloner implementation
*/
void setObjectCloner(Cloner cloner)
/**
* Get current object cloner
* @return Current cloner implementation
*/
Cloner getObjectCloner()Configure engine behavior through various options affecting completion, syntax checking, and execution.
/**
* Get engine configuration options
* @return Map of configuration options
*/
Map<String, Object> groovyOptions()Available Options:
CANONICAL_NAMES: Use canonical class names in completionNO_SYNTAX_CHECK: Disable syntax validationRESTRICTED_COMPLETION: Limit completion suggestionsALL_METHODS_COMPLETION: Include all methods in completionALL_FIELDS_COMPLETION: Include all fields in completionALL_CONSTRUCTORS_COMPLETION: Include all constructors in completionIDENTIFIERS_COMPLETION: Include identifiers in completionMETA_METHODS_COMPLETION: Include meta-methods in completionSYNTHETIC_METHODS_COMPLETION: Include synthetic methods in completionUsage Examples:
GroovyEngine engine = new GroovyEngine()
// Configure options
Map<String, Object> options = engine.groovyOptions()
options.put("ALL_METHODS_COMPLETION", true)
options.put("RESTRICTED_COMPLETION", false)
// Use completion
Completer completer = engine.getScriptCompleter()
// Get engine info
String engineName = engine.getEngineName()
List<String> extensions = engine.getExtensions()
Set<String> methodNames = engine.getMethodNames()/**
* Supported data serialization formats
*/
enum Format {
JSON, // JSON format
GROOVY, // Groovy object format
NONE // No formatting
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-groovysh