CtrlK
BlogDocsLog inGet started
Tessl Logo

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

Apache Groovy is a powerful multi-faceted programming language for the JVM platform

Pending
Overview
Eval results
Files

core-language.mddocs/

Core Language Runtime

Essential runtime classes and interfaces that enable Groovy's dynamic behavior, meta-programming capabilities, and integration with the JVM. These components form the foundation of Groovy's language features including closures, scripts, meta-programming, and dynamic method dispatch.

Capabilities

GroovyObject Interface

The root interface that all Groovy objects implement, providing the foundation for dynamic method and property access.

interface GroovyObject {
    /**
     * Invokes a method on this object with the given name and arguments.
     */
    Object invokeMethod(String name, Object args);
    
    /**
     * Gets the value of a property.
     */
    Object getProperty(String propertyName);
    
    /**
     * Sets the value of a property.
     */
    void setProperty(String propertyName, Object newValue);
    
    /**
     * Gets the MetaClass for this object.
     */
    MetaClass getMetaClass();
    
    /**
     * Sets the MetaClass for this object.
     */
    void setMetaClass(MetaClass metaClass);
}

MetaClass System

The meta-programming interface that controls method dispatch and property access for objects.

interface MetaClass {
    /**
     * Invokes a method on the given object.
     */
    Object invokeMethod(Object object, String methodName, Object[] arguments);
    
    /**
     * Gets a property value from the given object.
     */
    Object getProperty(Object object, String propertyName);
    
    /**
     * Sets a property value on the given object.
     */
    void setProperty(Object object, String propertyName, Object newValue);
    
    /**
     * Checks if the object responds to the given method name.
     */
    boolean respondsTo(Object obj, String name);
    
    /**
     * Checks if the object has the given property.
     */
    MetaProperty hasProperty(Object obj, String name);
    
    /**
     * Gets all methods available on this MetaClass.
     */
    List<MetaMethod> getMethods();
    
    /**
     * Gets all properties available on this MetaClass.
     */
    List<MetaProperty> getProperties();
}

class ExpandoMetaClass implements MetaClass {
    /**
     * Defines a new property on this MetaClass.
     */
    void defineProperty(String name, Object value);
    
    /**
     * Adds a method using the << operator.
     */
    void leftShift(Map<String, Closure> methodMap);
    
    /**
     * Adds a property using the >> operator.
     */
    void rightShift(Map<String, Object> propertyMap);
    
    /**
     * Enables global changes to this MetaClass.
     */
    void enableGlobally();
}

Groovy Shell

Shell for parsing and executing Groovy code dynamically at runtime.

class GroovyShell {
    /**
     * Creates a new GroovyShell with default settings.
     */
    GroovyShell();
    
    /**
     * Creates a new GroovyShell with the specified ClassLoader.
     */
    GroovyShell(ClassLoader parent);
    
    /**
     * Creates a new GroovyShell with the specified Binding.
     */
    GroovyShell(Binding binding);
    
    /**
     * Creates a new GroovyShell with ClassLoader and Binding.
     */
    GroovyShell(ClassLoader parent, Binding binding);
    
    /**
     * Parses the given script text and returns a Script object.
     */
    Script parse(String scriptText);
    
    /**
     * Parses the given file and returns a Script object.
     */
    Script parse(File file);
    
    /**
     * Evaluates the given script text and returns the result.
     */
    Object evaluate(String scriptText);
    
    /**
     * Evaluates the given file and returns the result.
     */
    Object evaluate(File file);
    
    /**
     * Runs the given script with arguments.
     */
    Object run(String scriptText, String[] args);
}

GroovyClassLoader

ClassLoader implementation for loading and compiling Groovy classes at runtime.

class GroovyClassLoader extends URLClassLoader {
    /**
     * Creates a new GroovyClassLoader.
     */
    GroovyClassLoader();
    
    /**
     * Creates a new GroovyClassLoader with the specified parent.
     */
    GroovyClassLoader(ClassLoader parent);
    
    /**
     * Parses a class from the given text.
     */
    Class<?> parseClass(String text);
    
    /**
     * Parses a class from the given file.
     */
    Class<?> parseClass(File file);
    
    /**
     * Loads a class by name.
     */
    Class<?> loadClass(String name);
    
    /**
     * Clears the internal cache.
     */
    void clearCache();
}

Extension Methods and DefaultGroovyMethods

Groovy adds hundreds of extension methods to existing Java classes through DefaultGroovyMethods, providing powerful collection processing, string manipulation, and utility functions.

/**
 * Extension methods added to Object and its subclasses
 */
class DefaultGroovyMethods {
    /**
     * Collection processing methods
     */
    static <T> List<T> findAll(Collection<T> self, Closure closure);
    static <T> T find(Collection<T> self, Closure closure);
    static <T> T inject(Collection<T> self, T initialValue, Closure closure);
    static <T> List<T> collect(Collection<T> self, Closure closure);
    static <T> List<T> sort(Collection<T> self);
    static <T> List<T> sort(Collection<T> self, Closure closure);
    static <T> List<T> unique(Collection<T> self);
    static <T> Map<Object, List<T>> groupBy(Collection<T> self, Closure closure);
    static int sum(Collection<Number> self);
    static <T> T max(Collection<T> self);
    static <T> T min(Collection<T> self);
    
