CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-groovy--groovy

Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax.

Pending
Overview
Eval results
Files

script-execution.mddocs/

Script Execution

High-level APIs for evaluating Groovy code, managing variable bindings, and creating reusable scripts with dynamic compilation capabilities.

Capabilities

GroovyShell

Shell for evaluating Groovy expressions and scripts with support for variable bindings and class loading.

/**
 * Shell for evaluating Groovy expressions and scripts
 */
class GroovyShell {
    /** Create a new shell with default configuration */
    GroovyShell()
    
    /** Create a new shell with the given binding */
    GroovyShell(Binding binding)
    
    /** Create a new shell with custom ClassLoader */
    GroovyShell(ClassLoader parent)
    
    /** Create a new shell with ClassLoader and binding */
    GroovyShell(ClassLoader parent, Binding binding)
    
    /** Evaluate a Groovy script from string */
    Object evaluate(String scriptText)
    
    /** Evaluate a Groovy script from file */
    Object evaluate(File file)
    
    /** Evaluate a Groovy script from Reader */
    Object evaluate(Reader reader)
    
    /** Parse script text and return reusable Script object */
    Script parse(String scriptText)
    
    /** Parse script from file and return reusable Script object */
    Script parse(File file)
    
    /** Parse script text and return the generated Class */
    Class parseClass(String text)
    
    /** Parse script from file and return the generated Class */
    Class parseClass(File file)
}

Usage Examples:

// Basic script evaluation
def shell = new GroovyShell()
def result = shell.evaluate("2 + 2")
println result // 4

// Evaluation with variables
def binding = new Binding()
binding.setVariable("x", 10)
binding.setVariable("y", 5)
shell = new GroovyShell(binding)
result = shell.evaluate("x * y")
println result // 50

// Reusable scripts
def script = shell.parse('''
    def calculate(a, b) {
        return a * b + 10
    }
    calculate(x, y)
''')
result = script.run()
println result // 60

// File evaluation
def scriptFile = new File("myscript.groovy")
result = shell.evaluate(scriptFile)

Variable Binding

Context for managing script variables and providing data exchange between Java and Groovy code.

/**
 * Variable binding context for scripts
 */
class Binding extends GroovyObjectSupport {
    /** Create empty binding */
    Binding()
    
    /** Create binding with initial variable map */
    Binding(Map variables)
    
    /** Get a variable value by name */
    Object getVariable(String name)
    
    /** Set a variable value by name */
    void setVariable(String name, Object value)
    
    /** Get all variables as a Map */
    Map getVariables()
    
    /** Set all variables from a Map */
    void setVariables(Map variables)
    
    /** Check if a variable exists */
    boolean hasVariable(String name)
    
    /** Remove a variable */
    Object removeVariable(String name)
}

Usage Examples:

// Creating and using bindings
def binding = new Binding()
binding.setVariable("name", "Alice")
binding.setVariable("age", 30)
binding.setVariable("active", true)

def shell = new GroovyShell(binding)
def result = shell.evaluate('''
    if (active) {
        return "$name is $age years old and active"
    } else {
        return "$name is inactive"
    }
''')
println result // "Alice is 30 years old and active"

// Sharing data between Java and Groovy
def binding = new Binding()
binding.setVariable("javaList", Arrays.asList(1, 2, 3, 4, 5))

def shell = new GroovyShell(binding)
shell.evaluate('''
    // Groovy can manipulate Java objects
    def sum = javaList.sum()
    result = "Sum is: $sum"
''')

println binding.getVariable("result") // "Sum is: 15"

GroovyClassLoader

ClassLoader that can dynamically compile and load Groovy classes at runtime.

/**
 * ClassLoader that can load Groovy classes at runtime
 */
class GroovyClassLoader extends URLClassLoader {
    /** Create with default parent ClassLoader */
    GroovyClassLoader()
    
    /** Create with specified parent ClassLoader */
    GroovyClassLoader(ClassLoader parent)
    
    /** Parse and compile Groovy source text into a Class */
    Class parseClass(String text)
    
    /** Parse and compile Groovy source from file into a Class */
    Class parseClass(File file)
    
    /** Parse source with a specific file name (for error reporting) */
    Class parseClass(String text, String fileName)
    
    /** Load a class by name */
    Class loadClass(String name)
    
    /** Define a class from bytecode */
    Class defineClass(String name, byte[] bytecode)
}

Usage Examples:

// Dynamic class creation
def classLoader = new GroovyClassLoader()
def classText = '''
    class DynamicClass {
        String name
        int value
        
        def greet() {
            return "Hello from $name with value $value"
        }
    }
'''

def dynamicClass = classLoader.parseClass(classText)
def instance = dynamicClass.newInstance()
instance.name = "Dynamic"
instance.value = 42
println instance.greet() // "Hello from Dynamic with value 42"

// Loading classes from files
def scriptFile = new File("MyGroovyClass.groovy")
def myClass = classLoader.parseClass(scriptFile)
def obj = myClass.newInstance()

GroovyScriptEngine

Script engine for running Groovy scripts with dependency management and automatic recompilation.

/**
 * Script engine for running Groovy scripts with dependency management
 */
class GroovyScriptEngine {
    /** Create engine with script root directories */
    GroovyScriptEngine(String[] roots)
    
    /** Create engine with URL roots */
    GroovyScriptEngine(URL[] roots)
    
    /** Run a script by name with the given binding */
    Object run(String scriptName, Binding binding)
    
    /** Get the GroovyCodeSource for a script */
    GroovyCodeSource getGroovyCodeSource(String scriptName)
    
    /** Get the ClassLoader used by this engine */
    GroovyClassLoader getGroovyClassLoader()
}

Usage Examples:

// Create script engine with script directories
def engine = new GroovyScriptEngine(["scripts", "lib"] as String[])

// Run scripts with bindings
def binding = new Binding()
binding.setVariable("input", "test data")

def result = engine.run("process.groovy", binding)
println "Script result: $result"

// Scripts can reference other scripts in the engine's path
// process.groovy might contain:
// import utils.Helper
// return Helper.processData(input)

Evaluation Utilities

Quick evaluation utilities for simple expressions.

/**
 * Utility for evaluating Groovy expressions quickly
 */
class Eval {
    /** Evaluate expression with self reference */
    static Object me(Object self, String expression)
    
    /** Evaluate expression with single variable x */
    static Object x(Object x, String expression)
    
    /** Evaluate expression with variables x and y */
    static Object xy(Object x, Object y, String expression)
    
    /** Evaluate expression with variables x, y, and z */
    static Object xyz(Object x, Object y, Object z, String expression)
}

Usage Examples:

// Quick expression evaluation
def result = Eval.me("'Hello World'.toUpperCase()")
println result // "HELLO WORLD"

// With variables
def nums = [1, 2, 3, 4, 5]
result = Eval.x(nums, "x.findAll { it % 2 == 0 }")
println result // [2, 4]

// Multiple variables
result = Eval.xy(10, 20, "x + y")
println result // 30

result = Eval.xyz(5, 10, 2, "x * y / z")
println result // 25

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-groovy--groovy

docs

cli-interface.md

collections-utilities.md

core-runtime.md

index.md

json-processing.md

meta-programming.md

script-execution.md

sql-database.md

template-processing.md

xml-processing.md

tile.json