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

transform-annotations.mddocs/

Transform Annotations

Compile-time code generation annotations that automatically add common functionality like toString(), equals(), builders, immutability, and static compilation support. These AST transformations reduce boilerplate code and enhance developer productivity.

Capabilities

Compilation Control

Annotations that control how Groovy code is compiled and type-checked.

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface CompileStatic {
    /**
     * List of extensions to apply during static compilation.
     */
    Class<? extends ExtensionMethodNode>[] extensions() default {};
    
    /**
     * Type checking mode to use.
     */
    TypeCheckingMode value() default TypeCheckingMode.PASS;
}

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface TypeChecked {
    /**
     * List of extensions to apply during type checking.
     */
    Class<? extends TypeCheckingExtension>[] extensions() default {};
    
    /**
     * Type checking mode to use.
     */
    TypeCheckingMode value() default TypeCheckingMode.PASS;
}

@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
@Retention(RetentionPolicy.SOURCE)
@interface Field {
    /**
     * Makes script variables accessible as fields.
     */
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface PackageScope {
    /**
     * Makes a class package-private instead of public.
     */
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface CompileDynamic {
    /**
     * Disables static compilation for this type or method.
     */
}

Class Generation

Annotations that generate common class functionality automatically.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Immutable {
    /**
     * Properties to exclude from immutable treatment.
     */
    String[] excludes() default {};
    
    /**
     * Whether to include fields in addition to properties.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to generate a copyWith method.
     */
    boolean copyWith() default false;
    
    /**
     * Known immutable classes.
     */
    Class<?>[] knownImmutableClasses() default {};
    
    /**
     * Known immutable classes by name.
     */
    String[] knownImmutables() default {};
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Canonical {
    /**
     * Properties to exclude from generated methods.
     */
    String[] excludes() default {};
    
    /**
     * Whether to include fields in generated methods.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to cache the hash code.
     */
    boolean cache() default false;
    
    /**
     * Whether to use super class in equals/hashCode.
     */
    boolean useCanEqual() default true;
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface ToString {
    /**
     * Properties to exclude from toString.
     */
    String[] excludes() default {};
    
    /**
     * Whether to include field names in output.
     */
    boolean includeNames() default false;
    
    /**
     * Whether to include fields in addition to properties.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to include super class in toString.
     */
    boolean includeSuper() default false;
    
    /**
     * Whether to ignore null values.
     */
    boolean ignoreNulls() default false;
    
    /**
     * Whether to include package name in class name.
     */
    boolean includePackage() default false;
    
    /**
     * Whether to cache the toString result.
     */
    boolean cache() default false;
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface EqualsAndHashCode {
    /**
     * Properties to exclude from equals/hashCode.
     */
    String[] excludes() default {};
    
    /**
     * Whether to include fields in addition to properties.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to call super in equals/hashCode.
     */
    boolean callSuper() default false;
    
    /**
     * Whether to use canEqual method.
     */
    boolean useCanEqual() default true;
    
    /**
     * Whether to cache the hash code.
     */
    boolean cache() default false;
}

Constructor Generation

Annotations that generate constructors automatically.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface TupleConstructor {
    /**
     * Properties to exclude from constructor.
     */
    String[] excludes() default {};
    
    /**
     * Properties to include in constructor.
     */
    String[] includes() default {};
    
    /**
     * Whether to include fields in addition to properties.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to include super properties.
     */
    boolean includeSuper() default false;
    
    /**
     * Whether to call super constructor.
     */
    boolean callSuper() default false;
    
    /**
     * Whether to force generation even if constructors exist.
     */
    boolean force() default false;
    
    /**
     * Default values for constructor parameters.
     */
    String[] defaults() default {};
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface InheritConstructors {
    /**
     * Whether to inherit constructors from parent class.
     */
    boolean constructorAnnotations() default false;
    
    /**
     * Whether to copy parameter annotations.
     */
    boolean parameterAnnotations() default false;
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface MapConstructor {
    /**
     * Properties to exclude from map constructor.
     */
    String[] excludes() default {};
    
    /**
     * Properties to include in map constructor.
     */
    String[] includes() default {};
    
    /**
     * Whether to include fields in addition to properties.
     */
    boolean includeFields() default false;
    
    /**
     * Whether to include super properties.
     */
    boolean includeSuper() default false;
    
    /**
     * Whether to generate a no-arg constructor.
     */
    boolean noArg() default false;
    
    /**
     * Post-processing closure.
     */
    Class<? extends Closure> post() default Closure.class;
}

Builder Pattern

Annotations for generating builder pattern implementations.

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.SOURCE)
@interface Builder {
    /**
     * Builder strategy to use.
     */
    Class<?> builderStrategy() default DefaultStrategy.class;
    
    /**
     * Builder class name prefix.
     */
    String builderClassName() default "";
    
    /**
     * Builder method name.
     */
    String builderMethodName() default "builder";
    
    /**
     * Build method name.
     */
    String buildMethodName() default "build";
    
    /**
     * Properties to exclude from builder.
     */
    String[] excludes() default {};
    
    /**
     * Properties to include in builder.
     */
    String[] includes() default {};
    
    /**
     * Whether to include super properties.
     */
    boolean includeSuper() default false;
    
    /**
     * Prefix for setter methods.
     */
    String prefix() default "";
    
    /**
     * Whether to force generation.
     */
    boolean force() default false;
}

Field and Method Enhancement

Annotations that enhance fields and methods with additional behavior.

@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface Lazy {
    /**
     * Whether to use soft references for lazy values.
     */
    boolean soft() default false;
}

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.SOURCE)
@interface Delegate {
    /**
     * Interfaces to delegate to.
     */
    Class<?>[] interfaces() default {};
    
    /**
     * Methods to exclude from delegation.
     */
    String[] excludes() default {};
    
    /**
     * Types of methods to exclude.
     */
    String[] excludeTypes() default {};
    
    /**
     * Whether to include deprecated methods.
     */
    boolean includeDeprecated() default true;
    
    /**
     * Whether to use parameter names in method signatures.
     */
    boolean parameterAnnotations() default false;
    
    /**
     * Whether to copy method annotations.
     */
    boolean methodAnnotations() default false;
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface Memoized {
    /**
     * Maximum cache size.
     */
    int maxCacheSize() default 0;
    
    /**
     * Whether to protect from recursion.
     */
    boolean protectedFromRecursion() default false;
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface TailRecursive {
    /**
     * Optimizes tail-recursive methods.
     */
}

Synchronization

Annotations for thread-safe method execution.

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface Synchronized {
    /**
     * Lock field name to use for synchronization.
     */
    String value() default "";
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface WithReadLock {
    /**
     * Lock field name to use for read locking.
     */
    String value() default "$reentrantlock";
}

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
@interface WithWriteLock {
    /**
     * Lock field name to use for write locking.
     */
    String value() default "$reentrantlock";
}

Bean Properties

Annotations for property binding and change notification.

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Bindable {
    /**
     * Generates PropertyChangeSupport for bindable properties.
     */
}

@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Vetoable {
    /**
     * Generates VetoableChangeSupport for vetoable properties.
     */
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface ListenerList {
    /**
     * Generates listener list support.
     */
}

Category Support

Annotations for category-based extensions.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Category {
    /**
     * Class to add methods to.
     */
    Class<?> value();
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Mixin {
    /**
     * Mixin classes to apply.
     */
    Class<?>[] value();
}

Singleton Pattern

Annotation for generating singleton implementations.

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
@interface Singleton {
    /**
     * Property name for singleton instance.
     */
    String property() default "instance";
    
    /**
     * Whether to use lazy initialization.
     */
    boolean lazy() default true;
    
    /**
     * Whether to make singleton strict (prevent inheritance).
     */
    boolean strict() default true;
}

Usage Examples

Immutable Classes

import groovy.transform.Immutable;

@Immutable
class Person {
    String name;
    int age;
    List<String> hobbies;
}

// Usage
Person person = new Person("John", 30, ["reading", "hiking"]);
// person.name = "Jane"; // This would cause a compile error

// With copyWith
@Immutable(copyWith = true)
class Address {
    String street;
    String city;
    String country;
}

Address address = new Address("123 Main St", "Anytown", "USA");
Address newAddress = address.copyWith(city: "New City");

Builder Pattern

import groovy.transform.builder.Builder;

@Builder
class Car {
    String make;
    String model;
    int year;
    String color;
    boolean convertible;
}

// Usage
Car car = Car.builder()
    .make("Toyota")
    .model("Camry")
    .year(2023)
    .color("Blue")
    .convertible(false)
    .build();

ToString and Equals Generation

import groovy.transform.ToString;
import groovy.transform.EqualsAndHashCode;

@ToString(includeNames = true, includeFields = true)
@EqualsAndHashCode(includeFields = true)
class Book {
    String title;
    String author;
    int pages;
    private String isbn;
}

// Usage
Book book1 = new Book(title: "Groovy in Action", author: "Dierk König", pages: 500);
Book book2 = new Book(title: "Groovy in Action", author: "Dierk König", pages: 500);

System.out.println(book1.toString());
System.out.println(book1.equals(book2)); // true

Lazy Initialization

import groovy.transform.Lazy;

class DataProcessor {
    @Lazy
    private ExpensiveResource resource = new ExpensiveResource();
    
    @Lazy(soft = true)
    private List<String> cache = loadCacheData();
    
    public void process() {
        // resource is initialized only when first accessed
        resource.doWork();
    }
    
    private List<String> loadCacheData() {
        // Expensive operation
        return Arrays.asList("data1", "data2", "data3");
    }
}

Memoization

import groovy.transform.Memoized;

class Calculator {
    @Memoized
    public long fibonacci(int n) {
        if (n <= 1) return n;
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
    
    @Memoized(maxCacheSize = 100)
    public double expensiveCalculation(double x, double y) {
        // Simulate expensive computation
        Thread.sleep(1000);
        return Math.pow(x, y);
    }
}

// Usage
Calculator calc = new Calculator();
long fib = calc.fibonacci(40); // Calculated once, then cached

Synchronization

import groovy.transform.Synchronized;
import groovy.transform.WithReadLock;
import groovy.transform.WithWriteLock;

class ThreadSafeCounter {
    private int count = 0;
    private final Object lock = new Object();
    
    @Synchronized("lock")
    public void increment() {
        count++;
    }
    
    @Synchronized("lock")
    public int getCount() {
        return count;
    }
}

class ReadWriteExample {
    private String data = "";
    private final java.util.concurrent.locks.ReentrantReadWriteLock rwLock = 
        new java.util.concurrent.locks.ReentrantReadWriteLock();
    
    @WithReadLock
    public String getData() {
        return data;
    }
    
    @WithWriteLock
    public void setData(String newData) {
        this.data = newData;
    }
}

Bindable Properties

import groovy.transform.Bindable;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

@Bindable
class Model {
    String name;
    int value;
}

// Usage
Model model = new Model();
model.addPropertyChangeListener(new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent evt) {
        System.out.println("Property " + evt.getPropertyName() + 
                          " changed from " + evt.getOldValue() + 
                          " to " + evt.getNewValue());
    }
});

model.setName("New Name"); // Fires property change event
model.setValue(42); // Fires property change event

Static Compilation

import groovy.transform.CompileStatic;
import groovy.transform.TypeChecked;

@CompileStatic
class MathUtils {
    public static int add(int a, int b) {
        return a + b; // Statically compiled
    }
    
    @TypeChecked
    public static double divide(double a, double b) {
        if (b == 0) {
            throw new IllegalArgumentException("Division by zero");
        }
        return a / b; // Type checked but dynamically compiled
    }
}

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