    /**
     * String manipulation methods
     */
    static boolean isNumber(String self);
    static String padLeft(String self, int numberOfCharacters);
    static String padRight(String self, int numberOfCharacters);
    static String center(String self, int numberOfCharacters);
    static String capitalize(String self);
    static List<String> tokenize(String self);
    static List<String> tokenize(String self, String token);
    
    /**
     * Iteration methods
     */
    static void times(Integer self, Closure closure);
    static void upto(Number self, Number to, Closure closure);
    static void downto(Number self, Number to, Closure closure);
    
    /**
     * General utility methods
     */
    static String dump(Object self);
    static Object identity(Object self, Closure closure);
    static Object with(Object self, Closure closure);
    static boolean asBoolean(Object self);
    static String toString(Object self);
}

/**
 * String-specific extension methods
 */
class StringGroovyMethods {
    static String multiply(String self, int factor);
    static String reverse(String self);
    static boolean startsWith(String self, String prefix);
    static boolean endsWith(String self, String suffix);
    static String[] split(String self, String regex);
    static String replaceAll(String self, String regex, String replacement);
    static String replaceFirst(String self, String regex, String replacement);
    static List<String> readLines(String self);
    static void eachLine(String self, Closure closure);
}

Usage Examples:

// Collection processing
def numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// Find elements
def evens = numbers.findAll { it % 2 == 0 }
assert evens == [2, 4, 6, 8, 10]

def firstBig = numbers.find { it > 5 }
assert firstBig == 6

