CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-codehaus-groovy--groovy-all

Apache Groovy - A powerful multi-faceted programming language for the JVM platform with comprehensive module support

Pending
Overview
Eval results
Files

core-language.mddocs/

Core Language Features

This document covers the fundamental Groovy language constructs including script execution, dynamic objects, closures, metaprogramming, and the core runtime system.

Script Execution

GroovyShell

The main entry point for executing Groovy scripts dynamically at runtime.

class GroovyShell {
    // Constructors
    GroovyShell()
    GroovyShell(Binding binding)
    GroovyShell(ClassLoader parent)
    GroovyShell(ClassLoader parent, Binding binding)
    GroovyShell(CompilerConfiguration config)
    GroovyShell(ClassLoader parent, CompilerConfiguration config)
    GroovyShell(Binding binding, CompilerConfiguration config)
    GroovyShell(ClassLoader parent, Binding binding, CompilerConfiguration config)
    GroovyShell(GroovyShell shell)
    
    // Basic evaluate methods
    Object evaluate(String scriptText)
    Object evaluate(String scriptText, String fileName)
    Object evaluate(String scriptText, String fileName, String codeBase)
    Object evaluate(File scriptFile)
    Object evaluate(Reader in)
    Object evaluate(Reader in, String fileName)
    Object evaluate(URI uri)
    Object evaluate(GroovyCodeSource codeSource)
    
    // Basic parse methods
    Script parse(String scriptText)
    Script parse(String scriptText, String fileName)
    Script parse(File scriptFile)
    Script parse(Reader reader, String fileName)
    Script parse(Reader in)
    Script parse(URI uri)
    Script parse(GroovyCodeSource codeSource)
    
    // Run methods with arguments
    Object run(File scriptFile, List<String> list)
    Object run(File scriptFile, String[] args)
    Object run(String scriptText, String fileName, List<String> list)
    Object run(String scriptText, String fileName, String[] args)
    Object run(GroovyCodeSource source, List<String> args)
    Object run(GroovyCodeSource source, String[] args)
    Object run(URI source, List<String> args)
    Object run(URI source, String[] args)
    Object run(Reader in, String fileName, List<String> list)
    Object run(Reader in, String fileName, String[] args)
    
    // Variable management
    Object getVariable(String name)
    void setVariable(String name, Object value)
    void removeVariable(String name)
    
    // Properties and context
    Object getProperty(String property)
    void setProperty(String property, Object newValue)
    Binding getContext()
    GroovyClassLoader getClassLoader()
    
    // Utility methods
    void resetLoadedClasses()
    static void main(String[] args)
}

Usage example:

def shell = new GroovyShell()
def result = shell.evaluate('Math.max(5, 10)')
assert result == 10

def binding = new Binding([x: 10, y: 20])
shell = new GroovyShell(binding)
result = shell.evaluate('x + y')  
assert result == 30

GroovyScriptEngine

Script engine with caching and dependency management for improved performance.

class GroovyScriptEngine {
    GroovyScriptEngine(String[] roots)
    GroovyScriptEngine(String[] roots, ClassLoader parent)
    GroovyScriptEngine(ResourceConnector rc)
    GroovyScriptEngine(ResourceConnector rc, ClassLoader parent)
    
    Object run(String scriptName, Binding binding)
    Class loadScriptByName(String scriptName)
    GroovyClassLoader getGroovyClassLoader()
    CompilerConfiguration getConfig()
}

GroovyClassLoader

Dynamic class loader for compiling and loading Groovy classes at runtime.

class GroovyClassLoader extends ClassLoader {
    GroovyClassLoader()
    GroovyClassLoader(ClassLoader parent)
    GroovyClassLoader(ClassLoader parent, CompilerConfiguration config)
    
    Class parseClass(String text)
    Class parseClass(String text, String fileName)
    Class parseClass(File file)
    Class parseClass(InputStream in, String fileName)
    Class parseClass(URL url)
    
