Skip to content

fix: generate compilable collection helpers for primitive array fields (#261) - #267

Merged
AndreasIgel merged 20 commits into
java-helpers:mainfrom
AndreasIgel:fix/261-primitive-array-collection-helpers
Aug 23, 2026
Merged

fix: generate compilable collection helpers for primitive array fields (#261)#267
AndreasIgel merged 20 commits into
java-helpers:mainfrom
AndreasIgel:fix/261-primitive-array-collection-helpers

Conversation

@AndreasIgel

@AndreasIgelAndreasIgel commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Fixes#261

Problem

For array fields the processor generates two collection convenience helpers — field(List<E>) (ArrayConversionGenerator) and field(Consumer<ArrayListBuilder<E>>) (ArrayBuilderConsumerGenerator). Both were emitted for any array field, but their bodies were only valid for reference element types:

// for int[] scoresthis.scores = changedValue(scores.toArray(newint[0])); // List<Integer>.toArray needs a reference arraynewArrayListBuilder<>(java.util.List.of(this.scores.value())); // infers List<int[]>, not List<Integer>

Any DTO with a primitive array field produced a builder that did not compile.

Fix

Primitive element types now get explicit boxing/unboxing between the boxed List and the primitive array. Reference element types keep the existing toArray(new E[0]) / List.of(...) paths unchanged, so no generated output changes for String[], Integer[], etc.

The project already depends on Apache Commons Lang3 at runtime (the generated builders already import org.apache.commons.lang3.builder.ToStringBuilder). Its org.apache.commons.lang3.ArrayUtils provides toPrimitive/toObject for all primitive/wrapper array pairs, and java.util.Arrays.asList is used to seed the ArrayListBuilder with boxed values. With the new $label:B template placeholder and a small mapper fix to preserve explicit code-block imports, the generated setter for a field int[] scores becomes:

importjava.util.Arrays;
importorg.apache.commons.lang3.ArrayUtils;
...
publicPrimitiveArrayDtoBuilderscores(List<Integer> scores) {
this.scores = changedValue(ArrayUtils.toPrimitive(scores.toArray(newInteger[0])));
returnthis;
}
publicPrimitiveArrayDtoBuilderscores(Consumer<ArrayListBuilder<Integer>> scoresBuilderConsumer) {
ArrayListBuilder<Integer> builder;
if (this.scores.isSet()) {
builder = newArrayListBuilder<Integer>(Arrays.asList(ArrayUtils.toObject(this.scores.value())));
} else {
builder = newArrayListBuilder<Integer>(Arrays.asList());
}
scoresBuilderConsumer.accept(builder);
this.scores = changedValue(ArrayUtils.toPrimitive(builder.build().toArray(newInteger[0])));
returnthis;
}

Changes

  • RoasterMapperresolveCodeTemplate now resolves $label:B for type placeholders via the existing mapBoxedType.
  • BuilderToGenerationTypeMappertoMethodDto now copies explicit codeBlockImports from the generation-side method code DTO to the rendering-side DTO via Set.addAll(...).
  • ArrayConversionGenerator — primitive element types now use ArrayUtils.toPrimitive(list.toArray(new Boxed[0])); reference types unchanged.
  • ArrayBuilderConsumerGenerator — all array element types now seed ArrayListBuilder<T> from Arrays.asList(...) (boxed existing array for primitives, or an empty list in the unset case) and convert back with toArray(...); primitive arrays additionally box/unbox via ArrayUtils.
  • CustomCollectionTypeTest — new primitiveArrayFields_shouldGenerateCompilableCollectionHelpers covering int[], boolean[] and double[]. Each generated collection-helper method and the build() method is asserted with a single contains block spanning the full method signature and body.

Design note

The builder keeps storing the DTO's own type (TrackedValue<int[]>) and converts at the setter boundary, rather than storing a boxed List<Integer> and unboxing in build(). The list-based helpers are not the only writers of an array field — the plain setter takes int... scores, the supplier setter takes Supplier<int[]>, the copy constructor reads int[] off an existing instance, and Jackson binds against the field type — so boxed storage would need a conversion on each of those paths plus in build(), and would make the builder field's type depend on its element type. There is also no incremental add-per-element method for array fields (AddToCollectionGenerator only applies to parameterized List/Set), so the "avoid repeated reallocation while elements accumulate" argument for boxed storage does not apply here.

Verification

  • mvn clean install from the repo root: BUILD SUCCESS for all modules (core, processor, example-custom-generator, example).
  • Full processor test suite green; the new test relies on the compile-testing harness actually compiling the generated builder, which is exactly what failed before.
  • Generated sources of the example module (checked in under example/generated-example-builder) were regenerated and committed; PersonDtoBuilder now uses Arrays.asList(...) for its String[] nickNames2 consumer helper, matching the new reference-array template.

AndreasIgeland others added 13 commits August 15, 2026 10:57
The class-level Javadoc and docs/CONFIGURATION.md implied that
@SimpleBuilder is inherited by subclasses, but the annotation was not
meta-annotated with @inherited. As a result BuilderProcessor, which
collects types via RoundEnvironment.getElementsAnnotatedWith(...),
only produced builders for the exact type carrying @SimpleBuilder and
not for unannotated subclasses.
Add @inherited to @SimpleBuilder so subclasses are treated as if they
also carried the annotation, mirroring the existing behaviour of
@SimpleBuilder.Template (which is already @inherited). Update the
Javadoc to document the inheritance explicitly and clarify the
CONFIGURATION.md wording. @Ignore4BuilderGeneration still suppresses
generation for the exact type it is placed on, so opt-outs continue
to work as before.
Add SimpleBuilderInheritanceTest covering direct inheritance, the
opt-out interaction, and multi-level (grandchild) inheritance.
Closesjava-helpers#244
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…ance
Rename SimpleBuilderInheritanceTest to BuilderAnnotationInheritanceTest
so the name reflects that it covers both builder-triggering annotations.
Add unannotatedSubclassGetsBuilderFromInheritedTemplate, which verifies
that a custom @inherited template annotation (meta-annotated with
@SimpleBuilder.Template) propagates to unannotated subclasses, matching
the existing behaviour of @SimpleBuilder itself.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
The Template Javadoc and CONFIGURATION.md "Template Annotations" section
did not explain that @SimpleBuilder.Template is @inherited, nor that a
custom template annotation must additionally declare @inherited to
propagate to unannotated subclasses. Add explicit documentation and an
example showing the @inherited custom annotation pattern.
Also move assertNoBuilderGenerated to ProcessorAsserts so it is shared
by BuilderAnnotationInheritanceTest and Ignore4BuilderGenerationTest
instead of being duplicated as a private helper in each test class.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
While @SimpleBuilder and @inherited template annotations now correctly
trigger builder generation for unannotated subclasses, the configuration
options declared on the parent's @SimpleBuilder(options = ...) or template
are not yet applied to inherited subclass builders — they use default
options instead. This is tracked separately in issue java-helpers#245.
Add caveats to the SimpleBuilder Javadoc, the CONFIGURATION.md Template
Annotations section, and the Template Annotations Not Working
troubleshooting section so users are not surprised by this limitation.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Add explicit guidance that @SimpleBuilder.Template is a meta-annotation
for custom annotation declarations (@interface) only and cannot be
placed directly on a class or record. @SimpleBuilder is for direct
one-off annotation of classes/records.
- SimpleBuilder.java: add 'When to use' section to class-level Javadoc
- SimpleBuilder.Template Javadoc: state it can only be placed on
annotation types (ANNOTATION_TYPE), not on classes/records
- CONFIGURATION.md 'Template Annotations': add comparison table and
introductory paragraph
- CONFIGURATION.md troubleshooting: add item about @SimpleBuilder.Template
not being a class annotation
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…Javadoc
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…arget
The compiler and IDE already enforce @target(ANNOTATION_TYPE) and show
a clear error when @SimpleBuilder.Template is placed on a class/record,
so this troubleshooting item adds no value.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
java-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
@codecov

codecovBot commented Aug 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

devin-ai-integrationBotand others added 7 commits August 23, 2026 08:55
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…ive-array helpers
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…ve arrays (java-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…ive arrays (java-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…sumer helper (java-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…erload (java-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…a-helpers#261)
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
@AndreasIgel
AndreasIgel merged commit b882250 into java-helpers:mainAug 23, 2026
6 checks passed
@AndreasIgel
AndreasIgel deleted the fix/261-primitive-array-collection-helpers branch August 23, 2026 09:47
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Primitive array fields (e.g. int[]) generate invalid toArray(new int[0]) in collection helper methods

1 participant

@AndreasIgel