Skip to content

Field-aware member ordering in generated builders #328

Closed

Description

@igel-devin-ai

Motivation

Methods in generated builders currently sort only by a global ordering bucket, then name, then signature. Grouping all direct setters at the end of the fluent section (done in #325 so MapStruct's last-wins accessor resolution binds the real setter) disconnected each field's methods: name(String) no longer sits next to name(Supplier), nameUpdate, add2Nicknames, etc.

Proposal: full member ordering

Order generated members by:

  1. Constructors first
  2. Access modifier — public before non-public
  3. Scope — static members last
  4. Field — methods belonging to the same bean field stay together (fields in a stable order, e.g. alphabetical as in the class-javadoc example chain)
  5. Name — within a field, overloads of one name stay adjacent
  6. Existing intra-name tie-breaks (parameter count, generic vs non-generic, signature)

With this layout each field's cluster reads naturally (name(String), name(Supplier), name(String,Object...), nameUpdate(...), add2Name(...)) while still guaranteeing the direct setter is the last overload of its name — which keeps #300's MapStruct binding working without the all-setters-at-the-end tradeoff.

Precedent

The class-javadoc example chain already groups exactly this way: fields alphabetical, each field's calls in a deterministic order (BuilderJavadocExampleTest asserts .pages(...), then .tags(...), then .title(...)). The member ordering would align the emitted class with what the javadoc already presents.

Implementation notes

  • MethodDto today deliberately carries no field-origin metadata — the comparator would need a field/group key (populated in BuilderToGenerationTypeMapper from BuilderMethodDto.sourceFieldName), or a redesigned comparator in the rendering layer.
  • Within a same-name overload group the direct setter must remain last for last-wins consumers (MapStruct, see MapStruct integration: ship a BuilderProvider SPI so generated builders are auto-detected #300).
  • Regenerates example/generated-example-builder/** and the golden test expectations — large but mechanical diff.

Refs #300, #325

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions