Skip to content

Generate primitive functional interfaces (BooleanSupplier, IntSupplier, …) for primitive fields #449

Done

Description

@igel-devin-ai

Problem

SupplierMethodGenerator always generates Supplier<T>, using the boxed type for primitive fields:

// generated today (example/generated-example-builder)
public ProductWithDefaultsBuilder active(Supplier<Boolean> activeSupplier)    // boolean active
public JacksonIntegrationDtoBuilder age(Supplier<Integer> ageSupplier)        // int age
public ProductRecordBuilder price(Supplier<Double> priceSupplier)             // double price

This boxes on every call, can throw a NullPointerException when the supplier returns null for a primitive field, and is flagged by Sonar rule java:S4276 in every generated builder. It came up in #429, where the hand-maintained DTO builders were switched to BooleanSupplier manually.

Proposal

For fields with a primitive type, generate the matching specialised interface:

Field type Generated parameter Call
boolean BooleanSupplier getAsBoolean()
int IntSupplier getAsInt()
long LongSupplier getAsLong()
double DoubleSupplier getAsDouble()
byte, short, char, float Supplier<Byte> etc. (no JDK interface) unchanged
wrapper types (Boolean, Integer, …) Supplier<Boolean> etc. (null is a valid value) unchanged

Also check the other generators that wrap the field type in a functional interface (e.g. update helpers, conditional methods) for the same pattern, so that the matching primitive specialisation is chosen everywhere it exists.

Things to consider

  • Compatibility: lambdas and method references keep compiling, but callers passing an existing Supplier<Boolean> variable break. This needs a release note or a configuration option (default on/off to decide).
  • Javadoc examples generated with addExampleChainFragmentWithSupplier must use the new type.
  • Regenerate the committed examples and extend the processor tests for every row of the table.

Written by Devin

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