Skip to content

#4145 Use the adder for expression and constant mappings - #4151

Open
matheuslealpa wants to merge 1 commit into
mapstruct:mainfrom
matheuslealpa:fix/4145-adder-expression-constant
Open

matheuslealpa wants to merge 1 commit into
mapstruct:mainfrom
matheuslealpa:fix/4145-adder-expression-constant

Conversation

@matheuslealpa

Copy link
Copy Markdown

Fixes #4145

With CollectionMappingStrategy.ADDER_PREFERRED (or any target whose write accessor is an adder), @Mapping(target = "list", expression = ...) and @Mapping(target = "list", constant = ...) generated code that does not compile:

if ( target.addList() != null ) {
    String string = java.util.Arrays.asList( "test2" );
    if ( string != null ) {
        target.addList().putAll( string );
    }
}

Cause
JavaExpressionMappingBuilder and ConstantMappingBuilder only distinguish a setter (or field) from everything else and treat everything else as a getter, wrapping the assignment in GetterWrapperForCollectionsAndMaps. With an adder, the write accessor takes one argument and targetType is the element type, hence the adder call without arguments and the String local variable.

Changes

  • JavaExpressionMappingBuilder: when the write accessor is an adder, the expression is assigned to a local Iterable<T> variable and wrapped in AdderWrapper, so the adder is called for each element, as it already is for source properties.
  • ConstantMappingBuilder: when the write accessor is an adder, the constant is added as a single element.
  • TypeFactory.iterableTypeOf(Type): new helper that builds Iterable<T>, next to the existing classTypeOf.
  • Issue4145Test: covers both cases. Without the change it fails with the compilation errors from the issue.

Generated code now:

Iterable<String> list = java.util.Arrays.asList( "a", source.getName() );
if ( list != null ) {
    for ( String list1 : list ) {
        target.addList( list1 );
    }
}
target.addList( "c" );

Known limitation
The type of an expression is unknown to the processor, so it is assumed to be an Iterable of the adder's element type, in the same way the getter handling assumes the target collection type. An expression that returns an array or a Stream does not compile against the Iterable<T> local variable. It has to be converted in the expression itself, e.g. Arrays.asList( ... ) or .collect( Collectors.toList() ).

Arrays could be supported by iterating over the expression directly (for ( T item : <expression> )), but that would drop the null check, and a Stream would need forEach, which does not work for arrays. I kept the null check and the single form. I'm happy to change this if you prefer a different trade-off.

Constant as a single element
A constant is one value, so it results in one adder call (target.addList( "c" )). This was proposed in the issue and matches what the reporter expects.

The full processor test suite passes locally on Java 21.

With an adder as the target write accessor, expression and constant
mappings fell back to the getter handling and generated code that
called the adder without arguments, which does not compile.

An expression is now assigned to a local Iterable variable and the
adder is called for each of its elements. A constant is added as a
single element.
Sign up for free to 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.

Adder not working together with expression

1 participant