    void addClasspath(String path)
    void clearCache()
    CompilerConfiguration getConfig()
}

Variable Binding

Binding

Container for script variables and their values.

class Binding {
    Binding()
    Binding(Map variables)
    Binding(String[] args)
    
    Object getVariable(String name)
    void setVariable(String name, Object value)
    boolean hasVariable(String name)
    void removeVariable(String name)
    Map getVariables()
    void setProperty(String property, Object newValue)
    Object getProperty(String property)
}

Usage example:

def binding = new Binding()
binding.setVariable('greeting', 'Hello')
binding.setVariable('name', 'World')

def shell = new GroovyShell(binding)
def result = shell.evaluate('"$greeting, $name!"')
assert result == 'Hello, World!'

Closures

Closure

First-class functions with lexical scoping and powerful features.

abstract class Closure<V> implements Cloneable, Runnable, GroovyCallable<V> {
    static final int DELEGATE_FIRST = 1
    static final int DELEGATE_ONLY = 2  
    static final int OWNER_FIRST = 3
    static final int OWNER_ONLY = 4
    static final int TO_SELF = 5
    
    Object call()
    Object call(Object... args)
    Object call(Object arguments)
    
    Closure<V> curry(Object... args)
    Closure<V> rcurry(Object... args)
    Closure<V> ncurry(int n, Object... args)
    
    Closure<V> memoize()
    Closure<V> memoizeAtMost(int maxCacheSize)
    Closure<V> memoizeAtLeast(int minCacheSize)
    Closure<V> memoizeBetween(int minCacheSize, int maxCacheSize)
    
    Object getDelegate()
    void setDelegate(Object delegate)
    Object getOwner()
    Object getThisObject()
    int getResolveStrategy()
    void setResolveStrategy(int resolveStrategy)
    
    int getMaximumNumberOfParameters()
    Class[] getParameterTypes()
    
    Closure<V> clone()
    Closure<V> asWritable()
}

Usage examples:

// Basic closure
def greet = { name -> "Hello, $name!" }
assert greet('World') == 'Hello, World!'

// Curry parameters
def multiply = { x, y -> x * y }
def double = multiply.curry(2)
assert double(5) == 10

// Delegate usage
def obj = [name: 'Groovy']
def closure = { "Hello, $name!" }
closure.delegate = obj
closure.resolveStrategy = Closure.DELEGATE_FIRST
assert closure() == 'Hello, Groovy!'

Dynamic Objects

Expando

Dynamic object that allows runtime addition of properties and methods.

class Expando implements GroovyObject {
    Expando()
    Expando(Map<String, Object> properties)
    
    Object invokeMethod(String name, Object args)
    Object getProperty(String property)
    void setProperty(String property, Object newValue)
    MetaClass getMetaClass()
    void setMetaClass(MetaClass metaClass)
    
    String toString()
}

Usage example:

def person = new Expando()
person.name = 'John'
person.age = 30
person.greet = { "Hello, I'm $name and I'm $age years old" }
person.birthday = { age++ }

assert person.greet() == "Hello, I'm John and I'm 30 years old"  
person.birthday()
assert person.age == 31

Metaprogramming

MetaClass System

The metaclass system enables runtime modification of class behavior.

interface MetaClass {
    Object invokeMethod(Object object, String methodName, Object[] arguments)
    Object invokeMethod(Object object, String methodName, Object arguments)
    Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
    Object invokeConstructor(Object[] arguments)
    
    Object getProperty(Object object, String property)
    void setProperty(Object object, String property, Object newValue)
    
    List<MetaMethod> getMethods()
    List<MetaMethod> getMetaMethods()
    MetaMethod getMetaMethod(String name, Class[] argTypes)
    MetaMethod getStaticMetaMethod(String name, Class[] argTypes)
    
    List<MetaProperty> getProperties()
    MetaProperty getMetaProperty(String name)
    boolean hasProperty(Object obj, String name)
    
