Skip to content

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

Description

@AndreasIgel

Location

Generated builder code for fields with primitive array types like int[], double[], boolean[], etc.

Description

When a DTO has a primitive array field (e.g., int[] scores), the generated builder produces collection helper methods that call List<Integer>.toArray(new int[0]). This does not compile because toArray(T[]) requires an object array type (T must be a reference type), but int is a primitive type.

Generated code (broken)

publicComplexTypesBenchmarkBuilderscores(List<Integer> scores) {
this.scores = changedValue(scores.toArray(newint[0]));
returnthis;
}
publicComplexTypesBenchmarkBuilderscores(Consumer<ArrayListBuilder<Integer>> scoresBuilderConsumer) {
ArrayListBuilder<Integer> builder = this.scores.isSet()
? newArrayListBuilder<Integer>(java.util.List.of(this.scores.value()))
: newArrayListBuilder<Integer>();
scoresBuilderConsumer.accept(builder);
this.scores = changedValue(builder.build().toArray(newint[0]));
returnthis;
}

Compilation errors

no suitable method found for toArray(int[])
method Collection.<T>toArray(IntFunction<T[]>) is not applicable
(cannot infer type-variable(s) T
(argument mismatch; int[] cannot be converted to IntFunction<T[]>))
method List.<T>toArray(T[]) is not applicable
(inference variable T has incompatible bounds
equality constraints: Integer
lower bounds: int)
incompatible types: inference variable E has incompatible bounds
equality constraints: Integer
lower bounds: int[]

Reproduction

@SimpleBuilderpublicclassComplexTypesBenchmark {
privateint[] scores;
publicint[] getScores() {
returnscores;
}
publicvoidsetScores(int[] scores) {
this.scores = scores;
}
}

Expected

The generated collection helper methods should produce compilable code for primitive array types.

Actual

The generated code uses toArray(new int[0]) which fails to compile because int is not a reference type and cannot be used as the type parameter T in toArray(T[]).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions