CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-immutables--immutables

Comprehensive Java annotation processing framework for generating immutable value objects, marshalers, repositories, and custom code generators with extensive integration support.

Pending
Overview
Eval results
Files

core-immutable.mddocs/

Core Immutable Generation

Primary annotation for generating immutable value objects with builders, copy methods, validation, and comprehensive customization options. This is the fundamental capability that enables all other features in the Immutables library.

Capabilities

Value.Immutable Annotation

The main annotation that marks interfaces or abstract classes for immutable implementation generation.

/**
 * Marks an interface or abstract class for immutable implementation generation.
 * Generates a concrete immutable class with builder pattern, copy methods, 
 * equals/hashCode, and toString implementations.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Value.Immutable {
  /** Generate singleton object pattern instead of regular immutable */
  boolean singleton() default false;
  
  /** Strongly intern instances on construction for memory efficiency */
  boolean intern() default false;
  
  /** Enable/disable copy methods (withX methods) generation */
  boolean copy() default true;
  
  /** Precompute hashCode during construction for performance */
  boolean prehash() default false;
  
  /** Enable/disable builder pattern generation */
  boolean builder() default true;
  
  /** Force JDK-only class usage, avoiding Guava dependencies */
  boolean jdkOnly() default false;
  
  /** Control visibility of generated implementation class */
  ImplementationVisibility visibility() default ImplementationVisibility.SAME;
}

enum ImplementationVisibility {
  PUBLIC,    // Generated implementation is public
  SAME,      // Same visibility as abstract type  
  PACKAGE,   // Package-private implementation
  PRIVATE    // Private implementation (requires factory methods)
}

Usage Examples:

// Basic immutable generation
@Value.Immutable
public interface Person {
  String name();
  int age();
}

// Singleton pattern
@Value.Immutable(singleton = true)
public interface Config {
  String apiUrl();
  int timeout();
}

// High-performance settings
@Value.Immutable(intern = true, prehash = true)
public interface Coordinate {
  double x();
  double y();
}

// Private implementation with factory methods
@Value.Immutable(visibility = ImplementationVisibility.PRIVATE)
public interface User {
  String username();
  String email();
  
  static User of(String username, String email) {
    return ImmutableUser.of(username, email);
  }
}

Value.Immutable.Include Annotation

Include external classes for processing in the same compilation round.

/**
 * Include external classes for processing. Useful for processing types
 * that are not directly annotated but need immutable implementations.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Value.Immutable.Include {
  /** Classes to include for immutable processing */
  Class<?>[] value();
}

Usage Example:

@Value.Immutable
@Value.Immutable.Include({ExternalType.class, AnotherType.class})
public interface MyType {
  String value();
}

Nested Types

Generate nested immutable classes under an umbrella class for better organization.

/**
 * Generate nested immutable classes under umbrella class.
 * Useful for grouping related immutable types together.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Value.Nested {}

Usage Example:

public class Models {
  @Value.Immutable
  @Value.Nested
  public interface Person {
    String name();
    int age();
  }
  
  @Value.Immutable
  @Value.Nested  
  public interface Address {
    String street();
    String city();
  }
}

// Usage
Models.ImmutablePerson person = Models.ImmutablePerson.builder()
    .name("Alice")
    .age(30)
    .build();

Builder Generation

Generate builders for static factory methods.

/**
 * Generate builders for static factory methods.
 * Applied to static methods to generate builder-style creation.
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@interface Value.Builder {}

Usage Example:

public class PersonFactory {
  @Value.Builder
  public static Person createPerson(String name, int age, Optional<String> email) {
    return ImmutablePerson.of(name, age, email);
  }
}

// Generated builder usage
Person person = new PersonFactory.CreatePersonBuilder()
    .name("Alice")
    .age(30)
    .email("alice@example.com")
    .build();

Generated Features

When @Value.Immutable is applied, the annotation processor generates:

Immutable Implementation Class

  • Naming: Immutable prefix + type name (e.g., ImmutablePerson)
  • Fields: Final fields for all attributes
  • Constructor: Package-private constructor with validation
  • Accessors: Methods matching abstract type signatures

Builder Pattern

  • Builder Class: Nested static builder class
  • Fluent API: Method chaining for all attributes
  • Validation: Integrated validation on build()
  • Factory Method: Static builder() method on implementation

Copy Methods

  • With Methods: withX() methods for creating modified copies
  • Immutable Updates: Create new instances with changed values
  • Null Safety: Proper handling of optional and nullable fields

Structural Methods

  • equals(): Deep structural equality comparison
  • hashCode(): Consistent hash code computation
  • toString(): Readable string representation with all fields

Collection Support

  • Immutable Collections: Automatic conversion to immutable collections
  • Collection Builders: Fluent API for building collections
  • Null Safety: Proper handling of null collection elements

Install with Tessl CLI

npx tessl i tessl/maven-org-immutables--immutables

docs

attributes.md

bean-style.md

code-generation.md

collections.md

core-immutable.md

index.md

integrations.md

json-marshaling.md

runtime-marshaling.md

styling.md

tile.json