    void addMetaMethod(MetaMethod metaMethod)
    void addMetaProperty(MetaProperty metaProperty)
}

ExpandoMetaClass

Runtime metaclass modification capabilities.

class ExpandoMetaClass extends MetaClassImpl {
    ExpandoMetaClass(Class theClass)
    ExpandoMetaClass(Class theClass, boolean register)
    ExpandoMetaClass(Class theClass, boolean register, boolean allowChangesAfterInit)
    
    void registerInstanceMethod(String name, Closure closure)
    void registerStaticMethod(String name, Closure closure)
    void registerConstructor(Closure closure)
    
    Object getProperty(String property)
    void setProperty(String property, Object newValue)
    
    void initialize()
    boolean isModified()
    void enableGlobally()
    static void enableGlobally()
}

Usage example:

// Add method to existing class
String.metaClass.isPalindrome = {
    delegate == delegate.reverse()
}
assert 'racecar'.isPalindrome() == true
assert 'hello'.isPalindrome() == false

// Add static method
Integer.metaClass.static.between = { int a, int b ->
    (a..b).toList()
}
assert Integer.between(1, 5) == [1, 2, 3, 4, 5]

String Interpolation

GString

Groovy's template string implementation with embedded expressions.

abstract class GString implements Comparable, CharSequence, Writable, Buildable {
    abstract Object[] getValues()
    abstract String[] getStrings()
    
    String toString()
    Writer writeTo(Writer out)
    int length()
    char charAt(int index)
    CharSequence subSequence(int start, int end)
    
    int compareTo(Object that)
    boolean equals(Object that)
    int hashCode()
}

Usage example:

def name = 'World'
def number = 42
def gstring = "Hello, $name! The answer is ${number * 2}."
assert gstring.toString() == 'Hello, World! The answer is 84.'
assert gstring.values == ['World', 84]
assert gstring.strings == ['Hello, ', '! The answer is ', '.']

Ranges

Range Implementations

Groovy provides several range implementations for different data types.

class IntRange implements Range<Integer> {
    IntRange(int from, int to)
    IntRange(boolean inclusive, int from, int to)
    
    boolean contains(Object value)
    boolean containsWithinBounds(Object value)
    Integer get(int index)
    int size()
    List<Integer> step(int stepSize)
    List<Integer> step(int stepSize, Closure closure)
    String inspect()
    boolean isReverse()
    IntRange reverse()
}

class ObjectRange implements Range<Comparable> {
    ObjectRange(Comparable from, Comparable to)
    ObjectRange(Comparable from, Comparable to, boolean inclusive)
    
    void step(int step, Closure closure)
    List step(int step)
    int size()
    Comparable get(int index)
    boolean contains(Object obj)
    boolean containsWithinBounds(Object obj)
}

Usage examples:

// Integer ranges
def range1 = 1..5
def range2 = 1..<5  // exclusive
assert range1.contains(5) == true
assert range2.contains(5) == false
assert range1.size() == 5
assert range2.size() == 4

// Object ranges  
def letters = 'a'..'e'
assert letters.contains('c') == true
assert letters.size() == 5

// Range iteration
(1..3).each { println it }
(1..10).step(2) { println it }  // prints 1, 3, 5, 7, 9

System Information

GroovySystem

System-level information and version details.

class GroovySystem {
    static String getVersion()
    static MetaClassRegistry getMetaClassRegistry()
    static boolean isUseReflection()
    static void setUseReflection(boolean useReflection)
}

Usage example:

println "Groovy version: ${GroovySystem.version}"
def registry = GroovySystem.metaClassRegistry
println "MetaClass registry: $registry"

Install with Tessl CLI

npx tessl i tessl/maven-org-codehaus-groovy--groovy-all

docs

ast-transforms.md

cli.md

core-language.md

index.md

json.md

sql.md

swing.md

templates.md

xml.md

tile.json