Repository navigation
Generated Java Code
JavaModelMaker emits one .java file per schema: a final, immutable class with no setters, built through a
builder. This page walks the output member by member.
Everything below is produced from this schema, with jackson, validation and openApi on:
{
"$modelmaker": "v1.0",
"title": "com.example.dto.Customer",
"description": "Combines Jackson binding, jakarta.validation constraints and OpenAPI @Schema metadata on one type.",
"type": "object",
"required": ["id", "email", "shippingAddress"],
"properties": {
"id": { "type": "string", "pattern": "^C\\d+$", "description": "Customer identifier, C followed by digits.", "example": "C42" },
"email": { "type": "string", "format": "email", "description": "Primary contact email.", "example": "ada@example.com" },
"loyaltyPoints": { "type": "integer", "minimum": 0, "maximum": 100000, "default": 0, "description": "Accrued loyalty points." },
"shippingAddress": { "$ref": "com.example.dto.Address" }
}
}package com.example.dto;
import com.fasterxml.jackson.annotation.JsonCreator;
// ... one import per line, sorted, never a wildcard
// Generated by ModelMaker (io.github.malczuuu.modelmaker). Do not edit manually.That banner is the only marker a generated file carries. There is deliberately no
javax.annotation.processing.Generated annotation: it would force every consumer with a module-info.java to
requires java.compiler, and it says nothing the banner does not.
Imports are computed from what is actually emitted - a type with no constraints pulls in no
jakarta.validation imports even with the feature on.
@NullMarked
@Schema(description = "Combines Jackson binding, jakarta.validation constraints and OpenAPI @Schema metadata on one type.")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonPropertyOrder({"id", "email", "loyaltyPoints", "shippingAddress"})
public final class Customer {-
@NullMarked- always, on the top-level class only. -
@Schema(description = ...)-openApion and the schema has adescription. -
@JsonIgnoreProperties(ignoreUnknown = true)-jacksonon. -
@JsonPropertyOrder({...})-jacksonon and the type has properties; the order follows the schema.
The class is final (public static final for a nested type) and has no setters.
private final String id;
private final String email;
private final Integer loyaltyPoints;
private final Address shippingAddress;All private final. A property that is neither required nor defaulted is @Nullable:
private final @Nullable String nickname;loyaltyPoints above is optional but has "default": 0, so it can never be null and carries no @Nullable. See
Schema Format.
@JsonCreator
Customer(
@JsonProperty("id") String id,
@JsonProperty("email") String email,
@JsonProperty("loyaltyPoints") @Nullable Integer loyaltyPoints,
@JsonProperty("shippingAddress") Address shippingAddress) {
this.id = id;
this.email = email;
this.loyaltyPoints = loyaltyPoints != null ? loyaltyPoints : 0;
this.shippingAddress = shippingAddress;
}- All-args (no-arg when the type has no properties).
-
privatenormally; package-private withjacksonon, so Jackson binds straight through it instead of through the builder. - Defaults are applied here, which is why a deserialized instance gets them too.
- The parameter for a defaulted property is
@Nullable- the caller may omit it - while the field is not.
@Schema(description = "Customer identifier, C followed by digits.", example = "C42", requiredMode = Schema.RequiredMode.REQUIRED)
@JsonProperty("id")
@NotNull(message = "must not be null")
@Pattern(regexp = "^C\\d+$", message = "must match \"^C\\d+$\"")
public String getId() {
return id;
}Annotation order is stable: OpenAPI, then Jackson, then validation. By default all three features annotate the getters; Feature Configuration explains how to move them onto the fields.
Getters defend mutable state:
-
Scalar,
$ref, nested type - the field as-is. -
List<T>-Collections.unmodifiableList(...), or... != null ? ... : nullwhen optional. -
byte[]-.clone(), or... != null ? ....clone() : nullwhen optional.
public Builder mutate() {
return builder()
.id(id)
.email(email)
.loyaltyPoints(loyaltyPoints)
.shippingAddress(shippingAddress);
}A builder pre-filled with the current state - the idiomatic "copy with changes":
Customer updated = customer.mutate().email("new@example.com").build();Kotlin callers can get a DSL form of the same thing, see Kotlin Extensions.
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Customer other)) {
return false;
}
return Objects.equals(id, other.id)
&& Objects.equals(email, other.email)
&& Objects.equals(loyaltyPoints, other.loyaltyPoints)
&& Objects.equals(shippingAddress, other.shippingAddress);
}- Value semantics over every property.
-
byte[]usesArrays.equals/Arrays.hashCode, andtoStringprints its base64 form. -
toStringisCustomer[id=..., email=...]. - A property declared
"sensitive": trueprints as***intoString- on the DTO and on the builder - whileequalsandhashCodekeep comparing the real value. See Schema Format.
public static Builder builder() {
return new Builder();
}
public static final class Builder implements BuilderMutator {
private Builder() {}
private @Nullable String id;
// ...
@Override
public Builder id(@Nullable String id) {
this.id = id;
return this;
}
public Customer build() {
return new Customer(
Objects.requireNonNull(id, "id is required"),
Objects.requireNonNull(email, "email is required"),
loyaltyPoints,
Objects.requireNonNull(shippingAddress, "shippingAddress is required"));
}
}- The only way to construct an instance (the constructor is not public).
- Setter methods are named after the property -
id(...), notsetId(...)- and returnthis. - Every setter accepts
@Nullable, so a builder can be filled in any order; the check happens inbuild(). -
build()throwsNullPointerExceptionwith"<name> is required"for a missing required property. - A
Listis copied (new ArrayList<>(...)) and abyte[]cloned as they enter the builder. - The builder has a
toString, which helps when abuild()fails in a test.
public sealed interface BuilderMutator permits Builder {
BuilderMutator id(@Nullable String id);
// ...
}A sealed view of the builder that exposes the setters but not build(). It is what the Kotlin mutate { } block
receives, so the block can only configure - never build or leak the builder.
An inline object property renders as a nested public static final class with the same complete shape - fields,
constructor, getters, builder, BuilderMutator, equals / hashCode / toString. Only the top-level class carries
@NullMarked; nullability is inherited by the whole file.
Off by default. With withers { enabled = true }, every property gets a single-field copy method:
public Customer withEmail(String email) {
return new Customer(this.id, Objects.requireNonNull(email), this.loyaltyPoints, this.shippingAddress);
}
public Customer withNickname(@Nullable String nickname) {
return new Customer(this.id, this.email, nickname, this.shippingAddress);
}- Returns a new instance; the receiver is untouched.
- The parameter mirrors the property: a non-null property takes the plain type and is guarded with
Objects.requireNonNull, an optional one takes@Nullable. - A
byte[]replacement is cloned. - For several changes at once,
mutate()is cheaper - one copy instead of one per field.
Off by default. With preferPrimitives { enabled = true }, a property that can never be null - required, or
carrying a default - uses the primitive type:
| Boxed | Primitive |
|---|---|
Integer |
int |
Long |
long |
Double |
double |
Float |
float |
Boolean |
boolean |
Optional properties stay boxed, since they must be able to hold null. Builder setters keep the boxed, @Nullable
parameter type in every case - only the field, the constructor parameter and the getter change.
Generated code is JSpecify-annotated: @NullMarked on the top-level class, @Nullable on every optional field,
parameter and return. Consumers get correct nullability in the IDE and under a null checker such as NullAway.
The Gradle plugin puts org.jspecify:jspecify on compileOnly unless the build already declares it, see
Gradle Plugin.
Don't. The output directory is deleted and rewritten on every run of generateModelJava. Change the schema, or the
feature flags, and rebuild.
ModelMaker - Apache License 2.0 - Issues - Wiki index
Start here
Schemas
Output
Features
Integration
Reference