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

integrations.mddocs/

Third-Party Integrations

Integration annotations for popular frameworks including Jackson, Gson, and MongoDB with seamless serialization and repository generation. These integrations allow Immutables to work smoothly with existing Java ecosystem tools.

Capabilities

Jackson Integration

Integration with Jackson JSON processing library for seamless serialization.

/**
 * Enable Jackson ObjectMapper serialization for immutable types.
 * Generates appropriate Jackson annotations (@JsonCreator, @JsonValue)
 * and delegation methods for TokenBuffer-based marshaling.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Jackson.Mapped {}

Usage Example:

import org.immutables.value.Value;
import org.immutables.value.Jackson;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Optional;
import java.util.List;

@Value.Immutable
@Jackson.Mapped
public interface User {
  String username();
  String email();
  int age();
  Optional<String> fullName();
  List<String> roles();
}

// Jackson integration usage
ObjectMapper mapper = new ObjectMapper();

User user = ImmutableUser.builder()
    .username("alice")
    .email("alice@example.com")
    .age(30)
    .fullName("Alice Smith")
    .addRoles("user", "admin")
    .build();

// Serialize with Jackson
String json = mapper.writeValueAsString(user);

// Deserialize with Jackson
User restored = mapper.readValue(json, User.class);

// Works with Jackson modules and configurations
mapper.registerModule(new JavaTimeModule());
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Gson Integration

Integration with Google Gson library for JSON serialization.

/**
 * Generate Gson TypeAdapterFactory for immutable types.
 * Creates efficient type adapters that integrate with Gson's
 * serialization and deserialization system.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Gson.TypeAdapted {}

/**
 * Specify custom Gson field names (similar to @SerializedName).
 * Overrides the default field name used in JSON serialization.
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@interface Gson.Named {
  /** Custom field name for Gson serialization */
  String value();
}

/**
 * Specify expected subclasses for Gson polymorphic marshaling.
 * Enables Gson to handle inheritance hierarchies correctly.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Gson.Subclasses {
  /** Subclass types for polymorphic handling */
  Class<?>[] value();
}

Usage Examples:

import org.immutables.value.Value;
import org.immutables.gson.Gson;
import java.util.List;
import java.util.Map;
import java.time.Instant;

// Basic Gson integration
@Value.Immutable
@Gson.TypeAdapted
public interface Product {
  String name();
  double price();
  List<String> categories();
}

// Custom field names
@Value.Immutable
@Gson.TypeAdapted
public interface ApiRequest {
  @Gson.Named("request_id")
  String requestId();
  
  @Gson.Named("user_data")
  Map<String, Object> userData();
  
  @Gson.Named("created_at")
  Instant createdAt();
}

// Polymorphic types
@Value.Immutable
@Gson.TypeAdapted
@Gson.Subclasses({Circle.class, Rectangle.class})
public interface Shape {
  String type();
  double area();
}

@Value.Immutable
@Gson.TypeAdapted
public interface Circle extends Shape {
  double radius();
  
  @Value.Default
  default String type() { return "circle"; }
  
  @Value.Derived
  default double area() { 
    return Math.PI * radius() * radius(); 
  }
}

// Gson usage
import com.google.gson.GsonBuilder;

GsonBuilder gsonBuilder = new GsonBuilder()
    .registerTypeAdapterFactory(new GsonAdaptersProduct())  // Generated factory
    .registerTypeAdapterFactory(new GsonAdaptersApiRequest())
    .registerTypeAdapterFactory(new GsonAdaptersShape());

com.google.gson.Gson gson = gsonBuilder.create();

Product product = ImmutableProduct.builder()
    .name("Laptop")
    .price(999.99)
    .addCategories("electronics", "computers")
    .build();

String json = gson.toJson(product);
Product restored = gson.fromJson(json, Product.class);

MongoDB Integration

Generate MongoDB repositories and handle document mapping.

/**
 * Generate MongoDB repository for immutable types.
 * Creates repository classes that integrate with MongoDB driver
 * for CRUD operations on immutable objects.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.SOURCE)
@interface Mongo.Repository {
  /** 
   * MongoDB collection name. If empty, derives from type name.
   * @return collection name
   */
  String value() default "";
}

/**
 * Mark an attribute as MongoDB _id field.
 * The attribute will be mapped to MongoDB's _id field
 * and handled appropriately during serialization.
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
@interface Mongo.Id {}

Usage Example:

import org.immutables.value.Value;
import org.immutables.value.Mongo;
import com.mongodb.client.MongoDatabase;
import java.time.Instant;
import java.util.List;
import java.util.Optional;

@Value.Immutable
@Mongo.Repository("users")
public interface User {
  @Mongo.Id
  String id();
  
  String username();
  String email();
  Instant createdAt();
  List<String> tags();
  Optional<String> lastLogin();
}

// Generated repository usage
MongoDatabase database = mongoClient.getDatabase("myapp");
UserRepository userRepo = new UserRepository(database);

// Create user
User user = ImmutableUser.builder()
    .id("user123")
    .username("alice")
    .email("alice@example.com")
    .createdAt(Instant.now())
    .addTags("developer", "java")
    .build();

// Repository operations
userRepo.insert(user);
userRepo.save(user);

Optional<User> found = userRepo.findById("user123");
List<User> developers = userRepo.findByTag("developer");

User updated = user.withLastLogin(Instant.now().toString());
userRepo.update(updated);

userRepo.delete("user123");

Generated Repository Methods

When @Mongo.Repository is applied, the annotation processor generates:

// Generated repository class
public class UserRepository {
  public UserRepository(MongoDatabase database) { ... }
  
  // Basic CRUD operations
  public void insert(User user) { ... }
  public void save(User user) { ... }
  public Optional<User> findById(String id) { ... }
  public void update(User user) { ... }
  public void delete(String id) { ... }
  
  // Query methods based on attributes
  public List<User> findByUsername(String username) { ... }
  public List<User> findByEmail(String email) { ... }
  public List<User> findByTag(String tag) { ... }
  
  // Aggregation support
  public long count() { ... }
  public List<User> findAll() { ... }
  public List<User> findAll(int skip, int limit) { ... }
}

Framework-Specific Features

Jackson Advanced Features

@Value.Immutable
@Jackson.Mapped
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public interface AdvancedJacksonType {
  String firstName();
  String lastName();
  
  @JsonProperty("birth_date")
  LocalDate birthDate();
  
  @JsonIgnore
  String internalField();
}

Gson Advanced Features

@Value.Immutable
@Gson.TypeAdapted
public interface AdvancedGsonType {
  @Gson.Named("user_id")
  String userId();
  
  @SerializedName("created_timestamp") 
  long createdTimestamp();
  
  // Gson respects @Expose annotations
  @Expose(serialize = false)
  String secretData();
}

MongoDB Advanced Features

@Value.Immutable
@Mongo.Repository
public interface AdvancedMongoType {
  @Mongo.Id
  ObjectId id();
  
  String name();
  
  @BsonProperty("created_date")
  Date createdDate();
  
  @BsonIgnore
  String computedField();
  
  // Embedded documents
  Address address();
  
  // Collections
  List<Tag> tags();
}

@Value.Immutable
public interface Address {
  String street();
  String city();
  String country();
}

@Value.Immutable
public interface Tag {
  String name();
  String color();
}

Integration Dependencies

To use these integrations, add the appropriate dependencies:

Jackson

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.15.2</version>
</dependency>

Gson

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.10.1</version>
</dependency>

MongoDB

<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongodb-driver-sync</artifactId>
  <version>4.9.1</version>
</dependency>

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