Annotations at a Glance
The complete optic-generation surface in one page
- Which annotation to apply for each optic type, and what target each annotation expects (record, sealed interface, enum, method, package-info).
- How to choose between
@ImportOpticsand anOpticsSpecinterface for types you don't own. - The eight spec-method hints that drive optic generation for external types: prism matchers, lens copy strategies, traversal hints, and
Kind-field configuration. - Where in the book to look for the in-depth treatment of each annotation.
Every optic in this chapter is generated by an annotation. You write a record or sealed type, add the annotation, and the processor produces a typed companion class at compile time. There are no runtime reflection costs and no boilerplate to maintain.
This page is the lookup table. Each row links to the page that explores the annotation in depth.
1. Generate optics for your records
Apply these to your own records and sealed types. The generated class is placed in the same package by default; pass targetPackage = "..." to override.
| Annotation | Apply to | Generates | When to reach for it |
|---|---|---|---|
@GenerateLenses | record | XLenses (one lens per field, plus withFoo helpers) | Default starting point for any record you want to update functionally |
@GenerateFocus | record | XFocus (path-based DSL builder) | Pair with @GenerateLenses when you want fluent, IDE-friendly navigation |
@GenerateFolds | record | XFolds (read-only optics) | Querying without modification (CQRS-friendly) |
@GenerateGetters | record | XGetters (asymmetric read-only) | When you specifically want a Getter rather than a full Lens |
@GenerateSetters | record | XSetters (asymmetric write-only) | When you specifically want a Setter rather than a full Lens |
@GenerateTraversals | record containing List<T> etc. | XTraversals (one traversal per traversable field) | Bulk operations on a collection embedded in a record |
@GeneratePrisms | sealed interface or enum | XPrisms (one prism per variant) | Sum types, including both sealed hierarchies and enum constants |
@GenerateIsos | method returning Iso<A, B> | companion class with the iso as a static field | Lossless conversions between equivalent representations |
You almost always want at least @GenerateLenses and @GenerateFocus together. Add @GenerateTraversals if the record contains a collection field and @GenerateFolds if you also need read-only queries.
@GenerateLenses
@GenerateFocus
@GenerateTraversals
public record Order(Customer customer, List<LineItem> items) {}
2. Generate optics for types you don't own
External types like LocalDate, Jackson's JsonNode, JOOQ records, and Protobuf messages can't be annotated directly. Two gateways bring them into the optics world.
| Annotation | Apply to | Use when |
|---|---|---|
@ImportOptics | package-info.java or a type | The external type is a record (lenses), sealed interface (prisms), or enum (prisms) and the processor can analyse it directly |
OpticsSpec<S> | extend in your own interface | The external type defies auto-detection (no sealed hierarchy, no copy mechanism, predicate-based type checks). You declare what you want; hint annotations say how to build it. |
OpticsSpec interfaces use the spec-method hints below to tell the processor how to generate each optic.
3. Spec-method hints (inside OpticsSpec interfaces)
Apply these to abstract methods inside an interface that extends OpticsSpec<S>.
Prism hints, for sum-type-like external types
| Annotation | Generates a prism via |
|---|---|
@InstanceOf(SubType.class) | Java instanceof pattern matching (e.g. Jackson's ObjectNode, ArrayNode) |
@MatchWhen | Predicate method (isFoo()) plus a getter (asFoo()), for type-checking APIs that don't use sealed types |
Lens hints, copy strategies for legacy Java
External record-like types rarely have a single copy mechanism. The processor uses these hints to know how to rebuild the object after a set or modify.
| Annotation | Generates a lens that copies via |
|---|---|
@Wither | A withFoo(value) method on the source |
@ViaBuilder | The builder pattern (source.toBuilder().foo(value).build()) |
@ViaConstructor | The all-args constructor |
@ViaCopyAndSet | A copy constructor followed by a setter call (legacy bean style) |
Traversal hints
| Annotation | Generates |
|---|---|
@ThroughField | A traversal composing a lens-to-field with the auto-detected container traversal |
@TraverseWith | A traversal using an explicitly named Traverse instance |
Kind-field configuration
| Annotation | Effect |
|---|---|
@TraverseField | Configures how a Kind<F, A> field is processed by the Focus DSL generator. Pair with KindSemantics to declare cardinality (EXACTLY_ONE, ZERO_OR_ONE, ZERO_OR_MORE). |
4. Build setup
The HKJ Gradle and Maven plugins wire the annotation processor in for you. Apply the plugin and every annotation on this page is available immediately. See Build Plugins: One-Line HKJ Setup for the plugin DSL, or Manual Setup if you need to configure dependencies by hand.
For compile-time path-type checking, see Compile-Time Checks.
5. Quick decision guide
| You have... | You want to... | Reach for |
|---|---|---|
| A record | Get/set fields functionally | @GenerateLenses |
| A record | Navigate deeply with a fluent DSL | @GenerateLenses + @GenerateFocus |
A record with a List field | Update every element | @GenerateTraversals |
| A record | Query without modifying | @GenerateFolds |
A sealed interface or enum | Operate on one variant | @GeneratePrisms |
| Two equivalent types | Convert losslessly | @GenerateIsos on a method |
| An external record (in a library) | Lenses for its components | @ImportOptics |
| An external sealed/enum (in a library) | Prisms for its variants | @ImportOptics |
JsonNode, JOOQ records, anything tricky | Custom optics with copy strategy | @ImportOptics on an OpticsSpec interface + spec-method hints |
Where next?
- New to optics? Start with the Quickstart, three runnable examples in 100 lines.
- Updating a nested record right now? Jump to the Focus DSL.
- Working with external types? See Optics for External Types and Taming JSON with Jackson.
- Want hands-on practice? The Lens & Prism Journey is 40 minutes, 30 exercises.
Previous: Quickstart Next: Fundamentals