// Transform collections
def doubled = numbers.collect { it * 2 }
assert doubled == [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

// Aggregate operations
def total = numbers.sum()
assert total == 55

def product = numbers.inject(1) { acc, val -> acc * val }
assert product == 3628800 // 10!

// Grouping and sorting
def words = ['apple', 'banana', 'apricot', 'blueberry', 'cherry']
def byFirstLetter = words.groupBy { it[0] }
assert byFirstLetter.a == ['apple', 'apricot']
assert byFirstLetter.b == ['banana', 'blueberry']

def sorted = words.sort { it.length() }
assert sorted[0] == 'apple'

// String operations
def text = "hello world"
assert text.capitalize() == "Hello world"
assert text.padLeft(15) == "    hello world"
assert text.center(15) == "  hello world  "

// Check if string is numeric
assert "123".isNumber()
assert !"abc".isNumber()

// String multiplication
assert "ha" * 3 == "hahaha"

// Number iteration
def result = []
5.times { result << it }
assert result == [0, 1, 2, 3, 4]

def countdown = []
5.downto(1) { countdown << it }
assert countdown == [5, 4, 3, 2, 1]

// Object utilities
def person = [name: 'John', age: 30]
person.with {
    name = name.toUpperCase()
    age += 1
}
assert person.name == 'JOHN'
assert person.age == 31

Script Base Class

Base class for all Groovy scripts providing access to bindings and output methods.

abstract class Script extends GroovyObjectSupport {
    /**
     * The main method that executes the script.
     */
    public abstract Object run();
    
    /**
     * Gets the binding for this script.
     */
    public Binding getBinding();
    
    /**
     * Sets the binding for this script.
     */
    public void setBinding(Binding binding);
    
    /**
     * Prints a value to the output.
     */
    public void print(Object value);
    
    /**
     * Prints a value with a newline to the output.
     */
    public void println(Object value);
    
    /**
     * Printf-style formatted output.
     */
    public void printf(String format, Object... values);
}

Variable Binding

Container for script variables and their values.

class Binding extends LinkedHashMap<String, Object> {
    /**
     * Creates an empty binding.
     */
    Binding();
    
    /**
     * Creates a binding with the given variables.
     */
    Binding(Map<String, Object> variables);
    
    /**
     * Gets the value of a variable.
     */
    Object getVariable(String name);
    
    /**
     * Sets the value of a variable.
     */
    void setVariable(String name, Object value);
    
    /**
     * Gets all variables as a map.
     */
    Map<String, Object> getVariables();
    
    /**
     * Checks if a variable exists.
     */
    boolean hasVariable(String name);
}

Closures

Groovy's closure implementation providing functional programming capabilities.

abstract class Closure<V> implements Cloneable, Runnable, GroovyCallable<V> {
    /**
     * Calls the closure with no arguments.
     */
    V call();
    
    /**
     * Calls the closure with the given arguments.
     */
    V call(Object... args);
    
    /**
     * Creates a curried version of this closure.
     */
    Closure<V> curry(Object... args);
    
    /**
     * Creates a right-curried version of this closure.
     */
    Closure<V> rcurry(Object... args);
    
    /**
     * Creates a memoized version of this closure.
     */
    Closure<V> memoize();
    
    /**
     * Creates a trampoline version for tail recursion.
     */
    TrampolineClosure<V> trampoline();
    
    /**
     * Composes this closure with another (this << other).
     */
    Closure<V> leftShift(Closure<V> other);
    
    /**
     * Composes this closure with another (this >> other).
     */
    Closure<V> rightShift(Closure<V> other);
    
    /**
     * Gets the owner of this closure.
     */
    Object getOwner();
    
    /**
     * Gets the delegate of this closure.
     */
    Object getDelegate();
    
    /**
     * Sets the delegate of this closure.
     */
    void setDelegate(Object delegate);
    
    /**
     * Gets the resolve strategy for this closure.
     */
    int getResolveStrategy();
    
    /**
     * Sets the resolve strategy for this closure.
     */
    void setResolveStrategy(int resolveStrategy);
}

String Interpolation

Groovy's interpolated string implementation.

abstract class GString implements Comparable<Object>, CharSequence, Writable, Buildable {
    /**
     * Converts this GString to a String.
     */
    String toString();
    
    /**
     * Gets the interpolated values.
     */
    Object[] getValues();
    
    /**
     * Gets the string parts.
     */
    String[] getStrings();
    
    /**
     * Gets the length of the string representation.
     */
    int length();
    
    /**
     * Gets the character at the specified index.
     */
    char charAt(int index);
    
    /**
     * Returns a subsequence of this GString.
     */
    CharSequence subSequence(int start, int end);
}

Ranges

Groovy's range implementations for numeric and object sequences.

interface Range<T extends Comparable<T>> extends List<T> {
    /**
     * Gets the from value of this range.
     */
    T getFrom();
    
    /**
     * Gets the to value of this range.
     */
    T getTo();
    
    /**
     * Checks if this range contains the given value.
     */
    boolean contains(Object obj);
    
    /**
     * Gets the size of this range.
     */
    int size();
    
    /**
     * Creates a reversed range.
     */
    List<T> reverse();
    
    /**
     * Steps through the range with the given step size.
     */
    List<T> step(int step);
}

class IntRange implements Range<Integer> {
    /**
     * Creates an integer range.
     */
    IntRange(int from, int to);
    
    /**
     * Gets the from value as an int.
     */
    int getFromInt();
    
    /**
     * Gets the to value as an int.
     */
    int getToInt();
    
    /**
     * Steps through the range with the given step.
     */
    void step(int step, Closure closure);
}

class ObjectRange implements Range<Object> {
    /**
     * Creates an object range.
     */
    ObjectRange(Comparable from, Comparable to);
    
    /**
     * Steps through the range with the given step.
     */
    void step(int step, Closure closure);
}

Tuples

Immutable sequence implementation for multiple return values.

class Tuple implements List<Object> {
    /**
     * Creates a tuple from the given objects.
     */
    Tuple(Object... objects);
    
    /**
     * Gets the element at the specified index.
     */
    Object get(int index);
    
    /**
     * Gets the size of this tuple.
     */
    int size();
    
    /**
     * Creates a sub-tuple.
     */
    Tuple subTuple(int startIndex, int endIndex);
    
    /**
     * Converts to an array.
     */
    Object[] toArray();
}

Usage Examples

Dynamic Script Execution

import groovy.lang.GroovyShell;
import groovy.lang.Binding;

// Create shell with binding
Binding binding = new Binding();
binding.setVariable("x", 10);
binding.setVariable("y", 20);

GroovyShell shell = new GroovyShell(binding);
Object result = shell.evaluate("return x + y * 2");
System.out.println(result); // Prints: 50

Meta-Programming with ExpandoMetaClass

import groovy.lang.ExpandoMetaClass;

// Add method to String class
ExpandoMetaClass stringMeta = new ExpandoMetaClass(String.class, false);
stringMeta.defineProperty("reverse", new Closure<String>(null) {
    public String doCall() {
        return new StringBuilder((String)getDelegate()).reverse().toString();
    }
});
stringMeta.initialize();

// Use the new method
String text = "hello";
String reversed = (String) stringMeta.getProperty(text, "reverse");

Closure Usage

import groovy.lang.Closure;

// Create and use a closure
Closure<String> greetClosure = new Closure<String>(null) {
    public String doCall(Object name) {
        return "Hello " + name + "!";
    }
};

String greeting = greetClosure.call("World");
System.out.println(greeting); // Prints: Hello World!

// Curry the closure
Closure<String> helloJohn = greetClosure.curry("John");
System.out.println(helloJohn.call()); // Prints: Hello John!

Install with Tessl CLI

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

docs

ast-compilation.md

collections-utilities.md

config-data.md

core-language.md

dependency-management.md

index.md

io-file-processing.md

json-processing.md

sql-database.md

template-engines.md

testing-apis.md

time-date.md

transform-annotations.md

xml-processing.md

tile.json